为什么在循环的第一次迭代中,剩余的理由不起作用?

发布于 2025-01-17 20:49:43 字数 738 浏览 3 评论 0原文

#include <iostream>

int main(){
       using namespace std;

       string s1("Hello1");
       string s2("Hello2");
       
       for(int i = 0; i < 3; i++){
           
           cout.width(20); cout<<"Some String:"<<left<<s1<<endl;
           cout.width(20); cout<<"Another String:"<<left<<s2<<endl;
           
           cout<<endl;
       }
       return 0;
 }

这是我的代码。据我所知,它应该从屏幕左侧打印S1和S2 20个字符。但是,

        Some String:Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

根据我的老师的指示,它打印了我使用在线GDB进行编译的印刷。我犯了什么错误?

#include <iostream>

int main(){
       using namespace std;

       string s1("Hello1");
       string s2("Hello2");
       
       for(int i = 0; i < 3; i++){
           
           cout.width(20); cout<<"Some String:"<<left<<s1<<endl;
           cout.width(20); cout<<"Another String:"<<left<<s2<<endl;
           
           cout<<endl;
       }
       return 0;
 }

Here is my code. It, to my knowledge, should print s1 and s2 20 characters from the very left of the screen. However, it prints

        Some String:Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

I am using onlineGDB to compile, as per instruction of my teacher. What error am I making?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤檠 2025-01-24 20:49:43

std::left 是一个“粘性”操纵器,这意味着您只需设置一次。默认情况下,填充的字符串将右对齐,这就是在应用 std::left 操纵器之前输出 "Some String:" 时发生的情况。

请参阅文档,其中指出:

标准流的初始默认值相当于right

修复您的代码,并稍微整理一下:

#include <iostream>
#include <iomanip>

int main()
{
    using namespace std;

    string s1("Hello1");
    string s2("Hello2");

    cout << left;       
    for(int i = 0; i < 3; i++) {
        cout << setw(20) << "Some String:" << s1 << endl;
        cout << setw(20) << "Another String:" << s2 << endl;
        cout << endl;
    }
    return 0;
}

输出:

Some String:        Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

请注意,我使用了 中的 std::setw I/O 操纵器,而不是调用 <代码>cout.width()。这使得代码更容易阅读和遵循。

请参阅 文档了解 std::setw

std::left is a "sticky" manipulator, meaning you just set it once. By default, padded strings will be right-justified, which is what happens when you output "Some String:" before ever applying the std::left manipulator.

See the documentation which states:

The initial default for standard streams is equivalent to right.

Fixing your code, and tidying it up a bit:

#include <iostream>
#include <iomanip>

int main()
{
    using namespace std;

    string s1("Hello1");
    string s2("Hello2");

    cout << left;       
    for(int i = 0; i < 3; i++) {
        cout << setw(20) << "Some String:" << s1 << endl;
        cout << setw(20) << "Another String:" << s2 << endl;
        cout << endl;
    }
    return 0;
}

Output:

Some String:        Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

Some String:        Hello1
Another String:     Hello2

Note that I used std::setw I/O manipulator from <iomanip> instead of the call to cout.width(). This makes the code easier to read and follow.

See the documentation for std::setw

挽心 2025-01-24 20:49:43

该陈述说

cout.width(20); cout<<"Some String:"<<left<<s1<<endl;

set column width to 20,
output "Some String:"
start left justifying
output s1
output end of line 

你的意思是

cout.width(20);
cout << left;
cout<<"Some String:" << s1 << endl;

this statment says

cout.width(20); cout<<"Some String:"<<left<<s1<<endl;

set column width to 20,
output "Some String:"
start left justifying
output s1
output end of line 

you mean

cout.width(20);
cout << left;
cout<<"Some String:" << s1 << endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文