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 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
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
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 :)
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).
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.
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)
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:
...
|
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
|
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;
|
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...
|
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...
|
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...
|
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),...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |