Solved

Getting an iterator from a const vector.

Posted on 2008-06-16
9
271 Views
Last Modified: 2010-04-21
Hello,

Quite a number of questions today! Anyway, why cannot I return an iterator from a const vector? The .begin isn't modifying the vector, so why is this?

Thank you,
Uni
vector<BIT>::iterator myFunc(const vector<BIT> &aVector){
	return aVector.begin();
}

Open in new window

0
Comment
Question by:Unimatrix_001
  • 4
  • 3
  • 2
9 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 21793030
Because the vector is const so the iterator must be to. It's like trying to return a non-const pointer to a const object. If the compiler let you do this you might be able to change the object indirectly.

Code must be kept const correct.
int main()
{
	int const i = 0;
	int * pi = &i; // Error
	int const * cpi = &i; // Works
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21793038
^^^ try returning a vector::const_terator -- that should work fine.
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31467571
Okay thanks rix, I understand better now...
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 40

Expert Comment

by:evilrix
ID: 21793055
>> ^^^ try returning a vector::const_terator -- that should work fine.
NB. The vector has an overloaded begin() function for a const object, that returns const_iterator.
#include <vector>
 
typedef std::vector<int> vec_t;
 
vec_t::const_iterator foo(vec_t const & v)
{
	return v.begin();
}
 
int main()
{
	vec_t v;
	foo(v);
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21793066
evilrix was ahead of me again, but I'll post my reply anyway (because there's an other option - arguably less pretty, but still valid ;) )


>> why cannot I return an iterator from a const vector?

The const is basically a promise not to modify the vector inside the function. However, it also means that the vector is treated as if it were a non-modifiable (const) object.
A normal iterator allows the modification of whatever it points to. Since begin() returns such an iterator, you can't call begin() for a const vector, because the two don't match - ie. a non-const iterator for a const object doesn't make sense - so the compiler doesn't allow it.


You can either const_cast the reference :

        vector<BIT>::iterator myFunc(const vector<BIT> &aVector){
                return const_cast<vector<BIT>&>(aVector).begin();
        }

or you can return a const_iterator :

        vector<BIT>::const_iterator myFunc(const vector<BIT> &aVector){
                return aVector.begin();
        }
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21793073
Hm, that could be more useful/less risky than taking out the const... :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21793083
Hehe, cheers for 2nd view Infinity - it all helps. :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21793102
Note that the reason why the const_cast isn't pretty, is because it would allow you to get a non-const iterator for an ACTUAL const vector. That can cause a world of trouble :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21793130
>> Since begin() returns such an iterator, you can't call begin() for a const vector
rather than calling...

 iterator begin();

you end up calling...

const_iterator begin() const;

http://www.sgi.com/tech/stl/Vector.html
iterator begin()  Container  Returns an iterator pointing to the beginning of the vector.  
const_iterator begin() const  Container  Returns a const_iterator pointing to the beginning of the vector.  

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
multi threaded debug dll option in visual studio 2 109
GUI: DIalog Stacking and Popping in MS C++ 4 76
Unable to start eclipse ? 17 141
How do i run a c++ file? 15 29
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question