Solved

Getting an iterator from a const vector.

Posted on 2008-06-16
9
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

691 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