您的位置:首页 > 编程语言 > C语言/C++

C++:STL常用模块总结(map)

2017-12-03 22:11 471 查看

map

map又称为哈希表,是一个由标记值(key value)和映射(mapped value)组成的关系列表,其中标记值将映射值进行排序和整理,每一个标记值对应着一个映射值,map在通过标记值找到映射值的过程比unordered_map慢,但是可以通过指针依照排放顺序来进行操作。

使用之前引用

#include <map>


定义方法重载函数汇总

empty (1):
explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);
range (2):
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type());
copy (3):
map (const map& x);
map (const map& x, const allocator_type& alloc);
move (4):
map (map&& x);
map (map&& x, const allocator_type& alloc);
initializer list (5):
map (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());


定义程序示例

#include <iostream>
#include <map>

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 ()
{
std::map<char,int> first;

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

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

std::map<char,int> third (second);

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

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

return 0;
}


基本操作

1、
map::operator[]
:

mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);
//如果k在map里面存在,则返回映射值,如果不存在,就会以k值进本身行插入并返回一个相对映射值的引用(map会自动开辟一个空间以放置多余的元素)。相同的类似的操作符map::at,除了k在不存在时会抛出异常,其他与[]操作符功能相同。


示例程序

#include <iostream>
#include <map>
#include <string>

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

mymap['a']="an element";
mymap['b']="another element";
mymap['c']=mymap['b'];

std::cout << "mymap['a'] is " << mymap['a'] << '\n';
std::cout << "mymap['b'] is " << mymap['b'] << '\n';
std::cout << "mymap['c'] is " << mymap['c'] << '\n';
std::cout << "mymap['d'] is " << mymap['d'] << '\n';
//注意最后一个元素‘d’的访问操作时,操作器在map容器中插入了一个元素(初始化),一个空的字符串,仅仅可以通过检索找到,通过map::find是找不到的

std::cout << "mymap now contains " << mymap.size() << " elements.\n";

return 0;
}


output:

mymap['a'] is an element
mymap['b'] is another element
mymap['c'] is another element
mymap['d'] is
mymap now contains 4 elements.


2、
map::find

iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
//在map容器中寻找是否有对应的标记的映射值,如果有的话就返回只想这个元素的指针,如果没有就返回指向map::end的指针


示例程序

#include <iostream>
#include <map>

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

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

it = mymap.find('b');
if (it != mymap.end())
mymap.erase (it);

// print content:
std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n';
std::cout << "d => " << mymap.find('d')->second << '\n';

return 0;
}


3、
map::begin
:返回指向第一个元素的指针,如果不存在,返回的指针将不能被访问。

4、
map::end
:返回指向最后一个元素的指针,如果不存在,返回的指针将不能被访问。

示例程序

#include <iostream>
#include <map>

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

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

// show content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';

return 0;
}


5、
map::size
:返回map容器中的元素数量

6、
map::empty
:如果map容器为空,则返回1,反之返回0

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: