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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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