64,662
社区成员
发帖
与我相关
我的任务
分享
// set_difference example
#include <iostream> // std::cout
#include <algorithm> // std::set_difference, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it;
std::sort (first,first+5); // 5 10 15 20 25
std::sort (second,second+5); // 10 20 30 40 50
it=std::set_difference (first, first+5, second, second+5, v.begin());
// 5 15 25 0 0 0 0 0 0 0
v.resize(it-v.begin()); // 5 15 25
std::cout << "The difference has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
//The difference has 3 elements:
//5 15 25
#include <algorithm>
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> A {0,1,2,3,4,5,6,7,8,9};
std::vector<int>const B {3,5,8};
std::cout << "before removal" << std::endl;
for (auto const a : A) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : B) { std::cout << b << " "; } std::cout << std::endl;
struct not_in
{
std::vector<int>const& B;
not_in (std::vector<int>const& B) : B(B) { }
bool operator () (int const a) const
{
return B.end() != std::find(B.begin(),B.end(),a);
}
};
A.erase(std::remove_if(A.begin(),A.end(),not_in(B)),A.end());
std::cout << "after removal" << std::endl;
for (auto const a : A) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : B) { std::cout << b << " "; } std::cout << std::endl;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
int data1[] = {1,2,11,12,3,12,11,4,5,11,6,7,8,9};
int data2[] = {11,12};
//根据数组初始化2个容器
vector<int> v1(sizeof(data1) / sizeof(int));
vector<int> v2(sizeof(data2) / sizeof(int));
for (int i = 0; i < sizeof(data1) / sizeof(int); i++)
v1[i] = data1[i];
for (int i = 0; i < sizeof(data2) / sizeof(int); i++)
v2[i] = data2[i];
typedef vector<int>::iterator vit; //定义迭代器
vit it1 = v1.end(); //设置在v1搜索的区间,从 begin 到 it1
for (vit it = v2.begin(); it < v2.end(); ++it)
{
vit it2 = find(v1.begin(),it1, *it); //对v2中的每个值在v1中进行查找
if (it2 != it1)
{
it1 = remove(v1.begin(), it1, *it); //找到执行remove
}
}
v1.erase(it1, v1.end()); //remove并不会删除元素,最后erase删除
copy(v1.begin(), v1.end(), ostream_iterator<int, char>(cout, " "));
cout << endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main ()
{
std::vector<int> A {0,1,2,3,4,5,6,7,8,9};
std::vector<int> B{3,5,8};
std::cout << "before removal" << std::endl;
for (auto const a : A) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : B) { std::cout << b << " "; } std::cout << std::endl;
std::sort(A.begin(),A.end());
std::sort(B.begin(),B.end());
std::vector<int> C;
std::set_difference(A.begin(),A.end(),
B.begin(),B.end(),
std::back_inserter(C));
A = std::move(C);
std::cout << "after removal" << std::endl;
for (auto const a : A) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : B) { std::cout << b << " "; } std::cout << std::endl;
}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main ()
{
std::vector<int> a {0,1,2,3,4,5,6,7,8,9};
std::vector<int> b {3,5,8};
std::cout << "before removal" << std::endl;
for (auto const a : a) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : b) { std::cout << b << " "; } std::cout << std::endl;
std::sort(a.begin(),a.end());
std::sort(b.begin(),b.end());
std::vector<int> c;
std::set_difference(a.begin(),a.end(),
b.begin(),b.end(),
std::back_inserter(c));
a = std::move(c);
std::cout << "after removal" << std::endl;
for (auto const a : a) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : b) { std::cout << b << " "; } std::cout << std::endl;
}
#include <algorithm>
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> A {0,1,2,3,4,5,6,7,8,9};
std::vector<int>const B{3,5,8};
std::cout << "before removal" << std::endl;
for (auto const a : A) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : B) { std::cout << b << " "; } std::cout << std::endl;
A.erase(std::remove_if(A.begin(),A.end(),[&](int const a)
{
return B.end() != std::find(B.begin(),B.end(),a);
}),A.end());
std::cout << "after removal" << std::endl;
for (auto const a : A) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : B) { std::cout << b << " "; } std::cout << std::endl;
}
but a faster solution would be
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main ()
{
std::vector<int> a {0,1,2,3,4,5,6,7,8,9};
std::vector<int> b{3,5,8};
std::cout << "before removal" << std::endl;
for (auto const a : a) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : b) { std::cout << b << " "; } std::cout << std::endl;
std::sort(a.begin(),a.end());
std::sort(b.begin(),b.end());
std::vector<int> c;
std::set_difference(a.begin(),a.end(),
b.begin(),b.end(),
std::back_inserter(c));
a = std::move(c);
std::cout << "after removal" << std::endl;
for (auto const a : a) { std::cout << a << " "; } std::cout << std::endl;
for (auto const b : b) { std::cout << b << " "; } std::cout << std::endl;
}