可能重复:
为什么使用迭代器而不是数组索引?
我正在回顾我对C++的了解,我偶然发现了迭代器.我想知道的一件事是什么让它们如此特别,我想知道为什么:
using namespace std;
vector<int> myIntVector;
vector<int>::iterator myIntVectorIterator;
// Add some elements to myIntVector
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);
for(myIntVectorIterator = myIntVector.begin();
myIntVectorIterator != myIntVector.end();
myIntVectorIterator++)
{
cout<<*myIntVectorIterator<<" ";
//Should output 1 4 8
}
Run Code Online (Sandbox Code Playgroud)
比这更好:
using namespace std;
vector<int> myIntVector;
// Add some elements to myIntVector
myIntVector.push_back(1);
myIntVector.push_back(4);
myIntVector.push_back(8);
for(int y=0; y<myIntVector.size(); y++)
{
cout<<myIntVector[y]<<" ";
//Should output 1 4 8
}
Run Code Online (Sandbox Code Playgroud)
是的,我知道我不应该使用std命名空间.我刚把这个例子从cprogramming网站上删除了.那么请你告诉我为什么后者更糟?有什么大不同?
说,我有一个
std::vector<SomeClass *> v;
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我需要经常在程序中访问它的元素,向前和向后循环它们.
这两者之间的访问类型最快?
迭代器访问:
std::vector<SomeClass *> v;
std::vector<SomeClass *>::iterator i;
std::vector<SomeClass *>::reverse_iterator j;
// i loops forward, j loops backward
for( i = v.begin(), j = v.rbegin(); i != v.end() && j != v.rend(); i++, j++ ){
// some operations on v items
}
Run Code Online (Sandbox Code Playgroud)
下标访问(按索引)
std::vector<SomeClass *> v;
unsigned int i, j, size = v.size();
// i loops forward, j loops backward
for( i = 0, j = size - 1; i < size && j >= …Run Code Online (Sandbox Code Playgroud) 我有问题要通过使用索引访问(使用operator [])或使用迭代器来纠正我对访问向量元素的效率的理解.
我的理解是"迭代器"比"索引访问"更有效.(我认为vector::end()也比效率更高vector::size()).
现在我编写了示例代码测量它(在Windows 7下使用Cygwin,使用g ++ 4.5.3)
索引访问循环版本(以前标记为随机访问):
int main()
{
std::vector< size_t > vec ( 10000000 );
size_t value = 0;
for( size_t x=0; x<10; ++x )
{
for ( size_t idx = 0; idx < vec.size(); ++idx )
{
value += vec[idx];
}
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
迭代器循环代码是这样的:
for (std::vector< size_t >::iterator iter = vec.begin(); iter != vec.end(); ++iter) {
value = *iter;
}
Run Code Online (Sandbox Code Playgroud)
我很惊讶地看到"索引访问"版本更快.我用time命令"测量".数字是:
结果使用
g++ source.cpp(无优化)索引访问真正的800ms
迭代器访问
真正的2200ms …
每当有人开始使用STL并且他们有一个向量时,您通常会看到:
vector<int> vec ;
//... code ...
for( vector<int>::iterator iter = vec.begin() ;
iter != vec.end() ;
++iter )
{
// do stuff
}
我发现整个vector<int>::iterator语法都是恶心的.我知道你可以typedef vector<int>::iterator VecIterInt,这是稍微好一点..
但问题是,好的ol'有什么问题:
for( int i = 0 ; i < vec.size() ; i++ )
{
// code
}
我有一个内存位置,其中包含一个我想要与另一个角色进行比较的角色(并且它不在堆栈的顶部,所以我不能只是pop它).如何引用内存位置的内容以便进行比较?
基本上我如何在语法上做到这一点.
当使用std :: vector时,通过索引而不是使用迭代器来传递所有向量的元素总是更快?
我写了简单的愚蠢测试,VS 2010,优化被禁用
#include <vector>
#include <iostream>
#include <ctime>
const int SIZE = 100000;
int main()
{
std::vector<int> vInt;
int i, temp;
srand(time(0));
for(i = 0; i<SIZE; i++)
vInt.push_back(rand());
time_t startTime, endTime;
std::vector<int>::iterator it = vInt.begin(), itEnd = vInt.end();
startTime = clock();
for( ; it != itEnd; it++)
temp = *it;
endTime = clock();
std::cout<<"Result for iterator: "<<endTime - startTime;
i = 0;
int size = vInt.size();
startTime = clock();
for(; i<size; i++)
temp = vInt[i];
endTime = …Run Code Online (Sandbox Code Playgroud)