C++ STL map中的函数



at

#include "stdafx.h"

#include <iostream>
#include <map>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;

	m.insert(make_pair("one", 1));

	cout << m["one"] << endl;

	m.at("one")++;

	cout << m.at("one") << endl;
	return 0;
}

输出:

1

2


clear() :清空容器中所有元素(映射)。


count () :统计指定first元素存在的映射数,存在为1,不存在为0.

#include "stdafx.h"

#include <iostream>
#include <map>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;

	m.insert(make_pair("two", 2));

	cout << m.count("one") << endl;
	cout << m.count("two") << endl;
	return 0;
}

输出:

0

1


emplace() :

只有在当前容器不存在某个元素的主键等价于被放置插入的元素的主键的情况下,插入操作才会发生。

如果发生插入操作,将直接使容器的大小增加,增加的值为 1。

如果当前函数成功插入一个元素,将会返回由指向新插入元素的迭代器及 true 值组成的二元组(Pair)。


int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;

	m.emplace(make_pair("one", 1));

	cout << m["one"] <<endl;
	return 0;	
}
输出结果:

1


emplace_hint :

在特定位置插入元素(需要先检测是否存在该主键,存在则不插入)。


int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;
	map<string, int>::iterator it;

	m.emplace_hint(m.begin(), make_pair("one", 1));

	m.emplace_hint(m.begin(), make_pair("two", 2));

	m.emplace_hint(m.end(), make_pair("three", 3));

	for (it = m.begin(); it != m.end(); ++it)
		cout << "first:"<< it->first << "second:" << it->second << endl;
	return 0;	
}

输出:

first:onesecond:1
first:threesecond:3
first:twosecond:2

可见当在end()插入位置插入元素时是插入到最后一个元素之前的位置。


empty() : 判断容器是否为空。


equal_range :


erase :删除

通过迭代器删除;

通过主键删除;

通过迭代器返回删除;

int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;
	map<string, int>::iterator it;

	m.insert(make_pair("a", 1));
	m.insert(make_pair("b", 2));
	m.insert(make_pair("c", 3));
	m.insert(make_pair("d", 4));
	m.insert(make_pair("e", 5));

	m.erase("a");

	it = m.find("b");
        m.erase(it);

        it = m.find("d");
	m.erase(it, m.end());

	for (it = m.begin(); it != m.end(); ++it)
		cout << it->first << " " << it->second << endl;
	return 0;	
}

输出:

c 3


find : 根据主键查找查找并返回迭代器

int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;
	map<string, int>::iterator it;


	m.insert(make_pair("a", 1));
	it = m.find("a");

	cout << it->first << " " << it->second << endl;
	return 0;	
}

输出:

a 1


get_allocator() :返回适配器


insert  :

插入pair组

如果当前函数成功插入一个元素,将会返回由指向新插入元素的迭代器及 true 值组成的二元组(Pair)。

直接插入;

插入到指定位置;

按照迭代器范围插入;

int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> m;
	map<string, int>::iterator it;
	pair<map<string, int>::iterator, bool> ret;

	m.insert(make_pair("a", 1));

	ret = m.insert(make_pair("a", 1));
	if (ret.second == false)
	{
		cout << "已存在主键a" <<endl;
	}

	m.insert(m.begin(), make_pair("b", 2));

	map<string, int> M;

	M.insert(m.begin(), m.end());

	for (it = M.begin(); it != M.end(); ++it)
		cout << it->first << " " <<it->second<<endl;
	return 0;	
}

输出:

已存在主键a
a 1
b 2





begin() : 返回容器第一个元素的迭代器

end() : 返回容器“最后一个元素”的迭代器(记住这个迭代器不指向容器中的任何一个元素)

cbegin() ,cend() :返回指向常量元素的迭代器。

rbegin() , rend() :返回反向迭代器。

crbegin() ,crend() :返回反向常量迭代器。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值