map中不允许两个相同的key存在,如果已经存在key1了,再插入key1将会失败,不管关联的value是否相同。multimap允许两个相同的key存在。对同一个key或是不同的key,二者都不管value是否相同。
同样的,set中不允许有相同的元素存在,multiset则允许。set和multiset都是有序的,插入新元素后会自动排序,默认是升序排列,也可以指定一个排序函数对象。
int multimap_test()
{
multimap<string, int> m;
m.insert(pair<string, int>("Jack", 1)); //����
m.insert(pair<string, int>("Jack", 2));
m.insert(pair<string, int>("Body", 1));
m.insert(pair<string, int>("Navy", 4));
m.insert(pair<string, int>("Demo", 3));
m.insert(pair<string, int>("Jack", 2));
multimap<string, int>::iterator iter;
for (iter = m.begin(); iter != m.end(); ++iter) //����
{
cout << (*iter).first << " " << (*iter).second << endl;
}
m.erase("Navy");
cout << "The element after delete:" << endl;
for (iter = m.begin(); iter != m.end(); ++iter)
{
cout << (*iter).first << " " << (*iter).second << endl;
}
int num = m.count("Jack");
iter = m.find("Jack");
int i;
cout << "the search result is:" << endl;
for (i = 1; i <= num; i++)
{
cout << (*iter).first << " " << (*iter).second << endl;
iter++;
}
if (i == 1)
{
cout << "can not find!" << endl;
}
return 0;
}
void set_test1()
{
set<int> set1;
for (int i = 0; i < 10; ++i)
{
set1.insert(i);
}
for (set<int>::iterator p = set1.begin(); p != set1.end(); ++p)
{
cout << *p << " ";
}
if (set1.insert(3).second) //把3插入到set1中
//插入成功则set1.insert(3).second返回1,否则返回0
//此例中,集中已经有3这个元素了,所以插入将失败
cout << "set insert success";
else
cout << "set insert failed";
int a[] = { 4, 1, 1, 1, 1, 1, 0, 5, 1, 0 };
multiset<int> A;
A.insert(set1.begin(), set1.end());
A.insert(a, a + 10);
cout << endl;
for (multiset<int>::iterator p = A.begin(); p != A.end(); ++p)
cout << *p << " ";
}
void set_test2()
{
struct MyCmp
{
bool operator ()(const string s1, const string s2)
{
return s1 > s2;
}
};
typedef set<string, MyCmp> TMySet;
TMySet stSet1;
stSet1.insert(string("sfdsfd"));
stSet1.insert(string("apple"));
stSet1.insert(string("english"));
stSet1.insert(string("dstd"));
cout << endl << "s1:" << endl;
TMySet::iterator it = stSet1.begin();
while (it != stSet1.end())
cout << *it++ << " ";
TMySet stSet2;
stSet2.insert(string("abc"));
stSet2.insert(string("apple"));
stSet2.insert(string("english"));
cout << endl << "s2:" << endl;
it = stSet2.begin();
while (it != stSet2.end())
cout << *it++ << " ";
cout << endl << endl;
string str[10];
string *end = set_intersection(stSet1.begin(), stSet1.end(), stSet2.begin(), stSet2.end(), str, MyCmp());//求交集,返回值指向str最后一个元素的尾端
cout<<"result of set_intersection s1,s2:"<<endl;
string *first = str;
while (first < end)
cout << *first++ << " ";
cout << endl << endl << "result of set_union of s1,s2" << endl;
end = set_union(stSet1.begin(), stSet1.end(), stSet2.begin(), stSet2.end(), str, MyCmp()); //并集
first = str;
while (first < end)
cout << *first++ << " ";
cout << endl << endl << "result of set_difference of s2 relative to s1" << endl;
// set_difference,该函数用于求两个集合(广义的集合,数组、vector等等都是的)的差集,结果集合中包含所有属于第一个集合但不属于第二个集合的元素
first = str;
end = set_difference(stSet1.begin(), stSet1.end(), stSet2.begin(), stSet2.end(), str, MyCmp());//s2相对于s1的差集
while(first<end)
cout << *first++ << " ";
cout << endl << endl << "result of set_difference of s1 relative to s2" << endl;
first = str;
end = set_difference(stSet2.begin(), stSet2.end(), stSet1.begin(), stSet1.end(), str, MyCmp());//s1相对于s2的差集
while (first < end)
cout << *first++ << " ";
cout << endl << endl;
first = str;
end = set_symmetric_difference(stSet1.begin(), stSet1.end(), stSet2.begin(), stSet2.end(), str, MyCmp());//上面两个差集的并集 while(first<end)
cout << *first++ << " ";
cout << endl;
}