需要在结构体中重载'<'运算符,下面是我写的一个例子:
1 #include<iostream> 2 #include<set> 3 using namespace std; 4 struct t_test 5 { 6 int key; 7 string value; 8 t_test() 9 { 10 } 11 t_test(const t_test &s) 12 { 13 key = s.key; 14 value = s.value; 15 } 16 bool operator<(const t_test &src) const 17 { 18 return key < src.key; 19 } 20 }; 21 typedef struct t_test T_test; 22 int main(int argc,char ** argv) 23 { 24 set<t_test> s_p1; 25 t_test str1[2] ; 26 str1[0].key = 1; 27 str1[0].value = "lijin"; 28 str1[1].key = 2; 29 str1[1].value = "duanxiang"; 30 s_p1.insert(str1[0]); 31 s_p1.insert(str1[1]); 32 set<T_test>::iterator it; 33 it = s_p1.begin(); 34 cout<<it->key<<endl; 35 return 0; 36 }