Q:

Accessing members in a vector of objects as vectors

I have a c++ class that is contained as a vector of objects in another class. 
class Small
{

public:

   int a;
   int b;    
   int c;
};

class Big 
{
public:

   vector< Small > getSmallVec() const { return m_smallVec;}

private:

   vector< Small > m_smallVec;    
};

I'd like to be able to access the members of the Small class as a vector themselves. In other words, I'd like to have a function in Big like
vector<int> getSmallVec_a()

which would return a vector that contains each value of int Small::a in m_smallVec. The only way I can think of doing it would be to iterate over m_smallVec and copy to a vector of int. Is there a way to do this without copying?

A:

If this is how you expect to access data and performance is important, have you considered using parallel vectors? Instead of 
  std::vector<Small>

In "Big" you actually maintain 3 vectors
  std::vector<int> aVec;
  std::vector<int> bVec;
  std::vector<int> cVec;

All elements at 0 correspond to the 0'th "Small", and you can easily seperate out all the as from the bs.
This comes a lot of annoying bookkeeping that you'd be have to be sure to get right and encapsulate well. Its not a more elegant solution, I usually dislike this kind of thing and would probably question in a code review, but it would probably give you the best performance for the operations you've described.

