multimap 能接受一key对应多value的情况
multimap 会自动按key值排序,而value则按输入顺序排序,要想把一个key对应的多个value排序
必须自己构造排序函数
typedef pair<string, string> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
if(lhs.first==rhs.first)
return lhs.second<rhs.second;
else return lhs.first<rhs.first;
using namespace std;
typedef pair<string, string> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
if(lhs.first==rhs.first)
return lhs.second<rhs.second;
else return lhs.first<rhs.first;
}
int main()
{
int zz;
string s,k;
multimap<string,string>b;
multimap<string,string>bc;
multimap<string,string>kk;
while(getline(cin,s))
{
if(s=="END")
{
break;
}
else
{
string x,y;
x=s.substr(0,s.find_last_of("\"")+1);
y=s.substr(s.find_last_of("\"")+5);
b.insert(make_pair(y,x));
multimap 会自动按key值排序,而value则按输入顺序排序,要想把一个key对应的多个value排序
必须自己构造排序函数
typedef pair<string, string> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
if(lhs.first==rhs.first)
return lhs.second<rhs.second;
else return lhs.first<rhs.first;
}//这个挺重点的
///
本渣渣的想法是 :
先提取出输入的书名人名,利用了 substr 以及 find(借鉴了网上大佬的)
然后先按照人名排序 ,在把 人名 相同的按照 署名排序 ,用到了上述构造的排序函数(大家可以参考网上大佬的教学)
网址:https://blog.csdn.net/yutianxin123/article/details/52454252
上面都是基础的没基础有思路也做不动233
晚上上述任务后,再进行二次输入,我的做法先把原数组复制一遍,borrow删除原数组 元素后成为数组s1,return创建新数组s2
shelves归还,每次归还换一本s2消除一个元素,s1添加一个元素,同时对s1进行 再排序 防止s1相同key值 顺序错了,就这样子,代码这么冗长都是因为在排序问题,毕竟要进行输入再到vector再到排序再到重新赋值,这法用了7次每次7行....,如果有人能优化一下,请反馈一下,谢谢.还有网上方法比我的简单.
#include <bits/stdc++.h>using namespace std;
typedef pair<string, string> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
if(lhs.first==rhs.first)
return lhs.second<rhs.second;
else return lhs.first<rhs.first;
}
int main()
{
int zz;
string s,k;
multimap<string,string>b;
multimap<string,string>bc;
multimap<string,string>kk;
while(getline(cin,s))
{
if(s=="END")
{
break;
}
else
{
string x,y;
x=s.substr(0,s.find_last_of("\"")+1);
y=s.substr(s.find_last_of("\"")+5);
b.insert(make_pair(y,x));