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的序号的更多相关文章

  1. C++ STL vector容器学习

    STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...

  2. STL vector

    STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...

  3. STL vector用法介绍

    STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...

  4. STL vector 用法介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  5. STL vector使用方法介绍

    介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  6. stl——vector详解

    stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...

  7. C++STL vector详解(杂谈)

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  8. 迭代器类vector::iterator 和 vector::reverse_iterator 的实现、迭代器类型、常用的容器成员

    一.迭代器 迭代器是泛型指针 普通指针可以指向内存中的一个地址 迭代器可以指向容器中的一个位置 STL的每一个容器类模版中,都定义了一组对应的迭代器类.使用迭代器,算法函数可以访问容器中指定位置的元素 ...

  9. C++ stl vector介绍

    转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...

随机推荐

  1. TensorRT层和每个层支持的精度模式

    下表列出了TensorRT层和每个层支持的精确模式.它还列出了该层在深度学习加速器(DLA)上运行的能力.有关附加约束的更多信息,请参见 DLA Supported Layershttps://doc ...

  2. 程序员调 Bug 的样子,非常真实

    程序员调 Bug 的样子,非常真实

  3. Elasticsearch学习笔记(二)Search API 与 Query DSL

    一. Search API eg: GET /mall/product/_search?q=name:productName&sort=price desc 特点:search的请求参数都是以 ...

  4. java框架之SpringBoot(5)-SpringMVC的自动配置

    本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...

  5. python中接入支付宝当面付

    准备 由于正式环境需要商户信息,所以这里使用支付宝提供的沙箱环境.切换到正式环境后只需稍改配置. 1.点击进入蚂蚁金服平台官网. 2.如下图选择:开发者中心->开发服务下的研发服务->沙箱 ...

  6. Golang--选择、循环语法总结

    1.判断语句if 条件表达式没有括号 支持初始化表达式 初始化语句的变量自在本block内有效 if a,b,c := 1,2,3;a+b+c>6 { fmt.Println("hah ...

  7. Parco_Love_gcd

    传送门 出题人说正解为RMQ,鄙人实在太蒟蒻了,不会呀只能暴力…… #include <bits/stdc++.h> using namespace std; #define ll lon ...

  8. node 解析图片二维码的内容

    const {readFile, readFileSync} = require('fs'); const decodeImage = require('jimp').read; const qrco ...

  9. postman 做接口测试之学习笔记

    Postman 之前是作为Chrome 的一个插件,现在要下载应用才能使用. 以下是postman 的界面: 各个功能区的使用如下: 快捷区: 快捷区提供常用的操作入口,包括运行收藏夹的一组测试数据, ...

  10. Cocos Creator - 入门教程项目 - 博客频道 - CSDN.NET

    3457 教程司令部 [20160418] | Cocos Creator - CocoaChina CocoaChina_让移动开发更简单cocoachina.com 2033 Cocos Crea ...