954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dereferencing vector.begin() not dereferencable...

vector<int> iv;
	iv.push_back(4);
	vector<int>::iterator it = iv.begin();
	cout << *it;

	cout << *(iv.begin()); //debug assertion here


Why can't I dereference iv.begin() directly?

Jsplinter
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

I compiled this with Code::Blocks and Visual Studio and both worked just fine.

sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

Compiles (and runs) using gcc, too. Here's my entire program, built from your snippet:

#include "vector"
#include "iostream"

using namespace std;

int main ()
{
	vector<int> iv;
	iv.push_back(4);
	vector<int>::iterator it = iv.begin();
	cout << *it;

	cout << *(iv.begin()); //debug assertion here
	return 0;
}


This doesn't compile on your system?

mzimmers
Junior Poster in Training
81 posts since Oct 2011
Reputation Points: 10
Solved Threads: 8
 

Hmm, I have no idea why it is causing a problem on my system. It compiles but throws an exception in debug mode. I thought it should certainly work. But The code was cut and pasted and it wasn't working in debug mode of VS2010. I thought I was losing my mind. Glad to know that something else was going on.

Jsplinter
Junior Poster in Training
65 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You