C/C++ Problem with iterator for vector of lists in c++

  • Thread starter Thread starter dmatador
  • Start date Start date
  • Tags Tags
    C++ Vector
AI Thread Summary
The discussion centers on a compiler error encountered when trying to set up an iterator for a list within a vector of adjacency lists in C++. The issue arises from using a non-const iterator instead of a const iterator, which leads to a mismatch error. The correct approach is to declare the iterator as a const_iterator, reflecting the const nature of the adjacency list. This distinction between iterator and const_iterator is crucial, as the compiler does not allow implicit conversion between the two. The conversation also touches on the anticipation for C++11 features, such as the "auto" keyword, which would simplify such declarations.
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 :(
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
2
Views
2K
Replies
25
Views
2K
Replies
12
Views
2K
Replies
5
Views
1K
Replies
23
Views
2K
Replies
39
Views
4K
Replies
40
Views
3K
Replies
8
Views
2K
Back
Top