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 ...
随机推荐
- BASE64和图片之间的互相转换
package com.test.demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java. ...
- 查看Sql Server 数据库的内存使用情况
-- 查询SqlServer总体的内存使用情况 select type , sum(virtual_memory_reserved_kb) VM_Reserved , sum(virtual_memo ...
- 浅析python中的装饰器decorator
最近学习python,其中decorator比较难理解,遂写一篇来总结供后续查阅. 定义一个函数,想在运行时动态的改变函数的功能,又不想改变函数本身的代码,可以使用高阶函数(可以使用函数作为参数) 装 ...
- Spring中IOC和AOP的详细解释(转)
原文链接:Spring中IOC和AOP的详细解释 我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂 ...
- mysql限制用户只能访问指定数据库
1.使用root账户登录mysql mysql -uroot -ppassword 2.进入mysql数据库 mysql > use mysql 3.限制用户权限 GRANT SELECT, I ...
- python 字节转换成图像
python 字节转换成图像 使用base64 1.图片转成字节使用: base64.b64encode() 2.字节转成图片: base64.b64decode() 图片字节串: iVBORw0K ...
- Javascript根据id获取数组对象
在业务中,列表页跳转详情页时,经常会将Id值传入,然后再根据id值异步获取数据. 假设有服务端的json数据: <注意,这里的data是指已经从后端获取的json, 而非后端原始的文件> ...
- qt 安装包生成2
使用Qt Installer Framework制作安装包 2018年07月01日 03:45:37 大黄老鼠 阅读数:878 标签: qt更多 个人分类: Qt 版权声明:本文为博主原创文章,未 ...
- 133. Clone Graph(图的复制)
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains ...
- SVN拉分支,合并分支
前提是:本地已安装SVN,且在SVN中新建好branch和tag目录 拉分支: 把svn内容下载到本地,然后右键TortoiseSVN-->Branch/tag... 上图中红框选择存放的路径, ...