- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 C 的 itoa
函数和 C++ setfill
和 setw
函数生成二进制数。如果我只使用 itoa
,则显示的输出没有正确的 0
填充。
这是一个小代码片段。
int s = 8;
for (int i = 1; i<s;i++)
{
itoa(i,buffer,2);
cout<<setfill('0')<<setw(3)<<endl;
cout<<buffer<<endl;
}
现在它在打印输出方面做得很好。
如果我没有使用 setfill 和 setw,格式会是这样的
1
10
11
100
101
110
111
代替
001
010
011
100
101
110
111
现在我想存储生成的填充二进制数并将其存储到一个 vector 中。可能吗?
我想我有一个使用 bitset 的解决方案,而且效果很好。
std::ostringstream oss;
int s = 3;
for (int i = 1; i<s;i++)
{
itoa(i,buffer,2);
oss<<setfill('0')<<setw(3);
oss<<buffer;
string s = oss.str();
cout<<s<<'\n'<<endl;
};
不过,我只想指出,我得到的解决方案看起来有些像这个!
能否通过在连续迭代中刷新流来操纵它。这只是事后的想法。
最佳答案
考虑使用 bitset
而不是 itoa
:
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> binary_representations;
int s = 8;
for (int i = 1; i < s; i++)
{
binary_representations.push_back(std::bitset<3>(i).to_string());
}
}
编辑:如果您需要可变长度,一种可能性是
// Note: it might be better to make x unsigned here.
// What do you expect to happen if x < 0?
std::string binary_string(int x, std::size_t len) {
std::string result(len, '0');
for(std::string::reverse_iterator i = result.rbegin(); i != result.rend(); ++i) {
*i = x % 2 + '0';
x /= 2;
}
return result;
}
后来
binary_representations.push_back(binary_string(i, 3));
关于c++ - 将 setfill 和 setw 的输出存储到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27620849/
我试图让我的输出看起来像这样: size time1 time2 ------------------------------- 10 4
我正在尝试在 Java 中传递一个变量,我需要帮助让它工作,我在下面包含了所有相关代码,我正在使用三个 .java 文件。以下是所有设置的方式以及我需要它的工作方式; 属性.java public e
要点如下:我有一个 getColor 方法和一个 setColor 方法。 getColor 将全局指针 color_obj 更新为当前对象。 setColor 使用该指针来更改对象的颜色。 问题是,
我正在尝试创建一个基于多个文件的程序来读取时间,但我无法以所需的格式显示时间。更具体地说,setfill 似乎给我带来了问题。 这是我在编译时收到的一条很长的错误消息的开头: error: no ma
我试图了解 JavaFX Canvas setStroke 方法的工作原理。它不会将像素的颜色设置为所需的值。不过 setFill 方法没有问题。 Canvas canvas = new Canvas
这个问题已经有答案了: Can we call a method at class level rather than in a method? (3 个回答) 已关闭 3 年前。 我正在启动 Jav
我正在编写一款受康威的“生命游戏”启发的游戏。 虽然我已经弄清楚了整体游戏逻辑(但没有编码),但我仍然无法在玩家的第一个回合结束后更改矩形对象的填充颜色。当我运行我的程序时,它会跳过对玩家一号颜色(C
基本上,我正在尝试通过 setfill 和 setw 创建一个固定表来显示存储在数组中的信息,但是设置宽度似乎没有起作用,我不确定为什么。这是我正在使用的代码: const int MAXWIDTH
我有两个并排的函数,它们循环不断地绘制两个 UIBezierPaths,问题是,它们每个都有不同的颜色,所以我经常需要重申 UIColor.blackColor().setFill() 和 UICol
根据我的理解,setw(n) 通过创建一个宽度为 n 个字符的行为。另一方面,setfill('x') 允许我们打印 n 个 x 个字符。 我想得到两排星星,像这样: ****************
所以我已经解决这个问题一段时间了,在成员的帮助下,我快完成了。我的最后一个问题在上面。 如有必要,我需要将 cout 时间格式化为 01:01:01,但是,我的输出是 1:1:1 问题出在这里: st
Win7 - 64 cygwin Netbeans 7.4 gcc (cygwin) 4.8.3 编译器调用 g++ -Wall -Wno-reorder -Wno-unused-value -c -
在 C++ 中: int main() { cout << setfill('#') << setw(10) << 5 << endl; return 0; } 输出: #######
我有一个现有的代码库,它创建了一个舞台,然后添加了一些图层,并在其中一个图层中绘制了 KineticJS 多边形。现在我需要在用户单击舞台外的元素时更新多边形的背景。 我已经设法获取了舞台细节,然后是
我有一些 Java 代码: public static String getSaveFilePath(String title2) { FileDialog fd = new FileDial
一个简单的 C++ 乘法表。 int main() { int i, j; i = j = 1; for (i; i < 10; i++) { for (j = 1
我不明白为什么这段代码在 drawRect: 中有效: UIBezierPath *buildingFloor = [[UIBezierPath alloc] init]; // draw the s
我很好奇 std::setw 和 std::setfill 的真正返回类型 正如我们所见,它们返回值的数据类型是“未定义”。但是,是否可以声明一个没有返回类型的函数? 在我必须开发一种方法为“cout
所以我知道有很多次我想重置我使用 iomanip 所做的格式更改。我创建了一个类,您可以使用它来获取 ostream 对象的格式,例如用于写入/读取文件的 std::cout 或 fstream 对象
我正在尝试使用 setw 和 setfill 获得以下输出: OPTIONS: The usual operators +, -, *, / and % (remainder)
我是一名优秀的程序员,十分优秀!