简体   繁体   English

将迭代器转换为一对对象

[英]Casting an iterator to a pair of objects

I don't understand the following: 我不了解以下内容:

#include <iostream>
#include <vector>
using namespace std;

class Date {
public:
    Date(int y, int m) {
        year = y;
        month = m;
    }
    int year;
    int month;
};

int main() {
    vector<pair<Date, Date> > ranges;
    Date date1 = Date(2000, 1);
    Date date2 = Date(2000, 2);
    ranges.push_back(pair<Date, Date>(date1, date2));

    for (vector<pair<Date, Date> >::iterator it = ranges.begin(); it != ranges.end(); ++it) {
        (*it).first.year = 2002;
        pair<Date, Date> range = *it;
        range.first.year = 2001;
    }
    cout << ranges[0].first.year << endl;
}

When I run this, "2002" outputs, though I expected "2001". 当我运行此命令时,尽管我期望的是“ 2001”,但是输出“ 2002”。

It seems that casting an iterator does not work as I expected. 似乎投射迭代器无法按我预期的方式工作。 I can access range fine, and read its variables, but I cannot change them - or at least, the changes don't stick. 我可以很好地访问range ,并读取其变量,但是我无法更改它们-至少,更改不会生效。

I've fixed my problem by using (*it) instead of casting, but I'd like to know why my casting does not work. 我已经通过使用(*it)而不是强制转换解决了我的问题,但是我想知道为什么我的强制转换不起作用。

I recommend using range-based for loops instead, like this: 我建议改为使用基于范围的for循环,如下所示:

for (auto& range : ranges)
{
    ...
    range.first.year = 2001;
    ...
}

You can use a reference, like in the example above, and modify the collection directly. 您可以像上面的示例一样使用引用,并直接修改集合。

In the loop 在循环

for (vector<pair<Date, Date> >::iterator it = ranges.begin(); it != ranges.end(); ++it) {
    (*it).first.year = 2002;
    pair<Date, Date> range = *it;
    range.first.year = 2001;
}

range is a local variable of the loop. range是循环的局部变量。 In each iteration of the loop (there is only one iteration because the vector contains only one element) this variable is initialized by a copy of the value of the element of the vector. 在循环的每次迭代中(由于矢量仅包含一个元素,因此只有一个迭代),此变量通过矢量元素值的副本进行初始化。 So any changes of this local variable do not influence on the elements of the vector. 因此,此局部变量的任何更改都不会影响矢量的元素。 The vector itself was not changed. 矢量本身未更改。

Instead of these two lines 代替这两行

    pair<Date, Date> range = *it;
    range.first.year = 2001;

you could write simply 你可以简单地写

( *it ).first.year = 2001;

In this case you would directly change the element of the vector. 在这种情况下,您将直接更改向量的元素。

Or the other way to change the value of an element of the vector is to use a reference to the element instead of creating a separate object. 或更改向量元素值的另一种方法是使用对该元素的引用,而不是创建单独的对象。 For example 例如

for (vector<pair<Date, Date> >::iterator it = ranges.begin(); it != ranges.end(); ++it) {
    (*it).first.year = 2002;
    pair<Date, Date> &range = *it;
    range.first.year = 2001;
}

In this case as the reference is used the original element of the vector will be changed. 在这种情况下,将使用参考作为矢量的原始元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM