Problem with iterator for vector of lists in c++

  • Context: C/C++ 
  • Thread starter Thread starter dmatador
  • Start date Start date
  • Tags Tags
    C++ Vector
Click For Summary
SUMMARY

The discussion centers on a compiler error encountered while using an iterator with a vector of lists in C++. The user mistakenly declared a mutable iterator instead of a constant iterator for the list within a constant vector. The correct declaration is list >::const_iterator it, which resolves the issue by matching the type of the list returned by adj_list[0].begin(). This distinction between iterator and const_iterator is crucial for avoiding type mismatches in C++.

PREREQUISITES
  • Understanding of C++ standard library containers, specifically vector and list
  • Knowledge of iterators and the difference between iterator and const_iterator
  • Familiarity with the pair template in C++
  • Basic understanding of C++ syntax and compiler error messages
NEXT STEPS
  • Study the differences between iterator and const_iterator in C++
  • Learn about the C++11 feature of auto for type inference
  • Explore the usage of std::vector and std::list in C++
  • Investigate common compiler errors related to type mismatches in C++
USEFUL FOR

C++ developers, software engineers, and students learning about standard library containers and iterators in C++. This discussion is particularly beneficial for those encountering similar iterator-related compiler errors.

dmatador
Messages
120
Reaction score
1
For a function

using namespace std;

mst(const vector<list<pair<int,int> > > &adj_lists, int v){
// code before iterator
list<pair<int,int> >:: iterator it;
it = adj_list[0].begin();
// code after
}

I'm trying to set up an iterator to walk through the list contained within adj_list[0], but the code above gives me a compiler error about there not being a match for "operator=". Am I wrong in believing that the vector should contain the starting point of the list? Thanks for any help.
*** hey everyone while waiting for responses I figured out the solution.
I should have been using

list<pair<int,int> >:: const_iterator it;

instead of

list<pair<int,int> >:: iterator it;

thanks to anyone who considered it.
 
Last edited:
Technology news on Phys.org
Let's see.

adj_list is of type const vector<list<pair<int,int> > > &

Therefore, adj_list[0] is of type vector<list<pair<int,int> > >::const_reference, which ought to be of type const list<pair<int,int> > &.

Therefore, adj_list[0].begin() is of type list<pair<int,int> >::const_iterator

And since you generally cannot implicitly convert Container::const_iterator into Container::iterator, the compiler rightfully complains!
 
Ah, iterator vs const_iterator distinctions.

C++1x needs to hurry up so we can use "auto" already :(
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
12
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 52 ·
2
Replies
52
Views
4K