2

マップ内のキーと値を整理されたテーブルに出力したいと考えています。setw と left を使用しようとしていますが、出力は

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;

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

関数プロトタイプ/グローバル変数/typedef

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

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

1 に答える 1

2

5より大きい幅を指定する必要があります(フィールドを5文字より大きくする場合)。setwに2回目の呼び出しは必要ありません。

試す

 for( auto i=exampleMap.begin(); i!=exampleMap.end(); ++i)
     {
        cout << setw(8) << i->first << i->second << '\n';
      }
于 2012-11-17T16:07:28.867 に答える