STL源码剖析-关联式容器之set,map,multiset和multimap

来源:互联网 发布:苹果数据传输软件 编辑:程序博客网 时间:2024/05/19 00:11

一.set

1.set的特性是所有元素都会根据元素的键值自动排序,set元素的键值就是实值,实值就是键值.

2.不能通过set的迭代器改变set的元素,set iterator是一种const iterator.

3.客户端对set进行元素新增或者删除操作时,操作之前的所有迭代器在操作后都依然有效,被删除的元素的迭代器例外.

4.STL set以RB-tree为底层机制,set的操作几乎都是转调用RB-tree的函数而已.

5.测试例子:

    #include<set>      #include<iostream>      using namespace std;      int main(){          int ia[] = { 5, 3, 4, 1, 6, 2 };          set<int> iset(begin(ia), end(ia));          cout << "size=" << iset.size() << endl; //size=6          cout << "3 count=" << iset.count(3) << endl;//3 count=1          iset.insert(3);          cout << "size=" << iset.size() << endl;//size=6          cout << "3 count=" << iset.count(3) << endl;//3 count=1          iset.insert(7);          cout << "size=" << iset.size() << endl;//size=7          cout << "3 count=" << iset.count(3) << endl;//3 count=1          iset.erase(1);          cout << "size=" << iset.size() << endl;//size=6          cout << "1 count=" << iset.count(1) << endl;//1 count=0          set<int>::iterator it;          for (it = iset.begin(); it != iset.end(); ++it)              cout << *it << " "; //2 3 4 5 6 7          cout << endl;          it = find(iset.begin(), iset.end(), 3);          if (it != iset.end())              cout << "3 found" << endl;//3 found          it = find(iset.begin(), iset.end(), 1);          if (it == iset.end())              cout << "1 not found" << endl;//1 not found          system("pause");          return 0;      }  

二.map

1.map的特性是所有元素都会根据元素的键值自动排序,map的所有元素都是pair,pair的第一元素是键值,第二元素是实值.

2.不能通过map的迭代器改变map的键值,但通过map的迭代器能改变map的实值.因此map的迭代器既不是一种const iterator,也不是一种mutable iterator.

3.客户端对map进行元素新增或者删除操作时,操作之前的所有迭代器在操作后都依然有效,被删除的元素的迭代器除外.

4.STL map以RB-tree为底层机制,map的操作几乎都是转调用RB-tree的函数而已

5.测试例子

    #include<map>      #include<string>      #include<iostream>      using namespace std;      int main(){          map<string, int> mp;          mp["Jack"] = 1;          mp["John"] = 2;          mp["Lily"] = 3;          mp["Kate"] = 4;                                                              //按键值字典序排序          pair<string, int> value("Tom", 5);                  //Jack 1          mp.insert(value);                                   //John 2          map<string, int>::iterator it;                      //Kate 4          for (it = mp.begin(); it != mp.end(); ++it)         //Lily 3              cout << it->first << " " << it->second << endl; //Tom 5          cout << mp["Kate"] << endl;                   //4                it = mp.find("John");          if (it != mp.end())              cout << "John found" << endl;//John found          it->second = 8;          cout << mp["John"] << endl;//8          system("pause");          return 0;      }  

三,multiset

multiset的特性及用法和set完全相同,唯一的差别在于它运行键值重复,因为它的插入操作采用的是RB-tree的insert_equal().

    #include<set>      #include<iostream>      using namespace std;      int main(){          int ia[] = { 5, 3, 4, 1, 6, 2 };          multiset<int> iset(begin(ia), end(ia));          cout << "size=" << iset.size() << endl; //size=6          cout << "3 count=" << iset.count(3) << endl;//3 count=1          iset.insert(3); //和set区别的地方          cout << "size=" << iset.size() << endl;//size=7          cout << "3 count=" << iset.count(3) << endl;//3 count=2          iset.insert(7);          cout << "size=" << iset.size() << endl;//size=8          cout << "3 count=" << iset.count(3) << endl;//3 count=2          iset.erase(1);          cout << "size=" << iset.size() << endl;//size=7          cout << "1 count=" << iset.count(1) << endl;//1 count=0          set<int>::iterator it;          for (it = iset.begin(); it != iset.end(); ++it)              cout << *it << " "; //2 3 3 4 5 6 7          cout << endl;          it = find(iset.begin(), iset.end(), 3);          if (it != iset.end())              cout << "3 found" << endl;//3 found          it = find(iset.begin(), iset.end(), 1);          if (it == iset.end())              cout << "1 not found" << endl;//1 not found          system("pause");          return 0;      }  

四.multimap

multimap的特性及用法和map完全相同,唯一的差别在于它运行键值重复,因为它的插入操作采用的是RB-tree的insert_equal();

    #include<map>      #include<string>      #include<iostream>      using namespace std;      int main(){          multimap<string, int> mp;//multimap没有下标操作          mp.insert(pair<string, int>("Jack", 1));          mp.insert(pair<string, int>("John", 2));          mp.insert(pair<string, int>("Lily", 3));          mp.insert(pair<string, int>("Kate", 4));//按键值字典序排序          mp.insert(pair<string, int>("Tom", 5));              //Jack 1          mp.insert(pair<string, int>("John", 8));            //John 2                                                                 //John 8             map<string, int>::iterator it;                      //Kate 4          for (it = mp.begin(); it != mp.end(); ++it)         //Lily 3              cout << it->first << " " << it->second << endl; //Tom 5          it = mp.find("John");          if (it != mp.end())              cout << "John found" << endl; //John found          it->second = 8;          it = mp.find("John");          cout << it->second << endl;//8          system("pause");          return 0;      }  
0 0
原创粉丝点击