需要事先引入的头文件:
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <iterator>
自定义类:
class Loc
{
public:
short c;
short r;
// ...省略没有必要的
// std::less (这样就可以用一些算法了)
bool operator < (const Loc& right)
{
return ((this->c * this->c + this->r * this->r) < (right.c * right.c + right.r * right.r)) ? true : false;
}
};
// 测试时间效率
void test_std_vector_intersection_tm_small()
{
std::vector<Loc> v1 = { Loc(1, 1), Loc(1, 2), Loc(1, 3), Loc(1, 4), Loc(1, 5) };
std::vector<Loc> v2 = { Loc(2, 1), Loc(1, 2), Loc(1, 3), Loc(2, 4), Loc(1, 5) };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<Loc> v_intersection;
TM_INIT;
TM_START;
for (int i = 0; i < 300000; ++i)
{
std::set_intersection(
v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(v_intersection));
v_intersection.clear();
}
TM_END("1");
// 3.0s
TM_START;
for (int i = 0; i < 300000; ++i)
{
calc_intersection_small(v1, v2, v_intersection);
v_intersection.clear();
}
TM_END("2");
// r= 100000 [1]2.0 [2]1.0;
// r= 300000 [1]7.0 [2]4.0;
// r= 300000 [1]7.0 [2]3.0;
// 结论: 自己写的函数,效率高于通用函数
// r= 300000 ([1]加入sort) [1]7.0 [2]3.0;
for (Loc n : v_intersection)
std::cout << "(" << n.c << ' ' << n.r << ") ";
// 5, 7
}
// 测试大量数据
void test_std_vector_intersection_tm_big()
{
std::vector<Loc> v1 = {
{ 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 1, 8 }, { 1, 9 },
{ 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 }, { 2, 7 }, { 2, 8 }, { 2, 9 },
{ 3, 1 }, { 3, 2 }, { 3, 3 }, { 3, 4 }, { 3, 5 }, { 3, 6 }, { 3, 7 }, { 3, 8 }, { 3, 9 },
{ 4, 1 }, { 4, 2 }, { 4, 3 }, { 4, 4 }, { 4, 5 }, { 4, 6 }, { 4, 7 }, { 4, 8 }, { 4, 9 },
{ 5, 1 }, { 5, 2 }, { 5, 3 }, { 5, 4 }, { 5, 5 }, { 5, 6 }, { 5, 7 }, { 5, 8 }, { 5, 9 },
{ 6, 1 }, { 6, 2 }, { 6, 3 }, { 6, 4 }, { 6, 5 }, { 6, 6 }, { 6, 7 }, { 6, 8 }, { 6, 9 },
{ 7, 1 }, { 7, 2 }, { 7, 3 }, { 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 7 }, { 7, 8 }, { 7, 9 },
{ 8, 1 }, { 8, 2 }, { 8, 3 }, { 8, 4 }, { 8, 5 }, { 8, 6 }, { 8, 7 }, { 8, 8 }, { 8, 9 },
{ 9, 1 }, { 9, 2 }, { 9, 3 }, { 9, 4 }, { 9, 5 }, { 9, 6 }, { 9, 7 }, { 9, 8 }, { 9, 9 },
{ 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 },
};
// \ 对角线被修改为 x,0 一共有9个不同
std::vector<Loc> v2 = {
{ 1, 0 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 6 }, { 1, 7 }, { 1, 8 }, { 1, 9 },
{ 2, 1 }, { 2, 0 }, { 2, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 }, { 2, 7 }, { 2, 8 }, { 2, 9 },
{ 3, 1 }, { 3, 2 }, { 3, 0 }, { 3, 4 }, { 3, 5 }, { 3, 6 }, { 3, 7 }, { 3, 8 }, { 3, 9 },
{ 4, 1 }, { 4, 2 }, { 4, 3 }, { 4, 0 }, { 4, 5 }, { 4, 6 }, { 4, 7 }, { 4, 8 }, { 4, 9 },
{ 5, 1 }, { 5, 2 }, { 5, 3 }, { 5, 4 }, { 5, 0 }, { 5, 6 }, { 5, 7 }, { 5, 8 }, { 5, 9 },
{ 6, 1 }, { 6, 2 }, { 6, 3 }, { 6, 4 }, { 6, 5 }, { 6, 0 }, { 6, 7 }, { 6, 8 }, { 6, 9 },
{ 7, 1 }, { 7, 2 }, { 7, 3 }, { 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 0 }, { 7, 8 }, { 7, 9 },
{ 8, 1 }, { 8, 2 }, { 8, 3 }, { 8, 4 }, { 8, 5 }, { 8, 6 }, { 8, 7 }, { 8, 0 }, { 8, 9 },
{ 9, 1 }, { 9, 2 }, { 9, 3 }, { 9, 4 }, { 9, 5 }, { 9, 6 }, { 9, 7 }, { 9, 8 }, { 9, 0 },
{ 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 },
};
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<Loc> v_intersection;
TM_INIT;
TM_START;
for (int i = 0; i < 100000; ++i)
{
std::set_intersection(
v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(v_intersection));
v_intersection.clear();
}
TM_END("1");
// 3.0s
TM_START;
for (int i = 0; i < 100000; ++i)
{
calc_intersection_small(v1, v2, v_intersection);
v_intersection.clear();
}
TM_END("2");
TM_START;
for (int i = 0; i < 100000; ++i)
{
calc_intersection_big(v1, v2, v_intersection);
v_intersection.clear();
}
TM_END("3");
// r= 100000 [1]21.0 [2]14.0 [3]63.0
// 结论: 自己写的函数,效率高于通用函数
// r= 300000 ([1]加入sort) [1]x.0 [2]x.0;
for (Loc n : v_intersection)
std::cout << "(" << n.c << ' ' << n.r << ") ";
}
一些自己实现的函数:
void calc_intersection_small(const std::vector<Loc>& v1, const std::vector<Loc>& v2, std::vector<Loc>& v_out)
{
bool isFind = false;
for (auto it1 = v1.begin(); it1 != v1.end(); ++it1)
{
for (auto it2 = v2.begin(); it2 != v2.end(); ++it2)
{
if ((*it1) == (*it2))
{
isFind = true;
v_out.push_back(*it1);
}
if (isFind)
break;
}
if (isFind)
{
continue;
}
}
}
void calc_intersection_big(const std::vector<Loc>& v1, const std::vector<Loc>& v2, std::vector<Loc>& v_out)
{
std::vector<Loc> vv1;
std::vector<Loc> vv2;
vv1.insert(vv1.begin(), v1.begin(), v1.end());
vv2.insert(vv2.begin(), v2.begin(), v2.end());
bool isFind = false;
for (auto it1 = vv1.begin(); it1 != vv1.end(); )
{
for (auto it2 = vv2.begin(); it2 != vv2.end(); )
{
if ((*it1) == (*it2))
{
isFind = true;
v_out.push_back(*it1);
}
else
++it2;
if (isFind)
{
// it2 = vv2.erase(it2);
break;
}
}
if (isFind)
{
it1 = vv1.erase(it1);
continue;
}
else
++it1;
}
}