Page 1 of 1

Using the vector<type>::iterator With const

Posted: Sun May 17, 2009 7:42 pm
by Maevik
So after Marauder gave me some sound advice, I've been trying to implement the use of iterators into my for loops. However, being the diligent little code monkey I am, I've been trying to use the const keyword wherever I can, and the two concepts are not playing nicely :(

So when I pass a vector to a display function, I use the format:
void display( const vector<type>& data )

which, obviously, allows me to pass by reference and still protect the data in my vector (since a display function should not be allowed to manipulate my data, right)

So anyway, if I try to use the iterator for looping through my vector (which is a const reference), I get an error. I made a simple program to illustrate the error:

Code: Select all

#include <iostream>
#include <vector>

using namespace std;

void display( const vector<int>& data );

int main()
{
	vector<int> nums;

	display( nums );

}
void display( const vector<int>& data )
{
	for( vector<int>::iterator i = data.begin() ; i != data.end() ; ++i )
	{
		cout << *i << endl;
	}
}

Code: Select all

Error	1	error C2440: 'initializing' : cannot convert from 'std::_Vector_const_iterator<_Ty,_Alloc>' to 'std::_Vector_iterator<_Ty,_Alloc>'
So as you can see, we get a type mismatch. I suppose I could remedy this by making a copy of my vector and looping through the copy, but that would take time. My question is: What are the possible solutions for this? Which would tax the system the least or take the least time?

Re: Using the vector<type>::iterator With const

Posted: Sun May 17, 2009 7:50 pm
by MarauderIIC
Internet says "You are trying to use a non-const iterator on a const reference. Use std::vector<int>::const_iterator it = data.begin();"
And yeah, CodeCriminal had it right.

Oh yeah, and your error "std::_Vector_const_iterator<_Ty,_Alloc>' to 'std::_Vector_iterator" also says to use a const_iterator :)

You could also remove the const keyword from your reference.

Oh, by the way, != is used here instead of < in case the vector shifts in memory (or is empty? I forget, exactly).

Re: Using the vector<type>::iterator With const

Posted: Sun May 17, 2009 8:04 pm
by Maevik
Sweet! So the program is fully functional like this (also added a loop to populate the vector):

Code: Select all

#include <iostream>
#include <vector>

using namespace std;

void display( const vector<int>& data );

int main()
{
	vector<int> nums;
	int counter = 0;

	for( int i = 0 ; i != 20 ; ++i )
	{
		nums.push_back( ( counter * 3 ) );
		counter++;
	}
	display( nums );

}
void display( const vector<int>& data )
{
	for( vector<int>::const_iterator i = data.begin() ; i != data.end() ; ++i )
	{
		cout << *i << endl;
	}
}
Thanks again guys, hope this thread is helpful to someone :D

Re: Using the vector<type>::iterator With const

Posted: Sun May 17, 2009 10:12 pm
by hellknight
i was just wandering, if he wanted to "protect" his data from changing,he could have always passed it by value......right??????..

Re: Using the vector<type>::iterator With const

Posted: Mon May 18, 2009 12:14 am
by MarauderIIC
Yes. And no. Pass by value is slow for containers and classes, since they make a copy, and as a general rule they should be passed by reference/pointer with appropriate const correctness.

Re: Using the vector<type>::iterator With const

Posted: Mon May 18, 2009 3:14 am
by hellknight
MarauderIIC wrote:Yes. And no. Pass by value is slow for containers and classes, since they make a copy, and as a general rule they should be passed by reference/pointer with appropriate const correctness.
i guess u r right, moreover const doesnt hurt ur runtime performence. :D

Re: Using the vector<type>::iterator With const

Posted: Mon May 18, 2009 1:47 pm
by Ginto8
hellknight wrote:i was just wandering, if he wanted to "protect" his data from changing,he could have always passed it by value......right??????..
Or he could make the const vector<type>& a vector<type> const&. This doesn't copy, but it doesn't allow the target to be modified either. Also, this doesn't bring up any problems with const [type]& if you want to pass it a non-const [type]. ;)

Re: Using the vector<type>::iterator With const

Posted: Mon May 18, 2009 6:28 pm
by eatcomics
Maevik wrote:So after Marauder gave me some sound advice, I've been trying to implement the use of iterators into my for loops. However, being the diligent little code monkey I am, I've been trying to use the const keyword wherever I can, and the two concepts are not playing nicely :(
Don't use so many Consts... I think I might do some programming, seeing as how I finished my homework and my parents are gone... They can't tell me to go do something while I'm right in the middle of making an item function...