0

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"));


};
1
  • Apologies in advance about the indentation.
    – OVERTONE
    Mar 15, 2011 at 15:51

4 Answers 4

3

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"));
};
4
  • I suppose habit would be my only answer? I always thought there was an advantage to using pass by pointer in regards to memory management. but im still far beyond the level i should be to understand why that is.
    – OVERTONE
    Mar 15, 2011 at 15:59
  • @OVERTONE: Come on! Don't be one of those microoptimizers. A reference is a pointer "deep inside" anyway, so you write semantics, and let the optimizer optimize what is needed. Today's optimizers are impressive :) Mar 15, 2011 at 16:02
  • @Armen Tsirunyan error C3861: 'calculateAverage': identifier not found possibly my definition of the calcaverage method not being usable within the main method. i would usually use classes for these types of things but decided a different approcahe this time.
    – OVERTONE
    Mar 15, 2011 at 16:08
  • this may actually be becuase i didnt define calculateAverage before the main. so its out of scope? yep. that fixed it. my apologies
    – OVERTONE
    Mar 15, 2011 at 16:12
1

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);
2
  • 1
    Or fix the call site: calculateAverage(&movies). Const reference is a better solution still: void calculateAverage(const multimap<string, int>& q) Mar 15, 2011 at 15:55
  • @Alex Well from the bottom of my heart, thank you for the simplicity. that worked like a charm. Im getting another idntifier error now but your answer was by far the easiest to grasp. thanks
    – OVERTONE
    Mar 15, 2011 at 16:06
1

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)
{
...
}
0

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).

1
  • i have tried both. Both work in a sense that their correct but i still get the identifier not found error.
    – OVERTONE
    Mar 15, 2011 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.