2011-03-15 174 views
0

我有一個非常簡單的問題。我只是學習地圖和multimaps,並想知道如何將它們傳遞給一個函數。我的大部分想法都是圍繞着multimap進行的,但想要一個關於如何將它們傳遞給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")); 


}; 
+0

道歉提前約縮進。 – OVERTONE 2011-03-15 15:51:25

回答

3

爲什麼通過指針傳遞?我覺得這是更好地傳遞引用(如果映射應在函數內部進行修改)或引用的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")); 
}; 
+0

我想習慣會是我唯一的答案?我一直認爲在內存管理方面使用pass指針是有優勢的。但我仍然遠遠超出了我應該理解的原因。 – OVERTONE 2011-03-15 15:59:26

+0

@OVERTONE:加油!不要成爲那些微型優化器之一。無論如何,引用是一個「深入內部」的指針,因此您可以編寫語義,並讓優化器優化需要的內容。今天的優化器令人印象深刻:) – 2011-03-15 16:02:12

+0

現在即時通訊標識符未找到錯誤。嗯。 – OVERTONE 2011-03-15 16:03:00

1

通過引用傳遞:

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

但後來通過指針並不壞。只是語法看起來不太好。

如果選擇通過指針,然後向主叫網站,你使用這個語法:

calculateAverage(&movies); 
+1

或修復通話網站:'calculateAverage(&movies)'。 const引用仍然是一個更好的解決方案:'void calculateAverage(const multimap &q)' – 2011-03-15 15:55:40

+0

@Alex從我的內心深處,感謝您的簡單。像魅力一樣工作。我現在得到另一個標識錯誤,但你的答案是最容易掌握的。謝謝 – OVERTONE 2011-03-15 16:06:28

0

您正在試圖通過multimap<string,int>類型的值作爲一個指針類型,即multimap<string,int>*。將函數簽名更改爲​​並相應地修改其代碼(將->替換爲.),或者將其稱爲:calculateAverage(&movies)

+0

我已經嘗試了兩個。這兩個工作在某種意義上說,他們正確的,但我仍然得到標識符找不到錯誤。 – OVERTONE 2011-03-15 16:05:25

1

在我看來,更多的「在STL的精神」傳遞給迭代器,movies.begin()movies.end()calculateAverage函數。例如:

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

與以下定義:

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