如何从函数返回pair
how to return pair from a function?
我正试图编写一个函数,它将从函数返回对,但我在编译期间得到错误。
This is the whole file:
#include <iostream>
#include <map>
#include <utility>
using namespace std;
typedef pair<const string, const double> pr;
typedef map<const string,pr > mpr;
mpr mymap;
pr getvalue(const string s)
{
pr pValue;
mpr::iterator iter = mymap.find(s);
if(iter not_eq mymap.end())
{
pValue = (*iter).second;
}
return pValue;
}
int main( )
{
getvalue("test");
}
错误信息:
文件包括从/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../包括/c++/4.4.7/位/stl_algobase.h: 66年,从/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../包括/c++/4.4.7/位/char_traits.h: 41岁从/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../包括/c++/4.4.7/ios: 41岁从/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../包括/c++/4.4.7/上ostream: 40岁从/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../包括/c++/4.4.7/iostream: 40岁从test8.cxx: 1:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/…/…/…//include/c++/4.4.7/bits/stl_pair.h:在成员函数' std::pair, std::allocator>, const double>&Std::pair, Std::allocator>, const double>::operator=(const Std::pair, Std::allocator>, const double>&) ':/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../..//include/c++/4.4.7/bits/stl_pair.h:68:错误:非静态const成员' const std::basic_string, std::allocator> std::pair, std::allocator>, const double>::first ',不能使用默认赋值操作符/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../..//include/c++/4.4.7/bits/stl_pair.h:68:错误:非静态const成员' const double std::pair, std::allocator>, const double>::second ',不能使用默认赋值操作符test8。在函数"pr getvalue(std::string)"中:test8。Cxx:14:注释:合成方法' std::pair, std::allocator>, const double>&Std::pair, Std::allocator>, const double>::operator=(const Std::pair, Std::allocator>, const double>&) '这里首先需要
请帮帮我。
类型pr
定义为对的两个成员都是const
。变量pValue
一旦声明,就不能在赋值pValue = (*iter).second
中更改,因为它的所有成员实际上都是const。
代码可以修改为(应该编译);
pr getvalue(const string s)
{
mpr::iterator iter = mymap.find(s);
if(iter not_eq mymap.end())
{
return (*iter).second;
}
return pr();
}
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- 是否需要删除包含对象的"pair"?
- 迭代从 SWIG 绑定返回的 python 中的 vector<pair<int,int>>
- 队列返回错误的 pair.first 值
- C++错误处理-使用std::pair或std::tuple返回错误代码和函数返回的缺点
- 返回 vector<pair<int,int>> & from c++ 方法到 python 使用 swig typemap 的元组列表
- 如何返回std::pair的内容
- pair.first是否返回对第一个值的引用
- 如何从函数返回pair
- 从函数返回时std::pair秒的奇怪行为