繁体   English   中英

如何在C ++中将unordered_map作为函数参数传递?

[英]How do you pass an unordered_map as a function parameter within C++?

我的程序最初设置为通过函数传递数组。 此后,我将其更改为unordered_map。 我如何通过函数传递unordered_map而不是类似的东西:

int function(int array[])

作为参考:

int function(unordered_map<K, T> &map)

像这样:

int function(const std::unordered_map<int, int> &map)

除非您需要修改地图,否则不需使用const。

如果是我的程序,我将输入typedef map:

typedef int result_t;
typedef std::unordered_map<int, result_t> memoization_table_t;

result_t function(memoization_table_t &map)

我只是编造了东西,因为我不知道你在做什么。 typedef使模板化容器更易于使用,并且它们使用描述性类型名来记录代码。

你为什么感到困惑? 只需将int替换为unordered_map<int, int>

int foo(unordered_map<int, int> x)

如果您不需要修改它,则需要使用const引用传递它。

int foo(const unordered_map<int, int> &x)

如果要修改用于调用函数的原始对象,请不要使用const。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM