如何将多重映射传递到函数中

发布于 2024-10-22 02:25:36 字数 1619 浏览 6 评论 0原文

我有一个非常简单的问题。我刚刚学习地图和多重地图,想知道如何将它们传递到函数中。我的大部分精力都集中在多重映射上,但想要一个关于如何将它们传递到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里°也失望 2024-10-29 02:25:36

为什么要通过指针传递?我认为最好传递引用(如果应在函数内修改映射)或对 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"));
};

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

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"));
};
允世 2024-10-29 02:25:36

通过引用传递:

void calculateAverage(const multimap<string,int> & q)

但是传递指针也没有那么糟糕。只是语法看起来不太好。

如果您选择传递指针,那么在调用站点,您必须使用以下语法:

calculateAverage(&movies);

Pass by reference:

void calculateAverage(const multimap<string,int> & q)

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:

calculateAverage(&movies);
扛刀软妹 2024-10-29 02:25:36

在我看来,传递给迭代器,movies.begin()movies.end()calculateAverage<更“符合STL的精神”。 /代码> 功能。例如:

calculateAverage(movies.begin(),movies.end());

具有以下定义:

typedef multimap<string,int>::const_iterator MapIt;
void calculateAverage(const MapIt &begin, const MapIt &end)
{
...
}

It seems to me more "in the spirit of the STL" to pass to iterators, movies.begin() and movies.end() to the calculateAverage function. For example:

calculateAverage(movies.begin(),movies.end());

with the following defined:

typedef multimap<string,int>::const_iterator MapIt;
void calculateAverage(const MapIt &begin, const MapIt &end)
{
...
}
空城缀染半城烟沙 2024-10-29 02:25:36

您正在尝试将 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 to void calculateAverage(const multimap<string,int>& q) and modify its code accordingly (replace -> with .), or call it like this: calculateAverage(&movies).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文