标签:tor for find 笔试 题意 map 问题 out i++
关于map的按照value排序输出;会错题意,但是华为实习笔试是这个思路,按照map的value排序输出。
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include<vector>
#include<algorithm>
typedef pair<string, int> PAIR;
int cmp (PAIR &x, PAIR &y)
{
return x.second > y.second;
}
//按照输入的顺序……
int main()
{
int n = 0;
string name;
string voter;
while(cin >> n)
{
map<string, int> candidates;
int nVoters = 0;
int invalid = 0;
while(n--)
{
cin >> name;//专门用一个向量来存储输入的顺序,后面遍历向量元素,在map中找到对应的value
candidates[name] = 0;
}
cin >> nVoters;
while(nVoters--)
{
cin >> voter;
if(candidates.find(voter) == candidates.end())
{
invalid ++;
}
else
{
candidates[voter] ++;
}
}
/*
//做一个PAIR类型的向量
vector<PAIR> vec(candidates.begin(), candidates.end());
//对向量排序
sort(vec.begin(),vec.end(),cmp);
//打印输出
for(int i=0;i<vec.size();i++)
{
cout << vec[i].first << " : " << vec[i].second << endl;
}
cout << "Invalid : " << invalid << endl;
candidates.clear();
vec.clear();
/* //明确几个问题:it是一个指针,it->first;而first和second是成员,所以没有括号;对象用的是.运算符:vec[i].first
for(auto it = vec.begin();it != vec.end(); it++)
{
cout << it->first <<
}
*/
*/
}
return 0;
}
标签:tor for find 笔试 题意 map 问题 out i++
原文地址:https://www.cnblogs.com/zlh-1024powr/p/14605039.html