c++ - 将 `auto` 关键字与 STL 迭代器一起使用

标签 c++ c++11 stl iterator auto

我正在关注 thisauto 关键字与 STL 迭代器一起使用的教程。

它说这种语法对于 C++ 11 是可以的。

vector<int> vec;
auto itr = vec.iterator(); // instead of vector<int>::iterator itr

但是,我在尝试时遇到了这个编译错误?

error: invalid use of ‘std::vector::iterator’

最佳答案

教程是错误的。 iterator 是所有集合类中的type 成员,而不是function 成员。它是一些vector的成员函数的返回类型

教程可能写的意思是

vector<int> vec;
auto itr = vec.begin(); // itr is of type std::vector<int>::iterator

关于c++ - 将 `auto` 关键字与 STL 迭代器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49872949/

相关文章:

c++ - 使用模板 <class InputIterator> string (InputIterator begin, InputIterator end) 时出现奇怪的错误;

c++ - boost::sregex_iterator 编译错误

c++ - 与 std::hash 的意外冲突

c++ - 表示函数参数的元组的类型表达式

c++ - Boost语言环境规范化带状字符但不带重音符号

c++ - 使用 C++ Catch 测试浮点 std::vector

c++ - 如何在 C++11 中将 Unix 时间戳字符串转换为 time_t?

c++ - 映射键是 C++ 中多个值的组合

c++ - enable_if 可以用作非额外参数(例如,用于构造函数)吗?

c++ - 在 C++ 中的合并排序中传递数组时出错