Iterator STL as return value

I have a class A containing std :: vector, and I would like to provide access to the vector from the outer class A.

The first thing that occurred to me was to make a get function that returns an iterator to a vector, but I will need two iterators (start and end) to go through the vector.

I was wondering if there is a way (technique or patterns) for iterating the whole vector with just one iterator? Or maybe another way to access the vector, of course, not using the vector as the return value :)

+4
source share
5 answers

Why not add the begin () and end () function to the class, but simply return v.begin(); and return v.end(); ? (Assuming v is your vector.)

 class MyVectorWrapper { public: // Give yourself the freedom to change underlying types later on: typedef vector<int>::const_iterator const_iterator; // Since you only want to read, only provide the const versions of begin/end: const_iterator begin() const { return v.begin(); } const_iterator end() const { return v.end(); } private: // the underlying vector: vector<int> v; } 

Then you can iterate through your class, like any other:

 MyVectorWrapper myV; for (MyVectorWrapper::const_iterator iter = myV.begin(); iter != myV.end(); ++i) { // do something with iter: } 
+7
source

The STL introduced the idiom of using two iterators to describe a range. Since then begin() and end() are getters. The advantage of this is that a simple pair of pointers to the C array can be used as an ideal iterator. The disadvantage is that you need to carry two objects with you. C ++ 1x will, AFAIK, introduce the concept of range to make it a bit easier.

+1
source

It seems potentially unreasonable to provide such access to the vector, but if you're not just going to return a pointer or a link to a vector? (Returning the vector itself will be potentially expensive.) Alternatively, return std::pair<> iterators.

An iterator is just an indicator for a single object; for example, a pointer can be a perfectly good random access iterator. It does not carry information about which container the object is in.

+1
source

It seems that what you need to do will be a potential violation of the so-called Law of Demeter . This law is often redundant, but it is often wise to obey it. Although you can provide read-only access by issuing only constant constants, you still risk that your iterators will be invalidated (easily with vectors) if a third party doesn't find out about it. Since you can never predict how your program will evolve over time, it is best to avoid providing features that might be misused.
General motto: make your class interface easy to use and hard to use. The second part is important for overall product quality.

0
source

More details:

 template <typename T,typename A = std::allocator<T>> class yetAnotherVector { public: typedef std::_Vector_iterator<T, A> iterator; protected: std::vector<T,A> mV; public: void add(const T& t) { if(!isExist(t))mV.push_back(t); } bool isExist(const T& t) { std::vector<T,A>::iterator itr; for(itr = mV.begin(); itr != mV.end(); ++itr) { if((*itr) == t) { return true; } } return false; } iterator begin(void){return mV.begin();} iterator end(void){return mV.end();} }; 
0
source

All Articles