So... I've slept since college - meaning I've forgotten a few things. I need to do an inner-join on two large-ish sorted vectors. Thought about converting them to unsorted sets and walking through "find-ing", but I'm pretty sure this is the fastest way to do it - and still accurately. Still, wanted to check with DaniWeb:
#include <iostream>
#include <vector>
int main(){
std::vector<int>
aList={0,2,3,4,6,7,8,10,11,12},
bList={0,1,3,4,5,6,8,9,11},
cList={};
//Begin matching algorithm
for(
//Create iterators
auto aIter=aList.begin(),bIter=bList.begin();
//Break on either iterator reaching the end
(aIter != aList.end() && bIter != bList.end());
//Increment aIter before looping
aIter++
){
//Skip any elements on bList less than current aIter
while((*bIter)<(*aIter)){
bIter++;
}
//If there's a match, push it onto cList
if((*bIter)==(*aIter)){
cList.push_back(*bIter);
}
}
//End matching algorithm
for(auto c:cList){
std::cout<<c<<std::endl;
}
return 0;
}
This should run O(n).