stl 学习笔记 11

// stl/map1
#include  < iostream >
#include 
< map >
#include 
< string >
using   namespace  std;

int  main()
{
    
//StringFloatMap
    typedef map<string,float> StringFloatMap;

    StringFloatMap stocks;
    
    stocks[
"BASF"= 369.50;
    stocks[
"VW"= 413.50;
    stocks[
"Daimler"= 819.00;
    stocks[
"BMW"= 834.00;
    stocks[
"Siemens"= 842.00;

    StringFloatMap::iterator pos;

    
for(pos = stocks.begin();pos != stocks.end(); ++pos)
    
{
        cout 
<<"stocks: "<<pos ->first <<" "
            
<<"price: " << pos ->second <<endl;
    }

    cout 
<<endl;

    
for(pos = stocks.begin(); pos != stocks.end(); ++pos)
    
{
        pos 
->second *= 2;
    }


    
for(pos = stocks.begin();pos != stocks.end(); ++pos)
    
{
        cout 
<<"stocks: " << pos ->first<< " "
            
<<"price: " << pos ->second <<endl;
    }

    cout 
<<endl;

    stocks[
"Volkswagen"= stocks["VW"];
    stocks.erase(
"VW");

    
for(pos == stocks.begin(); pos != stocks.end(); ++pos)
    
{
        cout 
<<"stock: "<< pos ->first <<" "
            
<<"price: "<< pos ->second <<endl;
    }

}


/*
result
======================================
stocks: BASF    price: 369.5
stocks: BMW     price: 834
stocks: Daimler price: 819
stocks: Siemens price: 842
stocks: VW      price: 413.5

stocks: BASF    price: 739
stocks: BMW     price: 1668
stocks: Daimler price: 1638
stocks: Siemens price: 1684
stocks: VW      price: 827
=======================================
*/

 

 

// mmap1.cpp
#include  < iostream >
#include 
< map >
#include 
< string >
#include 
< iomanip >
using   namespace  std;

int  main()
{
    typedef multimap
<string,string> StrStrMMap;
    StrStrMMap dict;

    dict.insert(make_pair(
"day","Tag"));
    dict.insert(make_pair(
"strange","fremd"));
    dict.insert(make_pair(
"car","Auto"));
    dict.insert(make_pair(
"smart","elegant"));
    dict.insert(make_pair(
"trait","Merkmal"));
    dict.insert(make_pair(
"strange","seltsam"));
    dict.insert(make_pair(
"smart","raffiniert"));
    dict.insert(make_pair(
"smart","klug"));
    dict.insert(make_pair(
"clever","raffiniert"));

    StrStrMMap::iterator pos;
    cout.setf(ios::left,ios::adjustfield);
    cout 
<<' ' << setw(10<<"English "
        
<<"German "<<endl;
    cout 
<<setfill('-'<<setw(20<<""
        
<<setfill(' ')<<endl;

    
for(pos = dict.begin(); pos != dict.end();++pos)
    
{
        cout 
<<' ' <<setw(10<<pos ->first.c_str()
            
<< pos ->second <<endl;
    }

    cout 
<<endl;

    
string word("smart");
    cout 
<<word<<"" <<endl;

    
for(pos = dict.lower_bound(word);
        pos 
!= dict.upper_bound(word);
        
++pos)
    
{
        cout 
<<"   " << pos ->second <<endl;
    }


    word 
=("raffiniert");
    cout 
<<word <<"" <<endl;

    
for(pos = dict.begin(); pos != dict.end(); ++pos)
    
{
        
if(pos ->second ==word)
            cout 
<<"   "<<pos ->first<<endl;
    }

}


/*
result
===============================================
 English   German
--------------------
 car       Auto
 clever    raffiniert
 day       Tag
 smart     elegant
 smart     raffiniert
 smart     klug
 strange   fremd
 strange   seltsam
 trait     Merkmal

smart:
   elegant
   raffiniert
   klug
raffiniert:
   clever
   smart
==================================================
*/

 

 

// stl/mapfind.cpp
/*
================
find Object
================
*/

#include 
< iostream >
#include 
< algorithm >
#include 
< map >
using   namespace  std;

template 
< class  K, class  V >
class  value_equals
{
private:
    V value;
public:
    value_equals(
const V& v)
        :value(v)
{}

    
bool operator()(pair<const K,V> elem)
    
{
        
return elem.second == value;
    }

}
;

int  main()
{
    typedef map
<float,float> FloatFloatMap;
    FloatFloatMap coll;
    FloatFloatMap::iterator pos;

    coll[
1= 7;
    coll[
2= 4;
    coll[
3= 2;
    coll[
4= 3;
    coll[
5= 1;
    coll[
6= 6;
    coll[
7= 3;

    pos 
= coll.find(3.0);
    
if(pos != coll.end()){
        cout 
<<pos ->first<<""
            
<<pos ->second<<endl;
    }


    pos 
= find_if(coll.begin(),coll.end(),
        value_equals
<float,float>(3.0));

    
if(pos != coll.end())
    
{
        cout 
<<pos ->first<<""
            
<<pos ->second<<endl;
    }

}


/*
result
====================
3: 2
4: 3
====================
*/

 

 

// stl/mapcmp.cpp

/*
=================================================
高级技巧
1.如何使用maps
2.写仿函数
3.执行器定义排序准则
4.比较字符串,不在乎大小写
=================================================
*/

#include 
< iostream >
#include 
< iomanip >
#include 
< map >
#include 
< string >
#include 
< algorithm >
using   namespace  std;

class  RuntimeStringCmp
{
public:
    
enum cmp_mode {normal,nocase};
private:
    
const cmp_mode mode;

    
static bool nocase_compare(char c1,char c2)
    
{
        
return toupper(c1) <toupper(c2);
    }


public:
    RuntimeStringCmp(cmp_mode m 
= normal):mode(m)
    
{}

    
bool operator() (const string& s1,const string& s2)const
    
{
        
if(mode == normal)
            
return s1 < s2;
        
else
            
return lexicographical_compare(s1.begin(),s1.end(),
            s2.begin(),s2.end(),nocase_compare);
    }

}
;

typedef map
< string , string ,RuntimeStringCmp >  StringStringMap;

void  fillAndPrint(StringStringMap &  coll);

int  main()
{
    StringStringMap coll1;
    fillAndPrint(coll1);

    RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase);

    StringStringMap coll2(ignorecase);
    fillAndPrint(coll2);
}


void  fillAndPrint(StringStringMap &  coll)
{
    coll[
"Deutshland"= "Germanry";
    coll[
"deutsh"= "German";
    coll[
"Hanken"= "snag";
    coll[
"arbeiten"= "word";
    coll[
"Hund"= "dog";
    coll[
"gehen"= "go";
    coll[
"Unternehmen"= "enterprise";
    coll[
"unternehmen"= "undertake";
    coll[
"gehen"= "walk";
    coll[
"Bestatter"= "undertaker";

    StringStringMap::iterator pos;

    cout.setf(ios::left,ios::adjustfield);
    
for(pos=coll.begin(); pos != coll.end();++pos)
    
{
        cout 
<<setw(15)<<pos ->first.c_str() <<" "
            
<<pos ->second <<endl;
    }

    cout 
<<endl;
}


/*
result
=============================
Bestatter       undertaker
Deutshland      Germanry
Hanken          snag
Hund            dog
Unternehmen     enterprise
arbeiten        word
deutsh          German
gehen           walk
unternehmen     undertake

arbeiten        word
Bestatter       undertaker
deutsh          German
Deutshland      Germanry
gehen           walk
Hanken          snag
Hund            dog
Unternehmen     undertake
===============================
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值