簡體   English   中英

使用 setw 並從 iomanip 離開

[英]Using setw and left from iomanip

我想在有組織的表格中打印出地圖中的鍵和值。 我正在嘗試使用 setw 並離開,但輸出是

The  1
hello1

我希望它像

The     1
hello   1

到目前為止我做了什么

// System includes

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

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

本地包含

/************************************************************/
// 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;

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

函數原型/全局變量/類型定義

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

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

您需要指定大於 5 的寬度(如果您希望該字段大於 5 個字符)。 您不需要第二次調用 setw。

嘗試

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM