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?

Recommended Answers

All 3 Replies

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

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.