简体   繁体   English

向量迭代器C ++

[英]vector iterators c++

I am a little confused by the way begin and end work they seem to me to be inconsistant. 我对开始和结束工作的方式感到有些困惑,在我看来它们是不一致的。 When going forward and backwards they have different behaviors. 在前进和后退时,它们具有不同的行为。

vector<Actor *> a;
a.push_back(new Actor(11));
a.push_back(new Actor(22));
a.push_back(new Actor(33));
vector<Actor *>::iterator it = a.begin();


int x  =0;
while(a.begin()+x != a.end()){
cout << (*(a.begin()+x)) << "\n";
x++;
}

cout << "\n";

int y = 1; // if this is set to 0 then its a seg fault =/ when I access 
while(a.end()-y != a.begin()){
cout << (*(a.end()-y)) << "\n";
y++;
}

Outputs 产出

0x979a008
0x979a028
0x979a018


0
0x979a018
0x979a028

How can I get the expected pattern 我如何获得预期的模式

0x979a008
0x979a028
0x979a018

0x979a018
0x979a028
0x979a008

You should use reverse iterators : 您应该使用反向迭代器

int y = 0;
while(a.rbegin() +y != a.rend()){
    cout << (*(a.rbegin()+y)) << "\n";
    y++;
}

Or even better would be to use the overloaded ++ operator of the iterators themselves: 甚至更好的方法是使用迭代器本身的重载++运算符:

auto iter = a.rbegin();
while(iter != a.rend()){
    cout << *(iter++) << "\n";
}

Note that begin() points to the first element of the vector, but end() points past the last element . 请注意, begin()指向向量的第一个元素,但是end()指向最后一个元素 It's never safe to dereference end() , but you can compare iterators to it. 取消引用end()永远都不安全,但是可以将迭代器与其进行比较。

If the vector is empty, then begin() == end() , and you may not dereference either one. 如果向量为空,则begin() == end() ,并且您都不能取消引用。

A more idiomatic way to loop over a vector's elements is: 遍历向量元素的一种更惯用的方法是:

for (vector<Actor*>::iterator i = a.begin(); i != a.end(); ++i) {
   // do something here
}

To iterate in reverse, it's simpler to use rbegin() and rend() , which work much the same way and begin() / end() , but iterate in reverse order: 要反向进行迭代,使用rbegin()rend()更简单,它们的工作方式与begin() / end()大致相同,但是以相反的顺序进行迭代:

for (vector<Actor*>::reverse_iterator i = a.rbegin(); i != a.rend(); ++i) {
   // do something here
}

Also, if you don't intend to modify the elements, you should use a const_iterator (or const_reverse_iterator instead. 另外,如果您不打算修改元素,则应使用const_iterator (或const_reverse_iterator

One very simple way to achieve that would be following 一种非常简单的方法来实现

// first element to the last
auto it = a.begin()
while (it != a.end())
{
cout<<*it<<"\n";
++it;
}
cout<<"\n"
// Last element to first
auto rit = a.rbegin()
while(rit != a.rend())
{
cout<<*rit<<"\n";
++rit;
}

NB: Do not try to dereference a.end() and beyond. 注意:请勿尝试取消引用a.end()及其以后的内容。 When y = 0 in your program the a.end() is dereferenced in the line cout << (*(a.end()-y)) << "\\n"; 当程序中y = 0 ,在cout << (*(a.end()-y)) << "\\n";行中取消对a.end()的引用cout << (*(a.end()-y)) << "\\n"; This results in seg fault. 这会导致段故障。 Elements of vector are contained in a sequence which can be accessed from begin() through end()-1 . 向量的元素包含在可以从begin()end()-1进行访问的序列中。 .end() points to one "past" the last element of the container and should not be dereferenced. .end()指向容器的最后一个元素“过去”,因此不应取消引用。

std::for_each(a.begin(), a.end(), [](const Actor *& a){ std::cout << a; });
std::for_each(a.rbegin(), a.rend(), [](const Actor *& a){ std::cout << a; });



auto print_actor = [](const Actor *& a){ std::cout << a; };
std::for_each(a.begin(), a.end(), print_actor);
std::for_each(a.rbegin(), a.rend(), print_actor);

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

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