Domanda

Is there an efficient way to have a single iterator iterate on the concatenation of 2 objects vector, as if they were one?

The two vectors contain the same data type of course.

UPDATE:

I think I should have put more details about my question and my context. This may answer some of the questions:

In fact I am having one attribute that store the last position of that iterator and inside one method I start to iterate from the last position where I stopped in the previous call, which might be in the first vector or in the second one.

È stato utile?

Soluzione

What about this solution? It may be not elegant, but I guess it respects the standard. right?

vector<Whatever>::iterator it = vectorA.begin();
bool loopOnVectorA = true;

while(true) {

  // My stuff here

  if (loopOnVectorA && it == vectorA.end())
  {
    it = vectorB.begin();
    loopOnVectorA = false;
  }
  else if (it == vectorB.end())
  {
    break;
  }
  else
  {
    varPtrIt++;
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top