map相关

/*
map的find函数,当key不存在时 
1.仅使用find(key)返回迭代器 ,则返回mp.end()
2.使用mp[key]时,则创建一个<key,形式0>的元素对 
3.使用mp.find(key)->second时其值为形式0,但未创建元素对 
*/

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

int main()
{
	map<int,int> m;
	m[3] = 3;
	
	//结论1 
	if(m.end() == m.find(1))
		cout<<"未找到键为1的迭代器"<<endl;
	cout<<m.find(1)->second<<endl;		//此时未创建<1,0>的元素对
	//结论2 
	if(m.end() == m.find(1))
		cout<<"未找到键为1的迭代器"<<endl<<endl<<endl; 
	
	//结论3 
	cout<<m[2]<<endl;					//此时创建了<2,0>元素对 
	if(m.end() != m.find(2))
		cout<<"能找到键为2的迭代器";
	
	return 0;
}

2.map的键唯一。
当key为struct结构体时,要重载小于号运算符

bool operator <(const node &nd)const
	{
		return x==nd.x?y<nd.y:x<nd.x;
	}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

struct node
{
	int x,y;
	node(int _x, int _y):x(_x), y(_y) {}
	node(){}
	bool operator <(const node &nd)const
	{
		return x==nd.x?y<nd.y:x<nd.x;
	}
		
}Node;

int n;
map<node, int>mp;
vector<node>vt;
int ans[5] = {0};
int deltaX[] = {0,0,1,-1};
int deltaY[] = {1,-1,0,0};
int X[] = {-1,-1,1,1};
int Y[] = {1,-1,1,-1};

bool judge(node nd)	//判断当前坐标能否是垃圾站
{
	int cnt = 0;
	for(int i=0; i<4; i++)
	{
		node temp = node(nd.x+deltaX[i], nd.y+deltaY[i]);
		if(mp[temp])
			cnt++;
	}
	return 4==cnt;
}
		
void solve(node nd)	//计算当前垃圾点站的得分
{ 
	int cnt = 0;
	for(int j=0; j<4; j++)
	{
		node temp = node(nd.x+X[j], nd.y+Y[j]);
		if(mp[temp])		//巧 
			cnt++;
	}
	ans[cnt]++;
}

int main()
{
	scanf("%d", &n);
	for(int i=0; i<n; i++)
	{
		scanf("%d%d", &Node.x, &Node.y);
		vt.push_back(Node);
		mp[Node] = 1;
	}
	
	for(int i=0; i<n; i++)
	{
		if(judge(vt[i]))
			solve(vt[i]);
	}
	
	for(int i=0; i<5; i++)
		cout<<ans[i]<<endl;
		
	return 0;
} 
		

3.STL标准中没有hashmap,可用unordered_map。
4.multimap、unordered_multimap中无下标[]运算符(有重复的),故只能用insert(make_pair(s,5));而不能mp[‘s’] = 5;
multimap<int,int>::iterator it =hmap.lower_bound(delta);
multimap<int,int>::iterator it = hmap.upper_bound(delta);

unordered_multimap中没有lower_bounder和upper_bounder函数(无序)。只有find(key),返回第一个元素对的迭代器,可以++;

#include <iostream>
using namespace std;
#include <map>
#include <string>
 
typedef multimap<string, string> M;
 
int main()
{
	M mss;
	M::iterator ib ,ie;
	mss.insert(make_pair("aa", "bb"));
	mss.insert(make_pair("cc", "dd"));
	mss.insert(make_pair("ee", "ff"));
	mss.insert(make_pair("cc", "pp"));
	mss.insert(make_pair("aa", "mm"));
	mss.insert(make_pair("cc", "dd"));
	mss.insert(make_pair("aa", "kk"));
	
	ib = mss.begin();
	ie = mss.end();
	while(ib!=ie)
	{
		cout << ib->first << "的好友" << ib->second << endl;
		++ib; 
	}
	cout << "aa的好友数量:" << mss.count("aa") << endl; 
	cout << "cc的所有好友:" << endl;
	ib = mss.lower_bound("cc");
	ie = mss.upper_bound("cc");
	while(ib!=ie)
	{
		cout << ib->second<< ' ';
		++ib;
	}
	cout << endl;
	
	return 0;
}

在这里插入图片描述

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> ans;
        unordered_map<int, int> hmap;
        for(int i=0; i<nums.size(); i++)
        	hmap[nums[i]] = i;
        for(int i=0; i<nums.size(); i++)
        {
        	int delta = target-nums[i];
            //解决了[1,3] 6和[1,3,3] 6的问题,i=1时,delta=3,hmap[delta]==2
        	if(hmap.end()!=hmap.find(delta) && hmap[delta]!=i)
        	{
        		ans.push_back(i);
        		ans.push_back(hmap[delta]);
        		break;
        	}
        }
        return ans;
    }
};
/*
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> ans;
        unordered_multimap<int, int> hmap;
        for(int i=0; i<nums.size(); i++)
        	hmap.insert(make_pair(nums[i],i));
        for(int i=0; i<nums.size(); i++)
        {
        	int delta = target-nums[i];
        	if(hmap.find(delta)!=hmap.end())
        	{
        		if((nums[i]!=delta))
        		{
        			ans.push_back(i);
        			unordered_multimap<int,int>::iterator it = hmap.find(delta);
        			ans.push_back(it->second);
        			break;
        		}
                //解决[1,3] 6和[1,3,3] 6问题 
        		else if(hmap.count(delta)>=2)
        		{
        			ans.push_back(i);
        			//无序没有lower_bound函数 
        			unordered_multimap<int,int>::iterator it = hmap.find(delta)++;
        			ans.push_back(it->second);
        			break;
        			
        		}
        	}
        }
        return ans;
    }
};
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值