set :The template class describes an object that controls a varying-length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element with a number of operations proportional to the logarithm of the number of elements in the sequence (logarithmic time). Moreover, inserting an element invalidates no iterators, and removing an element invalidates only those iterators that point at the removed element.thus u can not put that type into set object in order to make an order.
i suggest u'd better to use another container to contain ur elements of that type instead of set,just like linklist or etc.
i suggest u'd better to use another container to contain ur elements of that type instead of set,just like linklist or etc.
#include <iostream>using namesapce std;int main()
{
set<type_kind> P;
P.insert(XXX);
P.find(XXXXX);
.....
return 0;
}
重载运算符啊.class CPos
{
char q;
int x; //键值
char p;
int m;
public:
bool operator<(CPos pos); //重载运算符"<"
bool operator>(CPos pos); //重载运算符">"
}
#include <iostream>int main( )
{
using namespace std;
set <int, less<int> > s1;
set<int, less<int> >::key_compare kc1 = s1.key_comp( ) ;
bool result1 = kc1( 2, 3 ) ;
if( result1 == true )
{
cout << "kc1( 2,3 ) returns value of true, "
<< "where kc1 is the function object of s1."
<< endl;
}
else
{
cout << "kc1( 2,3 ) returns value of false "
<< "where kc1 is the function object of s1."
<< endl;
} set <int, greater<int> > s2;
set<int, greater<int> >::key_compare kc2 = s2.key_comp( ) ;
bool result2 = kc2( 2, 3 ) ;
if(result2 == true)
{
cout << "kc2( 2,3 ) returns value of true, "
<< "where kc2 is the function object of s2."
<< endl;
}
else
{
cout << "kc2( 2,3 ) returns value of false, "
<< "where kc2 is the function object of s2."
<< endl;
}
}set可以控制数据自动按照某一种顺序排序的
{
char q;
int x; //键值
char p;
int m;
};class CCmp
{
public:
bool operator(const CPos &pos1,const CPos &pos2)
{
return pos1.x<pos2.x;
}
}set <CPos,CCmp> p;
#include<iostream>
using namespace std;
struct
{
int id;
int score;
string name;
};
struct compare
{
bool operator()(const Entity& e1,const Entity& e2)const {
if(e1.score<e2.score) return true;
else
if(e1.score==e2.score)
if(e1.id<e2.id) return true; return false;
}
};int main()
{
set<Entity,compare>s_test;
Entity a,b,c;
a.id=123;a.score=90;a.name="bill";
b.id=121;b.score=85;b.name="mary";
c.id=130;c.score=85;c.name="jerry";
s_test.insert(a);s_test.insert(b);s_test.insert(c);
set<Entity,compare>::iterator itr;
cout<<"Score List(ordered by score):\n";
for(itr=s_test.begin();itr!=s_test.end();itr++)
cout<<itr->id<<"---"<<itr->name<<"---"<<itr->score<<endl;
return 0;
}
建议有std::vector