我试图在循环中使用构造为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/