您的位置:首页 > 其它

STL学习之map容器-insert

2015-03-27 09:51 489 查看
pair<iterator,bool> insert(const value_type& x);
iterator insert(iterator position, const value_type& x);
template<class InputIterator>
void insert(InputIterator first, InputIterator last);


插入元素

map容器可以插入单个新元素(如果使用参数x)或者元素序列(如果使用输入迭代器)。

随着插入元素数量增加,容器的size也随着增加。

由于map容器不允许键值重复,所以插入操作需要确保要插入的key值在map容器中没有。如果有的话,新的元素是不允许插入的,并且此键值的映射值是不会发生改变的。

如果想允许插入相同的元素,可以参考multimap。

本质上,map容器保持所有元素按照结构体指定的比较对象排序。

map容器允许直接的访问通过map::operator[]操作(对[]操作符进行了重载),即可以通过object[key]=value;这样操作。

参数

x

初始化插入元素的值。

position

确定插入操作时第一个相比较的元素。该参数并不是强制插入元素的位置。

first,last

指定元素区间的迭代器。拷贝[first,last)区间的元素,并且插入到map容器中。

返回值

第一个版本的insert返回一个pair类型,pair类型的成员pair:first设置一个迭代器指针,这个迭代器指针可能指向新插入的元素或者已经在map容器中存在的有着相同key值的元素;成员pair:second元素,如果新的元素插入,返回为true,或者当已经存在相同的key,返回false。

第二个版本返回一个指向新插入的元素或者已经在map容器中存在的有着相同key值的元素的迭代器指针。

迭代器是一个成员类型,被定义成双向迭代器类型。

举例1:
#include <string>
#include <iostream>
#include <map>
#include <utility>

using namespace std;

int main(void)
{
map<string, int> Employees;

Employees["Mike C."] = 12306;
Employees["Charlie M."] = 95555;
//使用pair
Employees.insert(std::pair<string, int>("David D.", 1984));
Employees.insert(map<string, int>::value_type("John A.", 7582));
Employees.insert(make_pair("Peter Q.", 5328));

cout << "Map size: " << Employees.size() <<  endl;

for (map<string, int>::iterator ii= Employees.begin(); ii != Employees.end(); ++ii)
{
        cout << (*ii).first << ":" << (*ii).second << endl;
        }
        return 0;
}

执行结果:

liujl@liujl-Rev-1-0:~/mycode/STL$ ./mapEx2
Map size: 5
Charlie M.:95555
David D.:1984
John A.:7582
Mike C.:12306
Peter Q.:5328


实例2:

#include <iostream>
#include <map>

using namespace std;

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

//first insert function version
mymap.insert( pair<char, int>('a', 100) );
mymap.insert( make_pair('d', 200) );
mymap.insert( map<char, int>::value_type('z', 500));

//check return value
ret = mymap.insert( pair<char, int>('z', 800));
if (ret.second == false)
{
cout << " element 'z' already existed!";
cout << " with a value of " << ret.first->second << endl;
}

//second insert function version
it = mymap.begin();
mymap.insert(it, pair<char, int>('b', 300));
mymap.insert(it, pair<char, int>('c', 400));

//third insert function version
map<char, int> othermap;
othermap.insert(mymap.begin(), mymap.end());
//othermap.insert(mymap.begin(), mymap.find('c'));

//print container contents:

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

cout << "other contains: " << endl;
for (it=othermap.begin(); it != othermap.end(); it++)
cout << (*it).first << "=>" << (*it).second << endl;
return 0;
}


执行结果:

liujl@liujl-Rev-1-0:~/mycode/STL$ vim map_insert.cpp
liujl@liujl-Rev-1-0:~/mycode/STL$ g++ map_insert.cpp  -o map_insert
liujl@liujl-Rev-1-0:~/mycode/STL$ ./map_insert
element 'z' already existed! with a value of 500
mymap contains:
a=>100
b=>300
c=>400
d=>200
z=>500
other contains:
a=>100
b=>300
c=>400
d=>200
z=>500
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: