- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在有组织的表格中打印出 map 中的键和值。我正在尝试使用 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;
/************************************************************/
函数原型(prototype)/全局变量/类型定义
/************************************************************/
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(如果你希望字段大于5个字符)。您不需要第二次调用 setw。
尝试
for( auto i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(8) << i->first << i->second << '\n';
}
关于c++ - 使用 setw 并从 iomanip 离开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13432248/
我刚刚遇到了将自定义输出运算符与 io-manipulators 相结合的问题。也许我的期望完全不对,但如果 std::cout #include struct foo { int a,b; };
我正在尝试对齐我的输出,但由于某种原因我无法做到我多么想要它,这真的很令人沮丧。标题不会正确对齐。我不知道我是否正确使用了 setw()。 #include using std::cout; usi
我正在尝试获取一个 double 并通过使用 iomanip 并编写我自己的流操纵器将其输出为货币。为此,我编写了一个方法,该方法返回一个 ostream 并将一个 ostream 作为参数(以及两个
为什么 #include #include int main() { std::cout > overload page individually. 预期: But that's just
变量: static const float s_period[] = { 100, 50, 25, 12, 5, 7, 3, 2, 1 }; static const unsigned
我想使用 iomanip 打印以筛选小数点后最多 4 位的数字。 我了解到在默认模式下 setprecision 不仅计算小数点后的数字,还计算整数部分的数字。这段代码 #include #incl
我有以下代码 cout 的不一致行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938047/
在 C++ 程序中,我有一些代码可以打印一个名为 fraction 的类的对象。它的变量是 n(分子)、d(分母)和 sinal(信号:当分数为正时为真,否则为假)。 ostream &operato
一些标准的 iomanip 函数接受一个参数。 我想知道这是如何实现的,例如,我可以用一个函数做类似的事情吗?这确实是我需要的解决方案 this answer ,但我不知道该怎么做。 当我在 http
今天,我了解了setfill()和#include中的其他方法,因此我有使用#include的练习。 我想在控制台上打印它: +-------------------------+ |
在排序和显示大数字时,您通常通常会以符号形式显示大数字。我试图通过使用cpp中的库显示整个数字,但是对于很大的数字却失败了。 //big sorting #include #incl
对于下面的代码: cout << left << setw(20) << "Example Header\n"; cout << setw(60) << setfill('-') << "-" <<
当我注意到一些我不理解的东西时,我才刚刚开始编写程序。我定义了一个名为“时间”的结构。当我尝试定义一个名为“time1”的结构变量时,它发出一个错误:“expected an ';'”。但是当我删除头
这个问题在这里已经有了答案: Setting width in C++ output stream (3 个答案) 关闭 9 年前。 我知道如何设置字段宽度,但只适用于流中的第一个元素。例如。 co
我正在努力学习使用命名空间声明,而不是仅仅说“使用命名空间标准”。我正在尝试将我的数据格式化为小数点后两位,并将格式设置为固定而不科学。这是我的主文件: #include #include #in
我用 C++ 创建了一个 Vector 类,它非常适合我的问题。我现在正在清理它,我遇到了以下代码: std::ostream& operator<<(std::ostream &output, co
完成一个长项目,最后一步是确保我的数据排列在正确的列中。简单的。只有我在这方面遇到了麻烦,而且我在这方面的时间比我承认观看了很多视频的时间还要长,而且我真的不知道该怎么做所以这是我遇到问题的一小段代码
我正在尝试使用 iomanip 格式化我的代码输出: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2) << "M
我是 C++ STL 库的新手,需要帮助。 我想添加两个数字,假设 A = 4555 和 B = 50,并将它们输出为: 4555 +50 4605 另一个例子: 500000 + 12 500000
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我是一名优秀的程序员,十分优秀!