首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用setw和left from iomanip

使用setw和left from iomanip
EN

Stack Overflow用户
提问于 2012-11-18 00:04:38
回答 1查看 996关注 0票数 2

我想在一个有组织的表中打印出映射中的键和值。我尝试使用setw和left,但输出是

代码语言:javascript
复制
The  1
hello1

我想让它像这样

代码语言:javascript
复制
The     1
hello   1

到目前为止,我所做的

代码语言:javascript
复制
// System includes

#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>

/************************************************************/

本地包含

代码语言:javascript
复制
/************************************************************/
// 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;

/************************************************************/

函数原型/全局vars/typedefs

代码语言:javascript
复制
/************************************************************/

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;
}
EN

回答 1

Stack Overflow用户

发布于 2012-11-18 00:07:29

需要指定大于5的宽度(如果希望字段大于5个字符)。您不需要对setw进行第二次调用。

试一试

代码语言:javascript
复制
 for( auto i=exampleMap.begin(); i!=exampleMap.end(); ++i)
     {
        cout << setw(8) << i->first << i->second << '\n';
      }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13432248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档