English 中文(简体)
STL iterator as return value
原标题:

I have class A, that contains std::vector and I would like to give an access to the vector from outside class A.

The first thing that came to my mind is to make a get function that returns iterator to the vector, but walk through the vector I will need two iterators (begin and end).

I was wondering is there any way (technique or patters) to iterate whole vector with only one iterator? Or maybe some other way to get access to vector, of course without using vector as return value :)

问题回答

Why not add both a begin() and end() function to your class that 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 could iterate through your class like any other:

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

It seems potentially unwise to give that much access to the vector, but if you re going to why not just return a pointer or reference to the vector? (Returning the vector itself is going to be potentially expensive.) Alternately, return a std::pair<> of iterators.

An iterator is just an indicator for one object; for example, a pointer can be a perfectly good random-access iterator. It doesn t carry information about what sort of container the object is in.

The STL introduced the idiom of using two iterators for describing a range. Since then begin() and end() are the getters. The advantage of this is that a simple pair of pointers into a C array can be used as perfect iterators. The disadvantage is that you need to carry around two objects. C++1x will, AFAIK, introduce a range concept to somewhat ease the latter.

It sounds like what you need to accomplish will be potential violation of the so called Law of Demeter. This law is often an overkill, but oftentimes it is reasonable to obey it. Although you may grant read-only access by giving out only const-iterators you are still at risk that your iterators will get invalidated (easily with vectors) without third party knowing it. As you can never predict how your program will evolve over-time it is better to avoid providing features that can be misused.
Common motto: make your class interface easy to use and hard to misuse. The second part is important for overall product quality.

More:

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();}
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签