C++ 如何在控制台上显示地图的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1063453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display the content of a map on the console?
提问by Cute
I have a map
declared as follows:
我map
声明如下:
map < string , list < string > > mapex ; list< string > li;
How can I display the items stored in the above map on the console?
如何在控制台上显示存储在上述地图中的项目?
采纳答案by Skurmedel
Well it depends on how you want to display them, but you can always iterate them easily:
好吧,这取决于您想如何显示它们,但您始终可以轻松地迭代它们:
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;
}
回答by The Paramagnetic Croissant
Update (Back to the future): with C++11 range-based for loops –
更新(回到未来):使用 C++11 基于范围的 for 循环 –
std::map<Key, Value> m { ... /* initialize it */ ... };
for (const auto &p : m) {
std::cout << "m[" << p.first << "] = " << p.second << '\n';
}
回答by JaredPar
I'd try the following
我会尝试以下
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);
}
回答by Maik Beckmann
I'm a little off topic here...
我这里有点跑题了...
I guess you want to dump the map content for debugging. I like to mention that the next gdb release (version 7.0) will have a built in python interpreter which will be used by the gcc libstdc++ to provide stl pretty printers. Here is an example for your case
我猜您想转储地图内容以进行调试。我想提到下一个 gdb 版本(7.0 版)将有一个内置的 python 解释器,gcc libstdc++ 将使用它来提供 stl 漂亮的打印机。这是您的案例的示例
#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
}
which results in
这导致
(gdb) print mymap
= std::map with 2 elements = {
["bar"] = std::list = {
[0] = "item 1",
[1] = "item 2"
},
["foo"] = std::list = {
[0] = "item 1",
[1] = "item 2"
}
}
Yay!
好极了!
回答by mMontu
Another form, using <algorithm>
:
另一种形式,使用<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);
Test program:
测试程序:
#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);
}
回答by sancho.s ReinstateMonicaCellio
You can write a quite generic overloaded function, which is good for two purposes:
您可以编写一个非常通用的重载函数,它有两个用途:
- It works with any
map
. - It allows for using
<<
.
- 它适用于任何
map
. - 它允许使用
<<
.
The function is
功能是
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 <<
will work with any map
for which <<
is defined for typename
s key_t
and value_t
. In your case, this is not defined for value_t
(= list<string>
), so you also have to define it.
In a similar spirit, you can use
cout <<
将适用于为s和定义的任何map
内容。在您的情况下,这不是为(= )定义的,因此您还必须定义它。本着类似的精神,您可以使用<<
typename
key_t
value_t
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;
}
So, you may:
所以,你可以:
- Add these two functions.
- Add the prototypes where needed.
- Use
using namespace std;
(or addstd::
as needed). - Use, e.g.,
cout << mapex << endl;
cout << li << endl;
- 添加这两个功能。
- 在需要的地方添加原型。
- 使用
using namespace std;
(或std::
根据需要添加)。 - 使用,例如,
cout << mapex << endl;
cout << li << endl;
Remember that if there is any other viable candidate for the <<
s just defined (which I take there is not, otherwise you would likely not ask this question), it may take precedence over the present ones.
请记住,如果<<
刚刚定义的s有任何其他可行的候选者(我认为没有,否则您可能不会问这个问题),它可能优先于当前的候选者。
回答by honk
If you can use C++11features, then I think range-based for loopsas proposed in The Paramagnetic Croissant's answerprovide the most readable option. However, if C++17is available to you, then you can combine those loops with structured bindingsto further increase readability, because you no longer need to use the first
and second
members. For your specific use case, my solution would look as follows:
如果您可以使用C++11功能,那么我认为The Paramagnetic Croissant 的答案中提出的基于范围的 for 循环提供了最易读的选项。但是,如果您可以使用C++17,那么您可以将这些循环与结构化绑定结合起来 以进一步提高可读性,因为您不再需要使用and成员。对于您的特定用例,我的解决方案如下所示:first
second
std::map<std::string, std::list<std::string>> mapex;
mapex["a"] = { "1", "2", "3", "4" };
mapex["b"] = { "5", "6", "7" };
for (const auto &[k, v] : mapex) {
std::cout << "m[" << k.c_str() << "] =";
for (const auto &s : v)
std::cout << " " << s.c_str();
std::cout << std::endl;
}
Output:
输出:
m[a] = 1 2 3 4
m[b] = 5 6 7
m[a] = 1 2 3 4
m[b] = 5 6 7