首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在向量中使用迭代器循环

在向量中使用迭代器循环
EN

Stack Overflow用户
提问于 2016-06-07 02:50:46
回答 2查看 70关注 0票数 0

我刚刚在向量中的迭代器上写了一个测试程序,在开始的时候,我刚刚创建了一个向量并用一系列数字1-10初始化它。

之后,我创建了一个迭代器"myIterator“和一个const迭代器"iter”。我用iter来显示载体的内容。

后来,我将"myIterator“分配给"anotherVector.begin()”。所以他们指的是同样的东西。

经检查

代码语言:javascript
复制
//cout << /* *myIterator << */"\t" << *(anotherVector.begin()) << endl;

因此,在第二个迭代器循环中,我只是将"anotherVector.begin()“替换为myIterator。

但这产生了不同的产出。

守则是:

代码语言:javascript
复制
    vector<int> anotherVector;

for(int i = 0; i < 10; i++) {
    intVector.push_back(i + 1);
    cout << anotherVector[i] << endl;
}

    cout << "anotherVector" << endl;

//*************************************
//Iterators

cout << "Iterators" << endl;

vector<int>::iterator myIterator;
vector<int>::const_iterator iter;

for(iter = anotherVector.begin(); iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

cout << "Another insertion" << endl;

myIterator = anotherVector.begin();

//cout << /* *myIterator << */"\t" << *(anotherVector.begin()) << endl;

myIterator[5] = 255;
anotherVector.insert(anotherVector.begin(),200);

//for(iter = myIterator; iter != anotherVector.end(); ++iter) {
    //cout << *iter << endl;
//}

for(iter = anotherVector.begin(); iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

输出使用

代码语言:javascript
复制
for(iter = anotherVector.begin(); iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

给予:

代码语言:javascript
复制
    Iterators
    1
    2
    3   
    4
    5
    6
    7
    8
    9
    10
    Another insertion
    200
    1
    2
    3
    4
    5
    255
    7
    8
    9
    10

和输出使用

代码语言:javascript
复制
for(iter = myIterator; iter != anotherVector.end(); ++iter) {
    cout << *iter << endl;
}

给予:

代码语言:javascript
复制
    Iterators
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Another insertion
    0
    0
    3
    4
    5
    255
    7
    8
    9
    10
    81
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    0
    0
    0
    0
    0
    0
    0
    0
    97
    0
    200
    1
    2
    3
    4
    5
    255
    7
    8
    9
    10

如果他们只是指向同一个地址,为什么会有这么大的差异。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-07 02:56:50

在您的insert之后,myIterator不再一定有效。这是因为插入到std::vector中会导致向量重新分配,因此先前迭代器所指向的地址可能不会指向重新分配的向量的地址空间。

票数 3
EN

Stack Overflow用户

发布于 2016-06-07 03:06:27

我刚刚发现了我的错误,但是您可以检查迭代器地址位置的变化。

代码语言:javascript
复制
myIterator = anotherVector.begin();

    cout << "test line\t" << &(*myIterator) << "\t" << &(*(anotherVector.begin())) << endl;

    //myIterator[5] = 255;
    anotherVector.insert(anotherVector.begin(),200);

    cout << "test line\t" << &(*myIterator) << "\t" << &(*(anotherVector.begin())) << endl;

这给出了输出:

插入前

代码语言:javascript
复制
test line   0x92f070    0x92f070

插入后

代码语言:javascript
复制
test line   0x92f070    0x92f0f0

输出可能因机器而异。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37669731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档