2013-04-21 342 views
-2

是否可以將映射存儲在數組中?你能幫我嗎我該怎麼做? 很明顯,我可以存儲例如int,但我不知道如何使用地圖來做到這一點。將數組映射到數組C++

謝謝!

+0

只需嘗試一下。你如何聲明一個'int'數組?你如何聲明一個「地圖」? – juanchopanza 2013-04-21 10:00:04

回答

1

你可以用完全相同的方式做到這一點。假設你想映射來自std::stringint

std::map<std::string, int> array[10]; 

這將使你的那些地圖的10陣列。你可能更願意使用std::array容器,它封裝了一個數組:

std::array<std::map<std::string, int>, 10> array; 

或者,如果你希望能夠添加地圖,動態調整數組,你可以使用一個std::vector

std::vector<std::map<std::string, int>> array; 
+0

它看起來不錯,但是當我嘗試使用它時,代碼塊寫入「error:'vector'不是std的成員」。不知道可能是什麼問題? – 2013-04-21 10:10:09

+0

@PeterSmith #include 會有所幫助。 – 2013-04-21 10:10:35

+0

很酷,謝謝! :) – 2013-04-21 10:13:42

0

我建議你爲此使用std::vector。然後,您可以輕鬆地創建地圖的載體:

std::vector<std::map<std::string, int> > maps; 

注意,沒有必要在C++ 11 >字符之間的額外空間。

+0

謝謝,它也可以。 :) – 2013-04-21 10:14:44