473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Constant vector iterators

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
Jul 22 '05 #1
7 2578
Russ Ford wrote:
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
Jul 22 '05 #2
Russ Ford wrote:
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
Jul 22 '05 #3
Pete C. wrote:
Russ Ford wrote:
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


Sorry, ignore, I hit send before I finished :)
Jul 22 '05 #4
Russ Ford wrote:
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).

Jul 22 '05 #5
Pete C. wrote:
Pete C. wrote:
Russ Ford wrote:
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


Sorry, ignore, I hit send before I finished :)


Thanks very much guys.
Jul 22 '05 #6
Russ Ford wrote:

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::ostream_iterator(std::cout));
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7

"Pete Becker" <pe********@acm.org> wrote in message
news:40***************@acm.org...
It's easier to write code that doesn't care:

void printvector(const vector<int>& arr)
{
std::copy(arr.begin(), arr,end(), std::ostream_iterator(std::cout));
std::copy(arr.begin(), arr.end(),
std::ostream_iterator<int>(std::cout,"\n"));
}


Cheers,
Serge
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
9
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
12
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
16
by: xyz | last post by:
I have to run the simulation of a trace file around (7gb contains 116million entries)... presently i am using vector iterators to check the conditions in my program..... it is taking 2 days to...
8
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n),...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.