简体   繁体   English

如何使用迭代器遍历向量并访问元素值?

[英]how to traversing through vector using iterator and accessing element value?

I started to use iterators for traversing through a vector.我开始使用迭代器来遍历向量。 Usually, I would use this to go through a vector:通常,我会通过向量将其用于 go:

for( int i=0; i< vector.size(); i++){
    cout<<vector[i];
}

I learned if I want to use iterators I would have to do something like this:我了解到,如果我想使用迭代器,我必须这样做:

vector<int>::iterator col;
for(col = vector.begin(); col != vector.end(); col++){
    cout << *col;
}

But, if I want to access the value at which the iterator is pointing, how would I do that?但是,如果我想访问迭代器指向的值,我该怎么做呢?

I tried to do this:我试图这样做:

 int temp = *col;

but this gives an error但这给出了一个错误

Assigning to 'int' from incompatible type 'std::__1::vector<int, std::__1::allocator<int> >'从不兼容类型'std::__1::vector<int, std::__1::allocator<int> >'分配给'int'

I am trying this because I have a 2d vector and I'm trying to find the sum of individual columns.我正在尝试这个,因为我有一个 2d 向量并且我试图找到各个列的总和。

The code you showed is correct... for a 1D vector .您显示的代码是正确的......对于一维向量 But, you say you are actually using a 2D vector , in which case the outer vector's elements are themselves vectors, and so dereferencing an iterator for the outer vector gives you a reference to an inner vector.但是,您说您实际上使用的是 2D vector ,在这种情况下,外部向量的元素本身就是向量,因此取消引用外部向量的迭代器会为您提供对内部向量的引用。 That is exactly what the error message is complaining about - you are trying to assign an inner vector to an int , which will obviously not work.这正是错误消息所抱怨的 - 你试图将一个内部vector分配给一个int ,这显然是行不通的。 You would have to iterate that inner vector separately, same as you would have to do when using indexes instead of iterators, eg:您必须单独迭代该内部向量,就像使用索引而不是迭代器时必须做的一样,例如:

std::vector<std::vector<int> > rows_vec;
...
/*
for(size_t row_idx = 0; row_idx < rows_vec.size(); ++row_idx){
    std::vector<int> &row_vec = rows_vec[row_idx];
    int sum_cols = 0;
    for(size_t col_iter = 0; col_idx < row_vec.size(); ++col_idx){
        sum_cols += row_vec[col_idx];
    }
    // use sum_cols as needed...
}
*/
std::vector<std::vector<int> >::iterator row_iter;
for(row_iter = rows_vec.begin(); row_iter != rows_vec.end(); ++row_iter){
    std::vector<int> &row_vec = *row_iter; 
    std::vector<int>::iterator col_iter;
    int sum_cols = 0;
    for(col_iter = row_vec.begin(); col_iter != row_vec.end(); ++col_iter){
        sum_cols += *col_iter;
    }
    // use sum_cols as needed...
}

If you are using C++11 or later, this can be greatly simplified using range-based for loops :如果您使用的是 C++11 或更高版本,则可以使用基于范围的for循环大大简化:

std::vector<std::vector<int>> rows_vec;
...
for(auto &row_vec : rows_vec){
    int sum_cols = 0;
    for(int col_val : row_vec){
        sum_cols += col_val;
    }
    // use sum_cols as needed...
}

Which could be simplified more by using the standard std::accumulate() algorithm, eg:通过使用标准的std::accumulate()算法可以进一步简化,例如:

std::vector<std::vector<int>> rows_vec;
...
for(auto &row_vec : rows_vec){
    int sum_cols = std::accumulate(row_vec.begin(), row_vec.end(), 0);
    // use sum_cols as needed...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM