stl 学习笔记 11
2007-12-05 00:46
417 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//stl/map1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//StringFloatMap
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef map<string,float> StringFloatMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringFloatMap stocks;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks["BASF"] = 369.50;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks["VW"] = 413.50;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks["Daimler"] = 819.00;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks["BMW"] = 834.00;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks["Siemens"] = 842.00;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringFloatMap::iterator pos;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos = stocks.begin();pos != stocks.end(); ++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<"stocks: "<<pos ->first <<" "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<"price: " << pos ->second <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos = stocks.begin(); pos != stocks.end(); ++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pos ->second *= 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos = stocks.begin();pos != stocks.end(); ++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<"stocks: " << pos ->first<< " "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<"price: " << pos ->second <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks["Volkswagen"] = stocks["VW"];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks.erase("VW");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos == stocks.begin(); pos != stocks.end(); ++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<"stock: "<< pos ->first <<" "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<"price: "<< pos ->second <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
======================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: BASF price: 369.5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: BMW price: 834
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: Daimler price: 819
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: Siemens price: 842
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: VW price: 413.5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: BASF price: 739
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: BMW price: 1668
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: Daimler price: 1638
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: Siemens price: 1684
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stocks: VW price: 827
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
=======================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//mmap1.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iomanip>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef multimap<string,string> StrStrMMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StrStrMMap dict;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("day","Tag"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("strange","fremd"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("car","Auto"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("smart","elegant"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("trait","Merkmal"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("strange","seltsam"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("smart","raffiniert"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("smart","klug"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dict.insert(make_pair("clever","raffiniert"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StrStrMMap::iterator pos;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout.setf(ios::left,ios::adjustfield);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<' ' << setw(10) <<"English "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<"German "<<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<setfill('-') <<setw(20) <<""
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<setfill(' ')<<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos = dict.begin(); pos != dict.end();++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<' ' <<setw(10) <<pos ->first.c_str()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< pos ->second <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string word("smart");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<word<<": " <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos = dict.lower_bound(word);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pos != dict.upper_bound(word);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<" " << pos ->second <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
word =("raffiniert");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<word <<": " <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos = dict.begin(); pos != dict.end(); ++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(pos ->second ==word)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<" "<<pos ->first<<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
===============================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
English German
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
--------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
car Auto
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
clever raffiniert
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
day Tag
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smart elegant
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smart raffiniert
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smart klug
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strange fremd
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strange seltsam
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
trait Merkmal
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smart:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
elegant
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
raffiniert
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
klug
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
raffiniert:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
clever
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smart
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
==================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//stl/mapfind.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
find Object
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <algorithm>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template <class K,class V>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class value_equals
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
V value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
value_equals(const V& v)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
:value(v)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool operator()(pair<const K,V> elem)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return elem.second == value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef map<float,float> FloatFloatMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FloatFloatMap coll;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FloatFloatMap::iterator pos;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[1] = 7;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[2] = 4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[3] = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[4] = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[5] = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[6] = 6;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll[7] = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pos = coll.find(3.0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(pos != coll.end())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<pos ->first<<": "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<pos ->second<<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pos = find_if(coll.begin(),coll.end(),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
value_equals<float,float>(3.0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(pos != coll.end())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<pos ->first<<": "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<pos ->second<<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
====================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
3: 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
4: 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
====================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//stl/mapcmp.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
=================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
高级技巧
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
1.如何使用maps
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
2.写仿函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
3.执行器定义排序准则
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
4.比较字符串,不在乎大小写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
=================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iomanip>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <algorithm>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class RuntimeStringCmp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
enum cmp_mode ...{normal,nocase};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
const cmp_mode mode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static bool nocase_compare(char c1,char c2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return toupper(c1) <toupper(c2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RuntimeStringCmp(cmp_mode m = normal):mode(m)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool operator() (const string& s1,const string& s2)const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(mode == normal)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return s1 < s2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return lexicographical_compare(s1.begin(),s1.end(),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s2.begin(),s2.end(),nocase_compare);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
typedef map<string,string,RuntimeStringCmp> StringStringMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void fillAndPrint(StringStringMap& coll);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringStringMap coll1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fillAndPrint(coll1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringStringMap coll2(ignorecase);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fillAndPrint(coll2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void fillAndPrint(StringStringMap& coll)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["Deutshland"] = "Germanry";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["deutsh"] = "German";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["Hanken"] = "snag";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["arbeiten"] = "word";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["Hund"] = "dog";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["gehen"] = "go";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["Unternehmen"] = "enterprise";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["unternehmen"] = "undertake";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["gehen"] = "walk";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
coll["Bestatter"] = "undertaker";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringStringMap::iterator pos;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout.setf(ios::left,ios::adjustfield);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(pos=coll.begin(); pos != coll.end();++pos)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<setw(15)<<pos ->first.c_str() <<" "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<<pos ->second <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout <<endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
=============================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Bestatter undertaker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Deutshland Germanry
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hanken snag
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hund dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Unternehmen enterprise
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arbeiten word
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
deutsh German
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gehen walk
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
unternehmen undertake
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arbeiten word
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Bestatter undertaker
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
deutsh German
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Deutshland Germanry
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
gehen walk
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hanken snag
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hund dog
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Unternehmen undertake
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
===============================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
相关文章推荐
- 11/7/8 STL 映射容器 map< K , T > 学习笔记
- C++ Primer 学习笔记_37_STL实践与分析(11)--set类型
- STL学习笔记11— —特殊容器stack
- java 设计模式 学习笔记(11) 建造者模式
- STL学习笔记----12.STL算法之 (移除性算法)
- 09 11 30 Java学习笔记
- STL学习笔记----7.STL迭代器
- STL学习笔记 ---- 工具类pair
- C++ STL学习笔记二 vector向量容器
- C++ STL学习笔记十二 hash_map映照容器
- STL学习笔记-- map
- STL学习笔记之算法--algorithmn
- STL算法学习笔记。 from 《Professional C++》
- QT学习笔记11常用控件
- STL学习笔记之容器--list(一)
- STL学习笔记----1.概述
- C++ Primer 学习笔记_40_STL实践与分析(14)--概要、先来看看算法【上】
- STL学习笔记-- multimap
- PHP学习笔记11(字符串函数)
- 汇编学习笔记(11)MOVZX,MOVSX