STL.vector.iterator的序号
ZC:网上查到,使用vector时,只要将 find到的iterator(itX)减去vector::begin() 就可以得到itX的序号.
1、需求:得到 某个 iterator在 vector中是第几个(即 获取序号)
2、测试代码:(Win7x64,vs08x86)
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> #include <io.h> #include <map>
#include <math.h>
#include <list>
#include <string>
#include <sstream>
#include <algorithm>// std::find(...)
#include <vector>
using namespace std; //#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>
using namespace std; /*
ZC: 网上查到,使用vector时,只要将 find到的iterator(itX)减去vector::begin() 就可以得到itX的序号.
ZC: 但是,原理不明白...
ZC: 可靠性 也未有验证,能看到 高手的留言 或者 源码的查证?
ZC: vector<string>::iterator 的结构不明,iterator的相减到底是什么样的操作,需要看 STL源码??
//*/
// ZC: 估计是 iterator中 有重载 减号操作符?使得 在用于vector的时候,详见能够得到 序号? #pragma warning(disable:4996)
#pragma warning(disable:4786)// ZC: 貌似VC6使用map的时,在build的时候会报很多warning,用这个 禁止显示这些warning
#include <map>
#include <string>
#include <algorithm>// std::find(...)
#include <vector>
using namespace std; void main()
{
vector<string> vtr;
vtr.push_back("");
vtr.push_back("");
vtr.push_back("");
vtr.push_back("");
vtr.push_back("");
printf("%d\n", sizeof(string)); // ZC: 这里想用地址 来看看:是否 两个iterator的地址之差 和 idx 有什么联系,但是 没找到什么联系,估计还是得看源码 或者 别人的解释
vector<string>::iterator itBegin = vtr.begin();
vector<string>::iterator itEnd = vtr.end();
printf("0x%08X - 0x%08X\n", itBegin, itEnd);
int* piBegin = (int*)&itBegin;
int* piEnd = (int*)&itEnd;
printf("0x%08X - 0x%08X\n", *piBegin, *piEnd); vector<string>::iterator it = std::find(vtr.begin(), vtr.end(), "");
if (it == vtr.end())
printf("it == vtr.end()\n");
else
{
printf("it != vtr.end()\n");
printf("it : %s\n", it->c_str());
} printf("0x%08X - 0x%08X\n",
it->c_str(),
vtr.begin()->c_str() );// ZC: 这里相差 64(十进制)
printf("0x%08X - 0x%08X = %d(10进制)\n",
it, itBegin, (it-itBegin) );// ZC: 这里相差 40(十进制) int iIdx = it - vtr.begin();
printf("iIdx : %d, it-it0: %d\n", iIdx, (it-itBegin));
iIdx = iIdx / sizeof(string);
printf("iIdx : %d\n", iIdx);
printf("%d : %s\n", iIdx, vtr.at(iIdx).c_str());
//*/
system("pause");
}
3、
4、
5、
STL.vector.iterator的序号的更多相关文章
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- STL vector
STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- STL vector使用方法介绍
介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
- C++STL vector详解(杂谈)
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- 迭代器类vector::iterator 和 vector::reverse_iterator 的实现、迭代器类型、常用的容器成员
一.迭代器 迭代器是泛型指针 普通指针可以指向内存中的一个地址 迭代器可以指向容器中的一个位置 STL的每一个容器类模版中,都定义了一组对应的迭代器类.使用迭代器,算法函数可以访问容器中指定位置的元素 ...
- C++ stl vector介绍
转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...
随机推荐
- 用友U8存货分类通过DataTable生成EasyUI Tree JSON
<%@ WebHandler Language="C#" Class="InventoryClass" %> using System; using ...
- CKFinker 2.5.0.1 去demo标示
演示版会在文件浏览界面显示演示消息,分别是左下角(文件夹框下面)和列表框上部 都是通过修改ckfinder.js来实现 右下角标示: 查找: {if(C.getItem(E).rd("\x7 ...
- scrollview嵌套tableview
之前写过一次,忘记了, 用的通知 FS
- python摸爬滚打之day18----instance, type, issubclass和反射
1.issubclass(a,b) ----> 判断a是否是b的子类. isinstance(a,b) ----> 判断a这个对象是否是b类型的(可以向上判断, 即考虑继承关系往父类 ...
- PostgreSQL 自动输入密码(转)
原文:https://www.cnblogs.com/litmmp/p/5122534.html 在 Shell 命令行中,使用 postgresql-client 连接 PostgreSQL 数据库 ...
- JDBC最原始的代码做查询操作
首先编写一个User类. public class User { private String username; private String password; public String get ...
- gzframework demo搭建
感谢框架作者,这里给出他的博客 http://www.cnblogs.com/GarsonZhang/ 背景:由于作者对代码的持续开发,导致了以前博客中的下载地址和构建方法和目前的项目不对应,这里给出 ...
- eclipse web module版本问题:Cannot change version of project facet Dynamic Web Module to 2.5.
Description Resource Path Location TypeCannot change version of project facet Dynamic We ...
- python数据结构-如何为元组中的每个元素命名
如何为元组中的每个元素命名 简述 当对象数据格式固定时,用元组比列表更节省内存空间, 我们使用索引访问元组元素,但是这种访问方式会降低程序的可读性. 举个栗子 对于学生的信息,我们有固定的数据格式,我 ...
- Install rapyuta Robot Cloud Engine on Ubuntu12.04
Prepare on ubuntu12.04 sudo apt-get install vim Install fuerte ROS sudo sh -c 'echo "deb http:/ ...