c++ - 是否可以使用索引迭代器访问另一个数组的索引?

标签 c++ pointers stdvector

我试图在循环中使用构造为std::vector<std::array<int,3>>::iterator数据类型的迭代器来访问std::array的索引,但是每当取消引用和索引迭代器时,我都会收到错误“无运算符“*”与这些匹配操作数”。我该怎么做才能解决此问题?

std::vector<std::array<int,3>> x = {{0,1,2},{5,1,6}};
std::vector<std::array<int,3>>::iterator it;
std::array<int, 1000> y;
for (it = x.begin(); it != x.end(); it++) {
     if (y[*it[0]] == 0){
          do something;
     }

最佳答案

[]的优先级高于*(https://en.cppreference.com/w/cpp/language/operator_precedence)的优先级
您需要编写(*it)[0]
附带说明,使用range-for loop
<algorithm> (std::for_each()std::transform()std::find_if() ...)不太容易出错
而不是明确处理迭代器(或索引)
在一个循环中。

关于c++ - 是否可以使用索引迭代器访问另一个数组的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63871388/

相关文章:

c++ - 处理 QSqlDatabase 连接

c++ - 我正在为祖母的生日开发C++程序,但是我一直收到错误代码:error C2451

c - "segmentation fault"可能是由 C 中的内存分配引起的

c++ - 使优先级队列限制其在 C++ 中的内存

c++ - 如何编写一个接受不同大小的特征矩阵的 std::vector 的函数?

c++ - iostream 上的异常

c++ - 编写跨平台程序

c - 数组指针数组上的指针

用于扫描和打印任何基本数据类型数组的 C 函数

c++ - C++:从vector <byte>的任何位置获取int