If I have a vector which has many duplicate elements,then I construct a set from this vector .
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main ()
{
vector<pair<int,int> > myvec={{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}};
auto comp=[](pair<int,int> a,pair<int,int> b){return a.first<b.first;};
set<pair<int,int>, decltype(comp)> myset(myvec.begin(),myvec.end(),comp);
for (auto p:myset)
cout<<p.second<<endl;
return 0;
}
My question is, is it guaranteed that these elements will be insert to the set by the order of the vector ,like :
for (auto p:myvec)
myset.insert(p);
for (auto p : myvec)should befor (const auto& p : myvec)in this case, for betterment.setfrom an iterator range is defined in a way that answers your question? Secondly,setorders its elements anyway, so the order shouldn't make a difference. So why do you care?set, it's always ordered using your comparison function. And yes, like Ulrich Eckhardt points out, this can be found in documentation.std::multiset.