25
A
回答
22
那麼這取決於你想如何顯示它們,但你總是可以輕鬆地重複他們:
typedef map<string, list<string>>::const_iterator MapIterator;
for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++)
{
cout << "Key: " << iter->first << endl << "Values:" << endl;
typedef list<string>::const_iterator ListIterator;
for (ListIterator list_iter = iter->second.begin(); list_iter != iter->second.end(); list_iter++)
cout << " " << *list_iter << endl;
}
10
我想嘗試以下
void dump_list(const std::list<string>& l) {
for (std::list<string>::const_iterator it = l.begin(); l != l.end(); l++) {
cout << *l << endl;
}
}
void dump_map(const std::map<string, std::list<string>>& map) {
for (std::map<string,std::list<string>>::const_iterator it = map.begin(); it != map.end(); it++) {
cout << "Key: " << it->first << endl;
cout << "Values" << endl;
dump_list(it->second);
}
4
我有點偏離主題這裏...
我想你想轉儲地圖內容進行調試。我想提一提的是,下一個gdb版本(版本7.0)將包含一個內置的python解釋器,gcc libstdC++將使用它來提供漂亮的打印機。這裏是你的情況
#include <map>
#include <map>
#include <list>
#include <string>
using namespace std;
int main()
{
typedef map<string, list<string> > map_type;
map_type mymap;
list<string> mylist;
mylist.push_back("item 1");
mylist.push_back("item 2");
mymap["foo"] = mylist;
mymap["bar"] = mylist;
return 0; // stopped here
}
導致
(gdb) print mymap
$1 = std::map with 2 elements = {
["bar"] = std::list = {
[0] = "item 1",
[1] = "item 2"
},
["foo"] = std::list = {
[0] = "item 1",
[1] = "item 2"
}
}
耶的例子!
2
另一種形式,採用<algorithm>
:
void printPair(const pair<string, list<string> > &p)
{
cout << "Key: " << p.first << endl;
copy(p.second.begin(), p.second.end(), ostream_iterator<string>(cout, "\n"));
}
for_each(mapex.begin(), mapex.end(), printPair);
測試程序:
#include <iostream>
#include <map>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
void printPair(const pair<string, list<string> > &p)
{
cout << "Key: " << p.first << endl;
copy(p.second.begin(), p.second.end(), ostream_iterator<string>(cout, "\n"));
}
int main()
{
map<string, list<string> > mapex;
list<string> mylist1;
mylist1.push_back("item 1");
mylist1.push_back("item 2");
mapex["foo"] = mylist1;
list<string> mylist2;
mylist2.push_back("item 3");
mylist2.push_back("item 4");
mylist2.push_back("item 5");
mapex["bar"] = mylist2;
for_each(mapex.begin(), mapex.end(), printPair);
}
27
更新(回到未來):用C++ 11範圍基於for循環 -
std::map<Key, Value> m { ... /* initialize it */ ... };
for (const auto &p : m) {
std::cout << "m[" << p.first << "] = " << p.second << '\n';
}
0
您可以編寫一個非常通用的重載函數,這有兩個用途:
- 它適用於任何
map
。 - 它允許使用
<<
。
功能是
template<class key_t, class value_t>
ostream& operator<<(ostream& os, const map<key_t, value_t>& m) {
for (typename map<key_t, value_t>::const_iterator it = m.begin();
it != m.end(); it++) {
os << "Key: " << it->first << ", Value: " << it->second;
}
return os;
}
cout <<
將與該<<
用於typename
key_t
小號和value_t
定義的任何map
工作。在你的情況下,這沒有爲value_t
(= list<string>
)定義,所以你也必須定義它。 本着同樣的精神,你可以使用
template<class T>
ostream& operator<<(ostream& os, const list<T>& l) {
for (typename list<T>::const_iterator it = l.begin(); it != l.end(); it++) {
os << "\"" << *it << "\", ";
}
return os;
}
所以,你可以:
- 添加這兩種功能。
- 在需要的地方添加原型。
- 使用
using namespace std;
(或根據需要添加std::
)。 - 使用,例如,
cout << mapex << endl;
cout << li << endl;
請記住,如果沒有爲<<
的只有我定義的任何其他可行的候選者(我拿不存在,否則你可能不會問這個問題),它可能會被優先超過目前的。
相關問題
- 1. Google地圖只顯示部分內容
- 2. 如何在Google地圖上顯示某些內容?
- 3. 如何顯示sqlite內容
- 4. 如何顯示pdf內容?
- 5. 如何在圖像顯示時顯示某些內容?
- 6. 如何在jQuery中顯示內容之前顯示setTimeout圖片?
- 7. 谷歌地圖WordPress中的Jquery不顯示任何內容
- 8. Google地圖畫布上沒有任何內容顯示
- 9. Google地圖活動在api23上不顯示任何內容
- 10. 谷歌地圖彈出窗口不顯示任何內容
- 11. 如何在顯示內容之前顯示內容圖像不加載的隱藏內容?
- 12. 空視圖不顯示任何內容
- 13. 如何使圖像內容覆蓋圖顯示在圖像上?
- 14. 顯示內容
- 15. 顯示內容
- 16. 顯示內容
- 17. 顯示內容
- 18. nerdinner如何顯示地圖?
- 19. 錯誤地顯示xls的內容
- 20. 以當地語言顯示內容:R
- 21. Marionette LayoutView地區 - 顯示非ItemView內容
- 22. JDialogBox不間斷地顯示內容
- 23. Python:維恩圖:如何顯示圖內容?
- 24. 如何在內容沒有圖像時顯示圖像並在顯示圖像時顯示圖像
- 25. 如何顯示當前地點2.5英里內的地圖?
- 26. Drupal - 顯示不同地區的內容類型的內容
- 27. 如何在windows phone7中顯示印地文內容?
- 28. 如何使用PHP很好地顯示snmpwalk()的內容?
- 29. 如何使用RichTextBox顯示html內容?
- 30. 如何顯示wordpress頁面內容?
在第一行得到預期的非限定id之前<'token':void dump_list(const std :: list&l){`。我必須包括一些東西嗎? –
2013-01-15 14:33:38
在dump_list中應該是這樣的: std :: vector :: const_iterator it = l.begin(); (It; it!= l.end(); ++ it){ std :: cout << * it << std :: endl; } 而不是在cout!否則這對我很有幫助 /謝謝! –
Kahin
2015-01-29 14:52:50