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 ...
 
随机推荐
- 根据Excel模板,填写报表,并下载到web浏览器端
			
package com.neusoft.nda.basic.recordmanager.viewelec.servlet; import java.io.File; import java.io.Fi ...
 - java.lang.NoClassDefFoundError 错误
			
练习jfianl,,,配置数据库插件的时候遇到: java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource 解 ...
 - PHP计算年龄
			
<?php $birthday = strtotime('1992-10-03'); $time = time();//2019-03-14; function datediffage($bir ...
 - angular--获取时间方法services
			
写了一些公用方法获取自然周.传入开始和结束日期,获取中间全部日期等方法 .service('DateServices', [function () { // 获取某年自然周的方法 (如果是当年,只返回 ...
 - [js]js中事件的3要素
			
js中事件的3要素 事件源 事件 事件处理程序 <!DOCTYPE html> <html> <head lang="en"> <meta ...
 - 【JVM】-NO.112.JVM.2 -【JDK11 HashMap详解-2-tab[i = (n - 1) & hash])剖析】
			
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
 - json to entity in api
			
using (var client = new HttpClient()) { var WVMId = DB.Vehicles.Where(v => v.Id == new Guid(vehic ...
 - Cocos Creator 加载和切换场景(官方文档摘录)
			
Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...
 - jdk1.8新特性之lambda表达式及在Android Studio中的使用举例
			
Jdk1.8已经出很久了但是很多同学对它的特性在android studio 中的应用可能还不是很熟悉,今天我们就来对这个新特性在AS中做它的应用实践. 一.首先在有JDK1.8的情况下我们要在AS的 ...
 - vue路由1:基本使用
			
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...