小结:
构造函数:
explicit map ( const Compare& comp =
Compare(),
const Allocator& = Allocator() );
说明:默认构造函数,使用comp对象中定义的规则来创建一个空的map。
template <class
InputIterator>
map ( InputIterator first, InputIterator last,
const Compare& comp = Compare(), const
Allocator& = Allocator() );
说明:重复序列构造函数。将first(包含)到last(不包含)之间的元素复制到map中。
map ( const
map<Key,T,Compare,Allocator>&
x );
说明:复制构造函数。将x中的元素复制到map中。
map<Key,T,Compare,Allocator>&
operator= ( const
map<Key,T,Compare,Allocator>&
x );
说明:赋值运算符。将map中原来的元素全部清空后,然后将x中的元素拷贝到map中。
插入操作:
第一个版本的插入操作:
pair<iterator,bool> insert ( const
value_type& x );
说明:插入操作。需要注意的是map的insert操作和一般容器的insert操作有点不一样,它并不一定会将传入的键值对加入到容器中。它首先会查找
容器中是否存在待插入的键值,如果不存在这样的键值,则将传入的键值对插入到map中;如果存在的话,则不会将键值对插入到map中。
参数:x。插入的键值对。
typedef pair<key, value>
value_type
类型pair是STL中用来表示对的数据结构,我们可以通过它的数据成员first访问key,通过访问它的数据成员second访问value。
返回:pair<iterator,bool>。返回的是一个键值对。其中iterator表示元素在map中的迭代器,bool表示插入是
否成功。如果成功的话为true,失败则为false。需要注意的是,如果插入失败的话,iterator表示该键值元素在map中的迭代器。
第二个版本的插入操作:
iterator insert ( iterator position, const
value_type& x );
参数:position。表示在插入的时候,会与map中首先比较元素的迭代器。通过前面的介绍我们知道,向map中插入元素的时候,插入的元素会和
map中的元素进行比较,如果没有相同的键,才将元素插入到map中。通过传入position,我们可以定义待插入的元素会和map中哪一个元素首先进
行比较,以此来提高我们的插入效率。当然,该参数不能胡乱填写。如果胡乱填写的话,会引起效率的损失。
第三个版本的插入操作:
void insert ( InputIterator first, InputIterator last
);
参数:first。要插入map中序列迭代器的开头(包含开头元素)。
参数:last。要插入map中序列迭代器的结尾(不包含结尾元素)。
注意InputIterator所指向的元素类型应为value_type。
另外一个也拥有插入功能的操作
T& operator[] ( const key_type& x
);
说明:当该操作作为右值的时候,会得到该键值对应的值。当该操作作为左值的时候,如果要插入的键不存在,则会将新的键值对插入到map中。如果插入的键存在,则会修改键所对应的值。
eg.
#include
<cstring>
#include
<iostream>
#include
<map>
#include
<vector>
#include
<algorithm>
void print(const std::pair<char,
int> &p)
{
std::cout
<< "key =
"
<< p.first
<< "\t"
<< "value = "
<<
p.second
<< std::endl;
}
int main()
{
//
第二种插入方法
std::map<char, int>
m1;
m1.insert(std::pair<char, int>('a',
1));
m1.insert(std::pair<char, int>('b',
2));
m1.insert(std::pair<char, int>('d',
4));
m1.insert(m1.find('b'), std::pair<char,
int>('c', 3)); // effective
insertion
std::for_each(m1.begin(), m1.end(), print); //
很好的用法
std::cout
<< std::endl;
//
第一种插入方法
std::pair<std::map<char,
int>::iterator, bool> r =
m1.insert(std::pair<char, int>('d',
4));
if
(!r.second)
{
std::cout << "The key has already
existed in map." << std::endl
<<
std::endl;
}
//
第三种插入方法,利用vector插入
std::vector<std::pair<char,
int> > v;
v.push_back(std::pair<char, int>('a',
1));
v.push_back(std::pair<char, int>('b',
2));
v.push_back(std::pair<char, int>('c',
3));
std::map<char, int>
m2;
m2.insert(v.begin(), v.end());
std::for_each(m2.begin(), m2.end(), print);
std::cout
<< std::endl;
//
第四种插入方法
m2['d'] =
4;
m2['c'] =
5;
std::for_each(m2.begin(), m2.end(), print);
system("pause");
return
0;
}
结果:
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
The key has already existed in map.
key = a value = 1
key = b value = 2
key = c value = 3
key = a value = 1
key = b value = 2
key = c value = 5
key = d value = 4
请按任意键继续. . .
下面四个为map的迭代器函数
iterator begin ();
iterator end ();
iterator rbegin ();
iterator rend ();
说明:这几个函数分别获取map的开始、结尾、逆向开始、逆向结尾的迭代器。只是需要注意的是,map作为一种关联容器,其开始迭代器/逆向开始迭代器并不一定就是指向插入map中的第一个/最后一个元素,而是指向按照规则comp由小到大排序的最小/最大元素。
看一下在map中比较常用的几个操作:
iterator find ( const key_type& x
);
说明:搜索整个map,判断键值x是否在map中存在。
参数:x。待搜索的键值。
返回:成功返回指向键值为x的元素的迭代器,失败返回map::end()。
void erase ( iterator position );
size_type erase ( const key_type& x
);
void erase ( iterator first, iterator last
);
说明:删除操作。第一个版本在map中删除指向position的元素。第二个版本删除map中key的值为x的元素。第三个版本删除迭代器first(包含)到last(不包含)之间的所有元素。
参数:position。指向待删除元素的迭代器。
参数:x。待删除元素的键值。
参数:first。指向待删除序列头的迭代器。
参数:last。指向待删除序列尾的迭代器。
返回:只有第二个版本的删除操作有返回值。其返回值表示在map中删除键值为x的元素个数,该值在map中只能为1。
eg.
#include
<cstring>
#include
<iostream>
#include
<map>
#include
<vector>
#include
<algorithm>
void print(const std::pair<char,
int> &p)
{
std::cout
<< "key =
"
<< p.first
<< "\t"
<< "value = "
<<
p.second
<< std::endl;
}
int main()
{
std::map<char, int>
m;
for (int i =
1, c = 'a'; i <= 10; ++i,
++c)
{
m.insert(std::pair<char, int>(c,
i));
}
for_each(m.begin(), m.end(), print);
std::cout
<< std::endl;
//
第一种方法
m.erase(m.find('c'));
for_each(m.begin(), m.end(), print);
std::cout
<< std::endl;
//
第二种方法
m.erase('d');
for_each(m.begin(), m.end(), print);
std::cout
<< std::endl;
//
第三种方法
m.erase(m.find('f'), m.end());
for_each(m.begin(), m.end(), print);
system("pause");
return
0;
}
结果:
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7
key = h value = 8
key = i value = 9
key = j value = 10
key = a value = 1
key = b value = 2
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7
key = h value = 8
key = i value = 9
key = j value = 10
key = a value = 1
key = b value = 2
key = e value = 5
key = f value = 6
key = g value = 7
key = h value = 8
key = i value = 9
key = j value = 10
key = a value = 1
key = b value = 2
key = e value = 5
请按任意键继续. . .
一个使用自定义comp对象的例子:
#include
<cstring>
#include
<iostream>
#include
<map>
#include
<vector>
#include
<algorithm>
typedef bool (*compCB)(const char &lhs, const
char &rhs);
bool comp(const char &lhs, const char
&rhs)
{
return lhs
< rhs;
}
class CCompNoCase
{
public:
bool
operator()(const char &lhs, const char
&rhs)
{
return toupper(lhs) <
toupper(rhs);
}
};
void print(const std::pair<char,
int> &p)
{
std::cout
<< "key =
"
<< p.first
<<
"\t"
<< "value = "
<<
p.second
<< std::endl;
}
int main()
{
// default
comp
std::map<char, int>
m1;
m1['a'] =
1;
m1['b'] =
2;
std::for_each(m1.begin(), m1.end(), print);
std::cout
<< std::endl;
// case
comp
std::map<char, int, compCB>
m2(comp);
m2['a'] =
1;
m2['B'] =
2;
std::for_each(m2.begin(), m2.end(), print);
std::cout
<< std::endl;
// nocase
comp
std::map<char, int, CCompNoCase>
m3;
m3['a'] =
1;
m3['B'] =
2;
std::for_each(m3.begin(), m3.end(), print);
system("pause");
return
0;
}
结果:
key = a value = 1
key = b value = 2
key = B value = 2
key = a value = 1
key = a value = 1
key = B value = 2
请按任意键继续. . .
|