map浅谈 Easy problem UVa 11991

本文详细介绍了C++编程中map容器的使用方法,包括构造、插入、查找、遍历、删除等基本操作,并通过实例展示了如何在实际项目中应用map容器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 STL中的list就是一双向链表,可高效地进行插入删除元素。
1.map 中内部有序,由红黑树保证,因此很多函数执行的时间复杂度都是log2N。Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个是该关键字的值)的数据处理功能。
2.map的构造函数
一般是Map<int, string> mapStudent;
3.数据的插入:
第一种:用insert函数插入pair数据
    map<int, string> mapStudent;
    mapStudent.insert(pair<int, string>(1, "student_one"));
第二种:用insert函数插入value_type数据
    map<int, string> mapStudent;
    mapStudent.insert(map<int, string>::value_type (1, "student_one"));
第三种:用数组方式插入数据
    map<int, string> mapStudent;
    mapStudent[1] = "student_one";
    mapStudent[2] = "student_two";
用insert插入数据,当map中的关键字存在时,insert操作是插入数据不了的。用数组方式就不同了,它可以覆盖以前该关键字对应的值。
4. map的大小
在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数:
    Int nSize = mapStudent.size();


5. 数据的遍历
第一种:应用前向迭代器
    map<int, string>::iterator iter;
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
        Cout<<iter->first<<"        "<<iter->second<<end;
第二种:应用反相迭代器
    map<int, string>::reverse_iterator iter;
    for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
        Cout<<iter->first<<"        "<<iter->second<<end;
第三种:用数组方式
    int nSize = mapStudent.size()
    for(int nIndex = 1; nIndex <= nSize; nIndex++) 
        Cout<<mapStudent[nIndex]<<end;
6. 数据的查找(包括判定这个关键字是否在map中出现)
这里给出三种数据查找方法
第三种:用数组方式
    int nSize = mapStudent.size()
    for(int nIndex = 1; nIndex <= nSize; nIndex++) 
        Cout<<mapStudent[nIndex]<<end;
第一种:用count函数来判定关键字是否出现,但是无法定位数据出现位置。出现的情况count函数返回1,或者0
第二种:用find函数来定位数据出现位置它返回的一个迭代器,     出现的情况find函数返回的是一个迭代器,参数是关键字 
当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
Int main()
{
    Map<int, string> mapStudent;                                     map<int, string>:: iterator ite; for(ite=mapStudent.begin(); 
    mapStudent.insert(pair<int, string>(1, “student_one"));  
    mapStudent.insert(pair<int, string>(2, “student_two"));
    mapStudent.insert(pair<int, string>(3, “student_three"));
    map<int, string>::iterator iter;
    iter = mapStudent.find(1);
    if(iter != mapStudent.end())
    {
        Cout<<"Find, the value is "<<iter->second<<endl;
    }
    Else
    {
        Cout<<"Do not Find"<<endl;
    }
}
7. 数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map
8. 数据的删除
这里要用到erase函数,它有三个重载了的函数
迭代器删除 
    iter = mapStudent.find(1);
    mapStudent.erase(iter);
用关键字删除
    Int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
9.Map中的元素是自动按key升序排序,所以不能对map用sort函数:


这道题可能大家都有自己的思路,我看白皮书上是用map写的,刚好可以熟悉一下,我写了一遍;

代码如下:

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
using namespace std;
map< int,vector<int> > map;
int main() {
	int n,i,k,v;
	cin>>n;
	map.clear();
	for(i=1; i<=n; i++) {
		cin>>v;
		if(!map.count(v)) map[v] = vector<int> ();
		map[v].push_back(i);
	}
	cin>>k>>v;
	if(map[v].size()<k || !map.count(v)) cout<<0<<endl;
	else cout<<map[v][k-1]<<endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值