[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting an iterator from a const vector.

Posted on 2008-06-16
9
Medium Priority
?
318 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 2000 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
Independent Software Vendors: 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] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

872 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