Map函数与vector<pair<int,int>>函数的使用方法

一、两者之间的区别:

vector<pair<int,int>>用法和Map函数的异同点:

答:map会对插入的元素按键自动排序,而且不允许键重复;vector的这种用法不会自动排序,而且允许重复。

二、使用命令:

(1)、vector<pair<int,int>>使用方法:

vector<pair<int,int>> 可以使用vector的方法;

使用vector 时,要导入include< vector > 头文件:

初始化:vector<int> result(nums.size(), 0);

1.push_back 将数据放入vector中

2.pop_back 去掉末尾元素

3.at 得到对应下标的元素

4.begin 得到数组头的指针

5.end 得到数组的最后一个单元+1的指针

6.front 返回数组第一个元素

7.back 返回最后一个元素

8.max_size 得到vector最大可以是多大

9.capacity 当前vector分配的大小

10.size 当前使用数据的大小

11.resize 改变当前使用数据的大小,如果它比当前使用的大,则填充默认值

12.reserve 改变当前vecotr所分配空间的大小

13.erase 删除指针指向的数据项

14.clear 清空当前的vector

15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)

16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)

17.empty 判断vector是否为空

18.swap 与另一个vector交换数据

vector<int>::iterator 迭代器名; 常用语遍历vector

注意事项:

1.要注意end方法,其放回的并不是最后一个元素的指针,而是最后一个元素后一位的指针。

2.使用每个元素和使用数组时一样,可以直接用下标访问#include <iostream>

3.迭代器使用示例:

cpp 复制代码
#include <vector>
using namespace std;

int main(int argc, char** argv) 
{
	vector<int> a;
	a.push_back(3);
	a.push_back(4);
	vector<int>::iterator it;
	for(it=a.begin();it!=a.end();it++){
		printf("%d\n",*it);
	}
	return 0;
}

4、使用sort()函数对vector中的元素排序,要导入algorithm:

cpp 复制代码
#include <algorithm>
vector<int> nums;
sort(nums.begin(),nums.end());  //sort函数默认升序排序

(2)、map命令:

map 在插入元素时,通过比较键(key)的大小进行排序,实现二叉树自平衡。内部是自动排序的。但是不允许有相同的键。

如果想使用相同的键,那么就要使用 multimap,其可以存放相同的键。

map不能通过迭代器改变键。

基本命令:

map<type,type> strMap; //创建 map

map添加数据的方法:

1.pair<int,string> value(1,"a"); map.insert(value);

2.map.insert(pair<type,type>(data,data));

// value_type是固定的写法,不表示数值类型。

3.map.insert(map<type,type>::value_type(data,data));

4.map[1]="a";

erase() //删除一个元素

clear() //删除所有元素

size() //返回map中元素的个数

count() //某个元素出现的次数

find() //查找某个键

empty() //如果map为空则返回true

begin() //指向map头部

end() //指向map最后一个元素的下一个位置

rbegin() //指向map尾部

rend() //指向map头部

swap() //交换两个map

lower_bound() //找出第一个小于等于查找值的位置

upper_bound() //找出第一个大于查找值的位置

map<type,type>::iterator ite; //迭代器

ite->first和ite->second //取出键和值

迭代器:

cpp 复制代码
map<int,const char*>::iterator ite;
//遍历所有元素
for(ite=m.begin();ite!=m.end();++ite)
{
    printf("%d:%s\n",ite->first,ite->second);
}

(3)、multimap命令:

使用方式和map一致,但其支持重复的键,在插入时不允许使用数组方式的插入,也就是上文方法中添加数据的第4种方式。

示例:

cpp 复制代码
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main(int argc, char** argv)
{
	//声明(int为键,const char*为值)
	multimap<int,const char*> m;
	
	//插入元素
	m.insert(pair<int,const char*>(1,"ONE"));
	m.insert(pair<int,const char*>(1,"one"));
	m.insert(map<int,const char*>::value_type(2,"TWO"));
	m.insert(map<int,const char*>::value_type(2,"two"));
	m.insert(make_pair(10,"TEN"));
	m.insert(make_pair(10,"ten"));
//	m[100]="HUNDRED";
//	m[100]="hundred";   //不支持,会报错
	
	//遍历所有元素
	for(ite=m.begin();ite!=m.end();++ite)
    {
	    printf("%d:%s\n",ite->first,ite->second);
	} 
	
	return 0;
}
相关推荐
虚拟之1 分钟前
36、stringstream
c++
我很好我还能学6 分钟前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
jiunian_cn7 分钟前
【Linux】centos软件安装
linux·运维·centos
芜湖xin17 分钟前
【题解-洛谷】P1706 全排列问题
算法·dfs
程序员JerrySUN18 分钟前
[特殊字符] 深入理解 Linux 内核进程管理:架构、核心函数与调度机制
java·linux·架构
孤寂大仙v21 分钟前
【计算机网络】非阻塞IO——select实现多路转接
linux·计算机网络
派阿喵搞电子1 小时前
Ubuntu下有关UDP网络通信的指令
linux·服务器·网络
Evan_ZGYF丶1 小时前
【PCIe总线】 -- PCI、PCIe相关实现
linux·嵌入式·pcie·pci
舰长1151 小时前
Ubuntu挂载本地镜像源(像CentOS 一样挂载本地镜像源)
linux·ubuntu·centos
程序员JerrySUN1 小时前
全面理解 Linux 内核性能问题:分类、实战与调优策略
java·linux·运维·服务器·单片机