Problem with iterator for vector of lists in c++

  • C/C++
  • Thread starter dmatador
  • Start date
  • Tags
    C++ Vector
In summary, the conversation is about setting up an iterator to walk through a list contained within a vector, but the code gives a compiler error. The solution is to use a const_iterator instead of an iterator. There is also a mention of the need for C++1x to hurry up so "auto" can be used.
  • #1
dmatador
120
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
  • #2
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!
 
  • #3
Ah, iterator vs const_iterator distinctions.

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

What is an iterator for a vector of lists in c++?

An iterator is a special object that allows you to traverse through the elements of a vector of lists in c++. It acts as a pointer to the current element and can be used to access and manipulate the elements.

Why am I having trouble using an iterator for my vector of lists in c++?

There could be various reasons for this. Some possible reasons include using the wrong type of iterator for your data structure, attempting to access elements beyond the bounds of the vector, or not properly initializing the iterator before using it.

How can I fix the problem with my iterator for a vector of lists in c++?

The solution to your iterator problem will depend on the specific issue you are facing. Some common solutions include checking the type of iterator being used, double-checking the bounds of the vector, and ensuring proper initialization of the iterator before use.

What are the benefits of using an iterator for a vector of lists in c++?

Using an iterator allows for efficient traversal and manipulation of the elements in a vector of lists. It also provides a standardized way to work with data structures, making it easier to write reusable and maintainable code.

Are there any alternatives to using an iterator for a vector of lists in c++?

Yes, there are alternative methods for traversing and manipulating the elements of a vector of lists. These include using a traditional for loop, as well as built-in functions such as std::for_each and std::accumulate. However, iterators are generally considered to be the most efficient and versatile method for working with data structures.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
1
Views
641
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
2
Replies
52
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top