C++ 如何迭代常量向量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4890497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:51:25  来源:igfitidea点击:

How do I iterate over a Constant Vector?

c++visual-c++

提问by unj2

I have a vector of Student which has a field name.

我有一个带有字段名称的 Student 向量。

I want to iterate over the vector.

我想遍历向量。

void print(const vector<Student>& students)
    {
    vector<Student>::iterator it;
    for(it = students.begin(); it < students.end(); it++)
        {
            cout << it->name << endl;
        }
    }

This is apparently illegal in C++.

这在 C++ 中显然是非法的。

Please help.

请帮忙。

回答by eq-

You have two (three in C++11) options: const_iterators and indexes (+ "range-for" in C++11)

您有两个(C++11 中的三个)选项:const_iterators 和索引(+ C++11 中的“range-for”)

void func(const std::vector<type>& vec) {
  std::vector<type>::const_iterator iter;
  for (iter = vec.begin(); iter != vec.end(); ++iter)
    // do something with *iter

  /* or
  for (size_t index = 0; index != vec.size(); ++index)
    // do something with vec[index]

  // as of C++11
  for (const auto& item: vec)
    // do something with item
  */
}

You should prefer using !=instead of <with iterators - the latter does not work with all iterators, the former will. With the former you can even make the code more generic (so that you could even change the container type without touching the loop)

您应该更喜欢 using!=而不是<with 迭代器 - 后者不适用于所有迭代器,前者可以。使用前者,您甚至可以使代码更通用(这样您甚至可以在不触及循环的情况下更改容器类型)

template<typename Container>
void func(const Container& container) {
  typename Container::const_iterator iter;
  for (iter = container.begin(); iter != container.end(); ++iter)
    // work with *iter
}

回答by Fred Foo

Use const_iteratorinstead. An iteratorallows modification of the vector, so you can't get one from a constcontainer.

使用const_iterator来代替。Aniterator允许修改vector,因此您无法从const容器中获取。

Also, the idiomatic way to write this loop uses it != students.end()instead of <(though this should work on a vector).

此外,编写此循环的惯用方式使用it != students.end()而不是<(尽管这应该适用于 a vector)。

回答by Shital Shah

C++11 style:

C++11 风格:

void print(const vector<Student>& students) {
    for(auto const& student : students) {
            cout << student.name << endl;
    }
}

回答by user511274

Instead of vector<Student>::iterator, use vector<Student>::const_iterator.

而不是vector<Student>::iterator,使用vector<Student>::const_iterator

回答by Blastfurnace

void print(const vector<Student>& students)
    {
    for(auto it = students.begin(); it != students.end(); ++it)
        {
            cout << it->name << endl;
        }
    }

回答by Kirill V. Lyadvinsky

void print(const vector<Student>& students)
    {
    vector<Student>::const_iterator it; // const_iterator
    for(it = students.begin(); it != students.end(); it++)
        {
            cout << it->name << endl;
        }
    }