最近项目遇到一个问题,有关stl vector自定义类型的去重问题。
背景:1、在一个vector中,存在大量元素拥有同一属性,而其他属性我们不关心,为了减少数据包大小,需要去重
2、此自定义类型不能去重载==操作符(公司代码规范等原因)
3、正常情况下,vector中元素是有序的(拥有同一属性的元素排在一起) /*引起误解,之后补充*/
于是,花了十分钟撸出了下列代码原型。
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct test 8 { 9 int key; 10 int value; 11 }; 12 13 class finder 14 { 15 public: 16 explicit finder(const test& _t):t(_t){} 17 const bool operator()(const test& __t)const{return t.key==__t.key;} 18 private: 19 test t; 20 }; 21 22 int main(void) 23 { 24 vector<test> vTest; 25 test t1 = {1,10}; 26 test t2 = {1,11}; 27 test t3 = {2,8}; 28 test t4 = {2,9}; 29 test t5 = {1,6}; 30 vTest.push_back(t1); 31 vTest.push_back(t2); 32 vTest.push_back(t3); 33 vTest.push_back(t4); 34 vTest.push_back(t5); 35 36 for(int index=0; index!=vTest.size(); ++index) 37 { 38 vTest.erase(find_if(vTest.begin(),vTest.end(),finder(vTest[index]))); 39 if(index == vTest.size()) 40 break; 41 } 42 43 for(int index=0; index!=vTest.size(); ++index) 44 cout<<vTest[index].key<<'\t'<<vTest[index].value<<endl; 45 46 return 0; 47 }
结果:
这里利用了一下仿函数(functor)来构造find_if的条件,达到查找具有相同属性的自定义类型对象。
if里的判断是为了防止segment fault,erase在删除最后一个元素的时候,会给你带来惊喜-_-(详情请查看stl吧)。
目的达到了,但是总感觉循环太浪费CPU太低效太罪恶了,各位看官有没有更好的办法呢?求解。
================================================================
有小伙伴说用unique,好吧,其实之前也想到了,貌似效率没啥提升,代码还增加了。
贴代码(所有):
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct test 8 { 9 int key; 10 int value; 11 }; 12 13 class finder 14 { 15 public: 16 explicit finder(const test& _t):t(_t){} 17 const bool operator()(const test& __t)const{return t.key==__t.key;} 18 private: 19 test t; 20 }; 21 const bool unique_finder(const test& first, const test& second) 22 { 23 return first.key == second.key; 24 } 25 const bool sort_finder(const test& first, const test& second) 26 { 27 return first.key < second.key; 28 } 29 30 int main(void) 31 { 32 vector<test> vTest; 33 test t1 = {1,10}; 34 test t2 = {1,11}; 35 test t3 = {2,8}; 36 test t4 = {2,9}; 37 test t5 = {1,6}; 38 vTest.push_back(t1); 39 vTest.push_back(t2); 40 vTest.push_back(t3); 41 vTest.push_back(t4); 42 vTest.push_back(t5); 43 44 /* 45 for(int index=0; index!=vTest.size(); ++index) 46 { 47 vTest.erase(find_if(vTest.begin(),vTest.end(),finder(vTest[index]))); 48 if(index == vTest.size()) 49 break; 50 } 51 */ 52 53 cout<<"=================origin===================="<<endl; 54 for(int index=0; index!=vTest.size(); ++index) 55 cout<<vTest[index].key<<'\t'<<vTest[index].value<<endl; 56 57 cout<<"=================sort===================="<<endl; 58 sort(vTest.begin(),vTest.end(),sort_finder); 59 for(int index=0; index!=vTest.size(); ++index) 60 cout<<vTest[index].key<<'\t'<<vTest[index].value<<endl; 61 62 /* 63 cout<<"=================stable_sort===================="<<endl; 64 stable_sort(vTest.begin(),vTest.end(),sort_finder); 65 for(int index=0; index!=vTest.size(); ++index) 66 cout<<vTest[index].key<<'\t'<<vTest[index].value<<endl; 67 */ 68 69 cout<<"==================unique==================="<<endl; 70 vTest.erase(unique(vTest.begin(),vTest.end(),unique_finder),vTest.end()); 71 72 for(int index=0; index!=vTest.size(); ++index) 73 cout<<vTest[index].key<<'\t'<<vTest[index].value<<endl; 74 75 return 0; 76 }
结果:
嘿嘿,不错,如果大家还有更好的办法,可以提出来,谢谢!