This method seems strange to me .
The first thing that is obviously wrong is these lines:
while((*bIter)<(*aIter)){
bIter++;
}
What happens if bIter is at the last position in the array.
So in short I would have written something like this
std::vector<int>::const_iterator aIter=aList.begin();
std::vector<int>::const_iterator bIter=bList.begin();
if (bIter==bList.end()) return 0;
for(;aIter!=aList.end();aIter++)
{
while(*bIter < *aIter)
{
if (++bIter==bList.end()) break;
}
if (*bIter == *aIter)
cList.push_back(*bIter);
}
Finally there is always the standard algorithms and you can do this:
#include <algorithm>
//.....
std::set_intersection(aList.begin(), aList.end(),
bList.begin(), bList.end(),
std::back_inserter(cList));
I am not 100% certain of its speed but is it quick and easy to write and normally the stl is quick enough relative to simple hand coded stuff.
Notes:
I am very disinclinded to use auto
when the algorithm only works for int and not doubles or other number types.
Second many may not like the break in the middle of the loop -- you can put it outside if you like.