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 ...
随机推荐
- shell符号
*: 通配符 *.c : c结尾的文件 *v : v结尾的文件 v* : v开头的文件
- Python学习之旅(七)
Python基础知识(6):基本数据类型之列表 在Python中,最基本的数据结构是序列.序列中的每个元素被分配一个序号——即元素的位置,也称为索引.第一个索引从0开始,如果要从右边开始,序列中的最后 ...
- Grunt Bower构建前端
Grunt + Bower—前端构建利器 目前比较流行的WEB开发的趋势是前后端分离.前端采用重量级的Javascript框架,比如Angular,Ember等,后端采用restful API的W ...
- [教程]微信官方开源UI库-WeUI使用方法【申明:来源于网络】
[教程]微信官方开源UI库-WeUI使用方法 [ 教程]微信官方开源UI库-WeUI使用方法 地址:http://www.weui.org.cn/?/article/1 微信公众号开发-WeUI使用说 ...
- python2,python3同时安装时,python3可以安装并升级pip库,python2报错的解决办法
最近在使用pip安装包的的时候出现下面错误 UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f' 查询资料后发现原因是p ...
- xcode代码提示没了
defaults write com.apple.dt.XCode IDEIndexDisable 0 https://www.jianshu.com/p/57a14bed9d1b
- poj3278
#include<iostream> #define MAX 100001 int john,cow; int queue[MAX]; int vis[MAX]; int ans; voi ...
- LeetCode 521 Longest Uncommon Subsequence I 解题报告
题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...
- vue 点击弹窗外框关闭弹框
https://blog.csdn.net/zjw0742/article/details/77822777 ready() { document.addEventListener('click', ...
- MongoDB 查询 $关键词 方法目录
MongoDB $关键字 关系比较符号 $lt $lte $gt $gte $ne MongoDB 查询$关键字 $in $or $all MongoDB limit 选取 skip跳过 sort排序 ...