如何准确格式化我的输出?
How can format my output exactly how it's asked?
我的输出应该是使用星星的向下箭头。
但是,它不起作用,我是C++新手,不知道如何操作循环。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string operator*(const string& s, unsigned int n) {
stringstream out;
while (n--)
out << s;
return out.str();
}
string operator*(unsigned int n, const string& s) { return s * n; }
int main(int, char **)
{
string space = " ";
string mix = "* ";
for (int i = 0; i<3;i++)
{
cout << space*i;
for (int j = 3; j>= 0; --j)
{
cout <<mix*j << endl;
}
}
}
预期成果:
* * *
* *
*
实际结果:
* * *
* *
*
* * *
* *
*
* * *
* *
*
更改以下内容:
cout << space*i;
for (int j = 3; j>= 0; --j)
{
cout <<mix*j << endl;
}
对此:
cout << space * i;
cout << mix * (3 - i) << endl;
由于主函数中不需要双循环,因此在第一个重载*
运算符中具有内部循环。
相关文章:
- 为什么我的代码在输出中增加了93天
- 在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 我的输出中有一个额外的 0,为什么会这样
- 为什么我的 cout 在输出中没有显示字符串?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 为什么我的输出与输入不同?(C++)
- 当使用我在一个函数、另一个函数中修改的数组时,在我的输出中得到一个奇怪的负数
- 为什么我的代码块上没有显示输出?
- "Program ended with exit code: 0"出现在我的输出的开头?
- gdb 中的列表命令不输出我编写的代码
- 我的代码中是否有任何类型的错误,因为它没有给出正确的输出
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 我的求解(字符串 a、字符串 b)的输出与随机哈希中的预期输出不匹配
- fftw:为什么我的 2D DFT 输出与每行的 1D DFT 输出不同?
- 即使我的代码没有错误,我也没有得到正确的输出
- 为什么这一行不输出我的文本文件?
- 在任何情况下都会出现意外输出.我的解决方案出了什么问题