c++ - 如何将 multimap 传递给函数

标签 c++ function parameter-passing multimap

我有一个非常简单的问题。我只是在学习 map 和多重 map ,想知道如何将它们传递给函数。我大部分时间都在思考多重映射,但想要一个简单的示例来说明如何将它们传递给 void 函数。

int main()
{
multimap<string,int> movies;


movies.insert(pair<string,int>("Happy Feet",6));
movies.insert(pair<string,int>("Happy Feet",4));
movies.insert(pair<string,int>("Pirates of the Caribbean",5));
movies.insert(pair<string,int>("Happy Feet",3));
movies.insert(pair<string,int>("Pirates of the Caribbean",4));
movies.insert(pair<string,int>("Happy Feet",4));
movies.insert(pair<string,int>("Flags of out Fathers",4));
movies.insert(pair<string,int>("Gigli",4));

cout<<"There are "<<movies.count("Happy Feet")<<" instances of "<<"Happy Feet"<<endl;
cout<<"There are "<<movies.count("Pirates of the Caribbean")<<" instances of "<<"Pirates of the Caribbean"<<endl;
cout<<"There are "<<movies.count("Flags of out Fathers")<<" instances of "<<"Flags of out Fathers"<<endl;
cout<<"There are "<<movies.count("Gigli")<<" instances of "<<"Gigli"<<endl;



system("PAUSE");
calculateAverage(movies);  // this is where im getting errors such as no conversions
return 1;
}
void calculateAverage(multimap<string,int> *q)
{
// this function wont calculate the average obviously. I just wanted to test it
int averageH;
int averageP;
int averageF;
int averageG;

averageH = (q->count("Happy Feet"));
averageP = (q->count("Happy Feet"));
averageF = (q->count("Happy Feet"));
averageG = (q->count("Happy Feet"));


};

最佳答案

为什么要通过指针传递?我认为最好传递一个引用(如果 map 应在函数内修改)或引用 const 否则

void calculateAverage(const multimap<string,int> & q)
{
// this function wont calculate the average obviously. I just wanted to test it
int averageH;
int averageP;
int averageF;
int averageG;

averageH = (q.count("Happy Feet"));
averageP = (q.count("Happy Feet"));
averageF = (q.count("Happy Feet"));
averageG = (q.count("Happy Feet"));
};

关于c++ - 如何将 multimap 传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314313/

相关文章:

c - C中二叉树的迭代器方法

linux - bash 中的嵌套默认参数?

c++ - c++ 中的 vector 这么慢吗?

c++ - Eclipse 内容辅助无法识别 std::thread,但可以正确编译

c++ - jbyteArray 是一个 jobject(即 : as a parameter to DeleteLocalRef)?

c - 为什么我调用这个函数8次,结果都是上次的?在 C 中

function - 如何在 MATLAB 中得到 (-8)^0.333333 = -2?

javascript - 不显眼地调用带有参数的函数

c# - 如何将多个参数动态传递到 Asp .Net MVC 中的 Html.Action

c++ - 预览 Microsoft Outlook 对象库生成的电子邮件