set和map的底層模板是紅黑樹,能夠有不一樣的鍵值和實值,關於增刪改查,迭代器的使用都在代碼裏面,親手嘗試更方便記憶ios
#include <iostream>
#include <map>
#include <algorithm>less
//#include <functional>//不知道用來幹什麼spa
using namespace std;
void fun(pair<int,char>pr){
cout << pr.first << "-" << pr.second << " ";
}
int main()
{element
map<int,char>imp;
imp.insert(pair<int,char>(3,'a'));
imp.insert(pair<int,char>(2,'b'));
imp.insert(pair<int,char>(1,'c'));
imp.insert(pair<int,char>(4,'f'));
imp.insert(pair<int,char>(5,'g'));
imp[10]='i';it
//imp.insert(pair<int,char>(2,'a'));
map<int,char,less<int> >imp1(imp);
map<int,char>imp2(imp1.begin(),imp1.end());
map<int,char>::iterator ite=imp2.begin();
imp.clear();
ite++;
imp.insert(ite,imp2.end());
imp.swap(imp1);
//cout << imp.size() << " " << imp.count(2) << " " << imp.empty() << endl;
ite=imp.find(2);
ite=imp.upper_bound(1);//>,返回迭代器
ite=imp.lower_bound(3);//<=,返回迭代器
//cout << ite->second << endl;
//鍵值不可修改,實值能夠修改io
//刪
//imp.erase(2);
map<int,char>::iterator ite1=imp.find(3);
map<int,char>::iterator ite2=imp.find(4);
//imp.erase(ite1,ite2);//ite1到ite2的前一個元素
//imp.erase(ite);
//imp.clear();function
//cout << imp.max_size() << endl;模板
//改
//imp[3]='w';
//cout <<imp[3] << endl;stream
//查
//ite=imp.find(2);//若找不到,指向end()
//cout << imp[2] << " " << ite->second << endl;
//cout << imp[11] << endl;//找不到就不輸出
//for_each(imp.begin(),imp.end(),fun);
cout << endl;object
map<int,char,greater<int> >rimp(imp.begin(),imp.end());
for_each(rimp.begin(),rimp.end(),fun);
cout << endl;
pair<map<int,char>::iterator,map<int,char>::iterator> pre;
pre=imp.equal_range(2);
cout << pre.first->first << endl;
cout << pre.second->first << endl;
/*
//key_comp()
map <int, int, less<int> > m1;
map <int, int, less<int> >::key_compare kc1 = m1.key_comp( ) ;
bool result1 = kc1( 2, 3 ) ;
if( result1 == true )
{
cout << "kc1( 2,3 ) returns value of true, "
<< "where kc1 is the function object of m1."
<< endl;
}
else
{
cout << "kc1( 2,3 ) returns value of false "
<< "where kc1 is the function object of m1."
<< endl;
}
map <int, int, greater<int> > m2;
map <int, int, greater<int> >::key_compare kc2 = m2.key_comp( );
bool result2 = kc2( 2, 3 ) ;
if( result2 == true )
{
cout << "kc2( 2,3 ) returns value of true, "
<< "where kc2 is the function object of m2."
<< endl;
}
else
{
cout << "kc2( 2,3 ) returns value of false, "
<< "where kc2 is the function object of m2."
<< endl;
}
*/
/*
equal_range()//在algorithm頭文件,map頭文件
typedef map <int, int, less<int> > IntMap;
IntMap m1;
map <int, int> :: const_iterator m1_RcIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
pair <IntMap::const_iterator, IntMap::const_iterator> p1, p2;
p1 = m1.equal_range( 2 );
cout << "The lower bound of the element with "
<< "a key of 2 in the map m1 is: "
<< p1.first -> second << "." << endl;
cout << "The upper bound of the element with "
<< "a key of 2 in the map m1 is: "
<< p1.second -> second << "." << endl;
// Compare the upper_bound called directly
m1_RcIter = m1.upper_bound( 2 );
cout << "A direct call of upper_bound( 2 ) gives "
<< m1_RcIter -> second << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 2 )." << endl;
p2 = m1.equal_range( 4 );
// If no match is found for the key, // both elements of the pair return end( ) if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) ) cout << "The map m1 doesn't have an element " << "with a key less than 40." << endl; else cout << "The element of map m1 with a key >= 40 is: " << p2.first -> first << "." << endl;} */ return 0;}