C++ C++;for vs while循环中的迭代器行为

C++ C++;for vs while循环中的迭代器行为,c++,iterator,C++,Iterator,我不明白为什么用for循环遍历容器会产生与用while循环遍历不同的结果。下面的MWE用一个向量和一组5个整数说明了这一点 #include <iostream> #include <vector> #include <set> using namespace std; int main() { vector<int> v; set<int> s; // add integers 0..5 to vector v and

我不明白为什么用for循环遍历容器会产生与用while循环遍历不同的结果。下面的MWE用一个向量和一组5个整数说明了这一点

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

int main()
{
  vector<int> v;
  set<int> s;

  // add integers 0..5 to vector v and set s
  for (int i = 0; i < 5; i++) {
    v.push_back(i);
    s.insert(i);
  }

  cout << "Iterating through vector with for loop.\n";
  vector<int>::const_iterator itv;
  for (itv = v.begin(); itv != v.end(); itv++) cout << *itv << ' ';
  cout << '\n';

  cout << "Iterating through set with for loop.\n";
  set<int>::const_iterator its;
  for (its = s.begin(); its != s.end(); its++) cout << *its << ' ';
  cout << '\n';

  cout << "Iterating through vector with while loop.\n";
  itv = v.begin();
  while (itv++ != v.end()) cout << *itv << ' ';
  cout << '\n';

  cout << "Iterating through set with while loop.\n";
  its = s.begin();
  while (its++ != s.end()) cout << *its << ' ';
  cout << '\n';
}

for循环按预期工作,但while循环不工作。因为我使用
++
作为后缀,所以我不理解while循环为什么会有这样的行为。另一个谜团是为什么while循环为set
s
打印一个
5
,因为这个数字没有插入
s

,这可能是因为编译器在计算表达式的其余部分之前先计算while表达式中的
its++

您的
while
循环不等同于
for
循环

循环的
相当于

itv = v.begin();
while(itv != v.end()) {
    cout << *itv << ' ';
    itv++;
}
itv=v.begin();
while(itv!=v.end()){
库特
因为我使用了++作为后缀,所以我不明白为什么while循环会有这样的行为


这是因为首先计算while谓词,然后(如果谓词为true)while循环的主体。当您尝试访问主体中的值时,迭代器已递增。

当您使用
for
循环进行迭代时,仅在主体被计算之后递增迭代器。当您使用
while
循环进行迭代时,在检查之后但在主体之前递增迭代器在
循环的最后一次迭代中取消对迭代器的引用,而
循环会导致未定义的行为。

只是一些“随机”风格的提示,主要显示算法使用和现代C++11功能

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    const std::vector<int> v { 0,1,2,3,4 };
    const std::set<int>    s { 0,1,2,3,4 };

    for (auto element : v)
        std::cout << element << ' ';
    std::cout << '\n';

    for (auto element : s)
        std::cout << element << ' ';
    std::cout << '\n';

    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    std::copy(s.begin(), s.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}
#包括
#包括
#包括
#包括
#包括
int main()
{
常数std::向量v{0,1,2,3,4};
常数std::集s{0,1,2,3,4};
用于(自动元件:v)

std::你应该指出这只是C++11,因为不是每个人(MSVC…)都可以使用C++11。MSVC 2012有基于范围的,据我所知,我知道这是旧的,但由于Dan最初在看短线,你可以将你的cout改为“cout@AbhiP:在这种情况下,拥有清晰可读的代码比拥有简短的代码更重要。(一般来说,这是一个好策略)。请注意,我在这里明确表示,增量跟随
cout
。这在内联后增量中不太清晰。
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    const std::vector<int> v { 0,1,2,3,4 };
    const std::set<int>    s { 0,1,2,3,4 };

    for (auto element : v)
        std::cout << element << ' ';
    std::cout << '\n';

    for (auto element : s)
        std::cout << element << ' ';
    std::cout << '\n';

    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    std::copy(s.begin(), s.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}