stl_map复习

2020-02-02 16:01:38来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

stl_map复习

set和map的底层模板是红黑树,可以有不同的键值和实值,关于增删改查,迭代器的使用都在代码里面,亲手尝试更方便记忆

#include <iostream>
#include <map>
#include <algorithm>

//#include <functional>//不知道用来干什么

using namespace std;
void fun(pair<int,char>pr){
cout << pr.first << "-" << pr.second << " ";
}
int main()
{

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';

//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;
//键值不可修改,实值可以修改

//删
//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();

//cout << imp.max_size() << endl;

//改
//imp[3]='w';
//cout <<imp[3] << endl;

//查
//ite=imp.find(2);//若找不到,指向end()
//cout << imp[2] << " " << ite->second << endl;
//cout << imp[11] << endl;//找不到就不输出
//for_each(imp.begin(),imp.end(),fun);
cout << endl;

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;
}


原文链接:https://www.cnblogs.com/sos3210/p/12253668.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:从零开始学C++(1 变量和基本类型)

下一篇:向量容器vector操作