Constant vector iterators

R

Russ Ford

How does one define an iterator for a constant vector?

ie:
void printvector (vector<int>& arr)
{
vector<int>::iterator i;
for (i=arr.begin(); i<arr.end(); ++i)
cout << *i << endl;
}

works fine, but if I change the prototype to
void printvector (const vector<int>& arr)
I get a compile error on the for line about trying to convert a const int*
const to an int*.

How do I specify that it's the vector that's constant and not the iterator
itself?

Much appreciated.

Russ
 
P

Pete C.

Russ said:
How does one define an iterator for a constant vector?

ie:
void printvector (vector<int>& arr)
{
vector<int>::iterator i;
for (i=arr.begin(); i<arr.end(); ++i)
cout << *i << endl;
}

works fine, but if I change the prototype to
void printvector (const vector<int>& arr)
I get a compile error on the for line about trying to convert a const
int* const to an int*.

How do I specify that it's the vector that's constant and not the
iterator itself?

Much appreciated.

Russ

vector<T>::const::iterator

- Pete
 
P

Pete C.

Russ said:
How does one define an iterator for a constant vector?

ie:
void printvector (vector<int>& arr)
{
vector<int>::iterator i;
for (i=arr.begin(); i<arr.end(); ++i)
cout << *i << endl;
}

works fine, but if I change the prototype to
void printvector (const vector<int>& arr)
I get a compile error on the for line about trying to convert a const
int* const to an int*.

How do I specify that it's the vector that's constant and not the
iterator itself?

Much appreciated.

Russ

std::vector<T>::const_iterator

- Pete
 
R

Rolf Magnus

Russ said:
How does one define an iterator for a constant vector?

Use a const_iterator.
ie:
void printvector (vector<int>& arr)
{
vector<int>::iterator i;
for (i=arr.begin(); i<arr.end(); ++i)
cout << *i << endl;
}

works fine, but if I change the prototype to
void printvector (const vector<int>& arr)
I get a compile error on the for line about trying to convert a const
int* const to an int*.

void printvector (const vector<int>& arr)
{
vector<int>::const_iterator i;
for (i=arr.begin(); i<arr.end(); ++i)
cout << *i << endl;
}
How do I specify that it's the vector that's constant and not the
iterator itself?

When defining the iterator, you don't describe whether the vector you
use it with is const or not. You describe whether you want to use the
iterator for writing (iterator) or for reading (const_iterator).
 
P

Pete Becker

Russ said:
How does one define an iterator for a constant vector?

ie:
void printvector (vector<int>& arr)
{
vector<int>::iterator i;
for (i=arr.begin(); i<arr.end(); ++i)
cout << *i << endl;
}

works fine, but if I change the prototype to
void printvector (const vector<int>& arr)
I get a compile error on the for line about trying to convert a const int*
const to an int*.

How do I specify that it's the vector that's constant and not the iterator
itself?

It's easier to write code that doesn't care:

void printvector(const vector<int>& arr)
{
std::copy(arr.begin(), arr,end(), std::eek:stream_iterator(std::cout));
}
 
S

Sergiy Kanilo

It's easier to write code that doesn't care:

void printvector(const vector<int>& arr)
{
std::copy(arr.begin(), arr,end(), std::eek:stream_iterator(std::cout));

std::copy(arr.begin(), arr.end(),
std::ostream_iterator said:

Cheers,
Serge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top