如何返回 std::pair 的内容? [英] How to return the content of std::pair?
本文介绍了如何返回 std::pair 的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试返回具有 int
和 string
值的 std::pair
的内容.我应该保留什么返回类型的函数?
I am trying to return content of std::pair
which has int
and string
values.
What return type for function should I keep?
我尝试使用 int
和 char
返回类型,但两者都出现错误.我在下面给出了错误:
I tried with both int
and char
return type but gives error for both. I have given error below:
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
std::pair<int,std::string>client()
{
std::vector<std::string> most { "lion","tiger","kangaroo",
"donkey","lion","tiger",
"lion","donkey","tiger"
};
std::map<std::string, int> src;
for(auto x:most)
++src[x];
std::multimap<int,std::string,std::greater<int> > dst;
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
[] (const std::pair<std::string,int> &p) {
return std::pair<int,std::string>(p.second, p.first);
}
);
std::multimap<int,std::string>::iterator it = dst.begin();
for(int count = 0;count<3 && it !=dst.end();++it,++count)
std::cout<<it->second<<":"<<it->first<<std::endl;
return *it;
}
int main()
{
std::multimap<int,std::string>::const_iterator rec;
rec= client(); // Error no match for ‘operator=’ in ‘rec = client()()’
std::multimap<int,std::string>::iterator it = rec.begin(); //error: ‘std::multimap<int, std::basic_string<char> >::const_iterator’ has no member named ‘begin’
for(int count = 0;count<3 && rec !=it.end();++it,++count) // error: has no member named 'end'
std::cout<<rec->second<<":"<<rec->first<<std::endl;
}
推荐答案
只需返回一对 int
&std::string
本身因为 multimap
包含它作为它的元素
Just return the pair of int
& std::string
itself
as the multimap
contains that as its element
std::pair<int,std::string> client(){
//...
}
这篇关于如何返回 std::pair 的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文