Solved

Getting an iterator from a const vector.

Posted on 2008-06-16
9
262 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
Comment Utility
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
Comment Utility
^^^ try returning a vector::const_terator -- that should work fine.
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
Comment Utility
Okay thanks rix, I understand better now...
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> ^^^ 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
Hm, that could be more useful/less risky than taking out the const... :)
0
 
LVL 3

Author Comment

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

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
>> 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now