도 용기를 정렬하고 정렬이 실제로 너무 비싸거나 메모리가 부족이라고 가정 : 또는
v1.erase(std::remove_if(v1.begin(), v1.end(),
[&v2](int i) {
return std::find_if(v2.begin(), v2.end(),
[](const std::pair<int, float>& p) {
return p.first == i; })
!= v2.end() }), v1.end());
first
에 일종의 v2
대신 이진 검색을 사용합니다. 메모리가 충분하면 unordered_set
을 사용하여 first
을 v2
으로 정렬합니다.
완전한 C++ 03 버전 :
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
struct find_func {
find_func(int i) : i(i) {}
int i;
bool operator()(const std::pair<int, float>& p) {
return p.first == i;
}
};
struct remove_func {
remove_func(std::vector< std::pair<int, float> >* v2)
: v2(v2) {}
std::vector< std::pair<int, float> >* v2;
bool operator()(int i) {
return std::find_if(v2->begin(), v2->end(), find_func(i)) != v2->end();
}
};
int main()
{
// c++11 here
std::vector<int> v1 = {5, 3, 2, 4, 7, 8};
std::vector< std::pair<int, float> > v2 = {{2,8}, {7,10}, {5,0}, {8,9}};
v1.erase(std::remove_if(v1.begin(), v1.end(), remove_func(&v2)), v1.end());
// and here
for(auto x : v1) {
std::cout << x << std::endl;
}
return 0;
}
출처
2012-01-23 22:49:40
pmr
은 V1의 고유의 항목이 있습니까? – tstenner
벡터 중 하나를 옵션으로 정렬합니까? 아니면 순서대로 있어야합니까? –
@ tstenner : 예 – justik