C++ 如何创建返回类型为 map<> 的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2974855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:38:20  来源:igfitidea点击:

How to create a function with return type map<>?

c++functionmapreturn

提问by IAE

Fairly straightforward question. I have a map that I wish to initialize by calling a function like so:

相当直接的问题。我有一个地图,我希望通过调用这样的函数来初始化它:

map<string, int> myMap;

map<string, int> myMap;

myMap = initMap( &myMap );

myMap = initMap( &myMap );

map<string, int> initMap( map<string, int> *theMap )
{
    /* do stuff... */

However, the compiler is moaning. What's the solution to this?

但是,编译器正在呻吟。解决这个问题的方法是什么?

EDIT 1:

编辑 1:

I'm sorry, but I screwed up. The code was correctly written with *theMap, but when I posted the question, I failed to notice that I had omitted the *. So to answer the comment, the error message I get is:

对不起,我搞砸了。代码是用 正确编写的*theMap,但是当我发布问题时,我没有注意到我省略了*. 所以要回答评论,我得到的错误信息是:

1>Roman_Numerals.cpp(21): error C2143: syntax error : missing ';' before '<'

1>Roman_Numerals.cpp(21): error C2143: syntax error : missing ';' before '<'

which is thrown at

扔在

map<char, int> initMap( map<char, int> *numerals );

map<char, int> initMap( map<char, int> *numerals );

using VC++ 2010 Express and the same error again when I define the function.

使用 VC++ 2010 Express 并在我定义函数时再次出现相同的错误。

回答by jilles de wit

Either do:

要么做:

map<string, int> myMap;
initMap( myMap );

void initMap( map<string, int>& theMap )
{
    /* do stuff in theMap */
}

or do:

或做:

map<string, int> myMap;
myMap = initMap(  );

map<string, int> initMap()
{
    map<string, int> theMap;
    /* do stuff in theMap */
    return theMap;
}

i.e. let the function initialise the map you give it, or take the map the function gives you. You're doing both (without a returnstatement too!)

即让函数初始化你给它的地图,或者获取函数给你的地图。你两者都在做(也没有return声明!)

I'd go for the first option.

我会选择第一个选项。

回答by Fred Larson

It's probably complaining because you're passing the address of the map, but your function accepts the map by value.

它可能会抱怨,因为您正在传递地图的地址,但您的函数按值接受地图。

You might want something more like this:

你可能想要更像这样的东西:

void initMap(map<string, int>& theMap)
{
    /* do stuff...*/
}

回答by MSalters

The canonical solution is just

规范的解决方案只是

std::map<std::string, int> initMap();
// ...
std::map<std::string, int> myMap = initMap();

Why the tricky attempt to use an input parameter for a return value? Performance? Modern compilers don't care. In fact, not constructing an empty map will be slightly faster.

为什么要使用输入参数作为返回值的棘手尝试?表现?现代编译器不在乎。事实上,不构建空地图会稍微快一些。

回答by AraK

You should accept a pointer or preferably a reference to the map. You could also return a reference for convenience:

您应该接受一个指针或最好是对地图的引用。为方便起见,您还可以返回参考:

map<string, int>& initMap( map<string, int>& theMap )
...
// Call initMap
map<string, int> my_map;
initMap(my_map);

回答by Chris Card

why not do void initMap(map& theMap), instead of making so many copies of the map?

为什么不做void initMap(map& theMap),而不是制作这么多的地图副本?

回答by metaliving

&myMapis a pointer to a map object, while the argument theMapis a map object.

&myMap是一个指向地图对象的指针,而参数theMap是一个地图对象。

Two solutions:

两种解决方案:

Change myMap = initMap( &myMap );to myMap = initMap( myMap );.

更改myMap = initMap( &myMap );myMap = initMap( myMap );

or

或者

Change map<string, int> initMap( map<string, int> theMap )to map<string, int> initMap( map<string, int> * theMap ).

更改map<string, int> initMap( map<string, int> theMap )map<string, int> initMap( map<string, int> * theMap )

回答by MTGradwell

A bit late to the game but: I would guess from the error message that you're missing out

游戏有点晚了但是:我会从错误消息中猜测您错过了

#include <map>

at the top of your code. So the compiler doesn't know that map is supposed to be a template, and it consequently gets confused by the angle brackets which follow.

在代码的顶部。所以编译器不知道 map 应该是一个模板,因此它会被后面的尖括号混淆。

回答by Vedang Joshi

 void initMap(map<String,int> &Map)
 {
   //Do something
 }