现在的位置: 首页 > 综合 > 正文

map常用操作

2011年07月07日 ⁄ 综合 ⁄ 共 3314字 ⁄ 字号 评论关闭

初始化和赋值

// constructing maps
#include <iostream>
#include <map>
using namespace std;

bool fncomp (char lhs, char rhs) {return lhs<rhs;}

struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
};

int main ()
{
map<char,int> first;

first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;

map<char,int> second (first.begin(),first.end());

map<char,int> third (second);

map<char,int,classcomp> fourth; // class as Compare

bool(*fn_pt)(char,char) = fncomp;
map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare

return 0;
}


 如果map::operator[]中key不匹配任何在容器中的元素,该函数插入一个新元素,元素使用其默认构造函数构造。

 

获取元素数量

// map::size
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
mymap['a']=101;
mymap['b']=202;
mymap['c']=302;

cout << "mymap.size() is " << (int) mymap.size() << endl;

return 0;
}

 

// map::empty
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;

mymap['a']=10;
mymap['b']=20;
mymap['c']=30;

while (!mymap.empty())
{
cout << mymap.begin()->first << " => ";
cout << mymap.begin()->second << endl;
mymap.erase(mymap.begin());
}

return 0;
}

 

插入

// map::insert
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
pair<map<char,int>::iterator,bool> ret;

// first insert function version (single parameter):
mymap.insert ( pair<char,int>('a',100) );
mymap.insert ( pair<char,int>('z',200) );
ret=mymap.insert (pair<char,int>('z',500) );
if (ret.second==false)
{
cout << "element 'z' already existed";
cout << " with a value of " << ret.first->second << endl;
}

// second insert function version (with hint position):
it=mymap.begin();
mymap.insert (it, pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, pair<char,int>('c',400)); // no max efficiency inserting

// third insert function version (range insertion):
map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));

// showing contents:
cout << "mymap contains:\n";
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;

cout << "anothermap contains:\n";
for ( it=anothermap.begin() ; it != anothermap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;

return 0;
}

 

遍历

// map::begin/end
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;

// show content:
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;

return 0;
}

 

查找

// map::find
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;

it=mymap.find('b');
mymap.erase (it);
mymap.erase (mymap.find('d'));
it=mymap.find('d');
if (it == mymap.end()) {
cout << "key 'd' is not exist" << endl;
}
// print content:
cout << "elements in mymap:" << endl;
cout << "a => " << mymap.find('a')->second << endl;
cout << "c => " << mymap.find('c')->second << endl;

return 0;
}

 

删除

// erasing from map
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

// insert some values:
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
mymap['e']=50;
mymap['f']=60;

it=mymap.find('b');
mymap.erase (it); // erasing by iterator

mymap.erase ('c'); // erasing by key

it=mymap.find ('e');
mymap.erase ( it, mymap.end() ); // erasing by range

// show content:
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;

return 0;
}

 

更多操作

 参考  http://www.cplusplus.com/reference/stl/map/empty/

 

 

 

 

 

 

 

 

抱歉!评论已关闭.