Maps插入成对的key/value.
//
stl/mmap1.cpp
/**/
/* multimap 允许重复的key键值,如下输出 this is multimap of strings tagged map中存放 pair,可以用 make_pair函数生成*/
#include
<
iostream
>
#include
<
map
>
#include
<
string
>
using
namespace
std;
int
main()
...
{ typedef multimap < int , string > IntStringMMap; IntStringMMap coll; coll.insert(make_pair( 5 , " tagged " )); coll.insert(make_pair( 2 , " a " )); coll.insert(make_pair( 1 , " this " )); coll.insert(make_pair( 1 , " is " )); coll.insert(make_pair( 4 , " of " )); coll.insert(make_pair( 6 , " strings " )); coll.insert(make_pair( 3 , " multimap " )); IntStringMMap::iterator pos; for (pos = coll.begin();pos != coll.end(); ++ pos) ... { cout << pos -> second << ' ' ; } cout << endl; return 0 ; }
//
STL/map1.cpp
/**/
/* map 不允许重复键值*/
#define
WIN32_LEAN_AND_MEAN
#include
<
iostream
>
#include
<
map
>
#include
<
string
>
using
namespace
std;
int
main()
...
{ typedef map < string , double > StringFloatMap; StringFloatMap coll; coll[ " VAT " ] = 0.15 ; // 做下面的操作会出错 // coll["VAT"] =0.15; coll[ " PI " ] = 3.1415 ; coll[ " an arbitrary number " ] = 4983.223 ; coll[ " NULL " ] = 0 ; StringFloatMap::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++ pos) ... { cout << " key:" " << pos -> first << " " " << " value: " << pos -> second << endl; } return 0 ; }
/**/
/* 程序输出情况: key:"NULL" value:0 key:"PI" value:3.1415 key:"VAT" value:0.15 key:"an arbitrary number" value:4983.22 Press any key to continue*/
简单的stl function object 例子
//
stl
//
algo1.cpp
/**/
/* * using stl function object * min_element(), * max_element(), * find() * reverse()*/
#define
WIN32_LEAN_AND_MEAN
#include
<
iostream
>
#include
<
vector
>
#include
<
algorithm
>
using
namespace
std;
int
main()
...
{ vector < int > coll; vector < int > ::iterator pos; coll.push_back( 2 ); coll.push_back( 5 ); coll.push_back( 4 ); coll.push_back( 1 ); coll.push_back( 6 ); coll.push_back( 3 ); // min_element function pos = min_element(coll.begin(),coll.end()); cout << " min: " <<* pos << endl; // max_element function pos = max_element(coll.begin(),coll.end()); cout << " max: " <<* pos << endl; // sort all the element sort(coll.begin(),coll.end()); // find the first element with value 3 cout << " 原始字符串: " << endl;; for (pos = coll.begin();pos != coll.end(); ++ pos) cout <<* pos << ' ' ; cout << endl; // 从 查找到的pos到coll 的end进行反转 pos = find(coll.begin(),coll.end(), 3 ); reverse(pos,coll.end()); cout << " 从pos到end经过reverse后的字符串: " << endl;; for (pos = coll.begin();pos != coll.end(); ++ pos) cout <<* pos << ' ' ; cout << endl; }
stl的处理区间情况设计为半开,下面是个小型例子,帮助理解。
/**/
/* stl 设计的,要处理的情况是个半开区间*/
//
stl/find1.cpp
#include
<
iostream
>
#include
<
list
>
#include
<
algorithm
>
using
namespace
std;
int
main()
...
{ list < int > coll; list < int > ::iterator pos; for ( int i = 20 ; i <= 40 ; ++ i) coll.push_back(i); pos = find(coll.begin(),coll.end(), 3 ); // 搜索不到,所以返回的位置为40所在位置的下一个部位,即coll.end() reverse(pos,coll.end()); // 相当于 reverse(coll.end(),coll.end());什么功能也不做 list< int > ::iterator pos25,pos35; pos25 = find(coll.begin(),coll.end(), 25 ); pos35 = find(coll.begin(),coll.end(), 35 ); /**/ /* 进行区间操作的时候,实际上是个半开区间,数学上表示是[a,b),即最后一位不计入操作范围,所以下面第一个 树簇的时 [25,34] 中的最大值,即34; */ cout << " max: " <<* max_element(pos25,pos35) << endl; // [25,35] 中最大的 cout << " max: " <<* max_element(pos25, ++ pos35) << endl; }
/**/
/* OutPut Result: max: 34 max: 35*/