如何将多重映射传递到函数中
我有一个非常简单的问题。我刚刚学习地图和多重地图,想知道如何将它们传递到函数中。我的大部分精力都集中在多重映射上,但想要一个关于如何将它们传递到 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"));
};
I have a pretty simple question. Im just learning Maps and multimaps and want to know how to pass them into a function. Ive got most of my mind wrapped around multimaps but would like a quick example on how to pass them into a void function.
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"));
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要通过指针传递?我认为最好传递引用(如果应在函数内修改映射)或对 const 的引用,否则
Why pass by pointer? I think it is better to pass a reference (if the map shall be modified within the function) or reference to const otherwise
通过引用传递:
但是传递指针也没有那么糟糕。只是语法看起来不太好。
如果您选择传递指针,那么在调用站点,您必须使用以下语法:
Pass by reference:
But then passing pointer is not that bad. It's just that syntax doesn't look good.
If you choose to pass pointer, then at the calling site, you've to use this syntax:
在我看来,传递给迭代器,
movies.begin()
和movies.end()
到calculateAverage<更“符合STL的精神”。 /代码> 功能。例如:
具有以下定义:
It seems to me more "in the spirit of the STL" to pass to iterators,
movies.begin()
andmovies.end()
to thecalculateAverage
function. For example:with the following defined:
您正在尝试将
multimap
类型的值作为指向该类型的指针传递,即multimap*
。将函数签名更改为voidcalculateAverage(const multimap& q)
并相应地修改其代码(将->
替换为.< /code>),或者这样调用:
calculateAverage(&movies)
。You are trying to pass a value of type
multimap<string,int>
as a pointer to that type, i.e.multimap<string,int>*
. Either change the function signature tovoid calculateAverage(const multimap<string,int>& q)
and modify its code accordingly (replace->
with.
), or call it like this:calculateAverage(&movies)
.