STL forward_list iterator

STL iterators are confusing the crap out of me. How do I properly declare an STL forward_list iterator without causing a trainwreck of errors from my compiler?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <forward_list>/

using namespace std;
int nextInt(int lower_bound, int upper_bound);

int main(){
	forward_list<int> myList;

	for(int loop = 0; loop < 10; loop++){
		myList.push_front(nextInt(0, 100));
	}

	_Flist_iterator<int> * myPoint = myList.before_begin();
/*        iterator myIt;  //doesn't work either */

	myList.before_begin();
		
	cout << "Pausing..." << endl;
	system("pause");
	return 0;
}


I'm trying to use recursion to process a forward list as a priority queue. I want to do something like(and this is really rough):

1
2
3
4
5
6
7
8
9
10
11
//scribbleCode
int processQueue(iterator itObj){

   dequeue(whateverIteratorPointsTo);
   if(population >0){
      processQueue(itObj++);
   } else{
      return -1;
   }
}

1: But I'm having a hell of a time properly declaring, an iterator of forward_list, to the extent that I'm not sure if that is what I should be doing.
2: Should I be doing that?
3: Could I just use a pointer instead? If so, how do I properly declare it?
Last edited on
iterator is a nested class in whatever container class you're using. So simply declaring an iterator won't work... because it could be a vector<int> iterator, or a list<double> iterator... or whatever.

The full qualified name would be:
 
std::forward_list<int>::iterator myIt;


BUT... if you're using C++11 you don't even have to do that. You can just use auto (which is there for this very purpose):

1
2
auto myIt = myList.before_begin(); // <- auto automatically figures out which type you want
  // based on the assignment. 
Hah, thanks a ton. I just "figured it out" before I checked back here to get the thread deleted.

So, essentially, an iterator is just an overloaded pointer to a container's links?

If I pass the iterator by value is that essentially just passing another copy of the pointer? I'm pretty clear on how reference and value pointers work, due to experimenting, but not about iterators. They're confusing me right now.
Last edited on
If it helps to think of iterators as a pointer, that's a pretty safe way to look at them. They're basically pointers to a node in whatever container.

The key difference is that iterators are "smarter" in that incrementing them makes them look at the next node in the container... as opposed to pointers who simply look at the next area in memory when incremented.
Topic archived. No new replies allowed.