[英]Using setw and left from iomanip
I want to print out the keys and values in a map in a organized table.我想在有组织的表格中打印出地图中的键和值。 I am trying to use setw and left but the output is
我正在尝试使用 setw 并离开,但输出是
The 1
hello1
and I want it to be like我希望它像
The 1
hello 1
what i have done so far到目前为止我做了什么
// System includes
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>
/************************************************************/
Local includes本地包含
/************************************************************/
// Using declarations
using std::cout;
using std::map;
using std::unordered_map;
using std::string;
using std::cin;
using std::endl;
using std::ifstream;
using std::left;
using std::setw;
/************************************************************/
Function prototypes/global vars/typedefs函数原型/全局变量/类型定义
/************************************************************/
int
main (int argc, char* argv[])
{
//call the wordCount function on the user specified input file
unordered_map <string,int> exampleMap;
exampleMap["The"]++;
exampleMap["hello"]++;
cout << left;
//iterate through the map and print out the elements
for( unordered_map<string, int>::iterator i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(5) << (*i).first << setw(15) << (*i).second << endl;
}
return EXIT_SUCCESS;
}
you need to specify a width larger than 5(If you want the field to be larger than 5 characters).您需要指定大于 5 的宽度(如果您希望该字段大于 5 个字符)。 You don't need the second call to setw.
您不需要第二次调用 setw。
try尝试
for( auto i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(8) << i->first << i->second << '\n';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.