Jump to content

Proper iteration.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
FakeRobotGymnast

FakeRobotGymnast

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
I'll just include the code. The question is built-in =P

int main(void)

{

	vector<int> myVector;


	myVector.push_back(1);

	myVector.push_back(2);

	myVector.push_back(3);


	// Why should I use this...

	for(vector<int>::iterator i = myVector.begin(); i != myVector.end(); ++i)

		cout << *i << endl;


	// Instead of this?

	for(UINT i = 0; i < myVector.size(); ++i)

		cout << myVector[i] << endl;

	

	system("PAUSE");

	return 0;

}


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
To be honest, I can't name a good reason to prefer iterators over the [] operator, other than to help get you familiar with iterators. The [] operator is not defined for all container types, but iterators are.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog