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 :(
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

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