标签: setw

用0表示前面的数字

可能重复:
使用C++输出运算符(printf等效)打印前导零?

#include <iostream>
#include <iomanip>
int main()
{
   int n = 16;
   std::cout << ???(5) << n << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我希望输出以00016
setw()空格为前缀.是不是可配置哪些字符前置setw()

我最终的目标是在4个位置打印一个16字节的十六进制数字.像这样的东西:

#include <iostream>
#include <iomanip>
int main()
{
    unsigned short n = 0xA7;
    std::cout << std::hex << ???(4) << n << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我希望得到这个输出: 00A7

c++ formatting iostream setw iomanip

3
推荐指数
1
解决办法
434
查看次数

我需要c#中的一些东西,就像c ++中的setw()一样

我在c#中使用了richTextBox.我需要在一个richTextBox中显示不同长度的字符串,但这些字符串应该完全对齐..这是一个例子..

abcd   abcde  abcde
ab     abc    abcdef
Run Code Online (Sandbox Code Playgroud)

我知道如何使用setw函数在c ++中执行此操作..但我找不到c#中的等效项.

c# formatting alignment setw

3
推荐指数
2
解决办法
5847
查看次数

setw没有按预期工作

我正在尝试以hh:mm格式打印时间,但当时间如01:01时,它打印为1:1.这是我的代码:

void output(int hour, int min, char ampm){
    cout << setw(2) << setfill('0') << "The time is: " << hour << ":" << min << " ";

    if(ampm == 'P'){
        cout << "PM";
    }
    else if (ampm == 'A'){
        cout << "AM";
    }
}
Run Code Online (Sandbox Code Playgroud)

据我了解,这应该工作.我包括iomanip.你能看到它有什么问题吗?

c++ setw

3
推荐指数
1
解决办法
4136
查看次数

C++ iomanip 对齐

我正在尝试调整我的输出,但由于某种原因我无法达到我想要的效果,这真的很令人沮丧。标题不会正确对齐。我不知道我是否正确使用 setw() 。

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <assert.h>
#include <iomanip>
using std::setw;
using std::right;
using std::left;

//Movie Struct to hold movie data
struct MOVIES
{
    string Title; //Name of movie
    int CriticRating; //Critic rating 1-100
    int AudRating; //Audiences' rating 1-100
};
MOVIES Movies[10] = { { "The Wizard of Oz", 99 , 70 },
                      { "The Third Man"   , 78, 45 },
                      { "Citizen Kane"    , 86, 85 },
                      { "Charlie Chaplin in …
Run Code Online (Sandbox Code Playgroud)

c++ alignment setw iomanip

3
推荐指数
1
解决办法
2万
查看次数

std :: setw如何使用字符串输出?

我试图将setw字符串输出的设置宽度用于输出文件,但是,我无法使其工作.我跟我有以下例子.

// setw example
#include <iostream>     
#include <iomanip>      
#include <fstream>

int main () {
    std::ofstream output_file;
    output_file.open("file.txt");
    output_file << "first" <<std::setw(5)<< "second"<< std::endl;
    output_file.close();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑: 对于上述行我预计将有很多空间之间firstsecond,像 first second

我几乎看不到任何空格,输出就像firstsecond 我想我错过了工作setw()

注意:对于整数,它只能正常工作:

output_file << 1 <<std::setw(5)<< 2 << std::endl;
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

c++ string setw

2
推荐指数
1
解决办法
4714
查看次数

C ++:setw()仅在循环的第一行上工作

我试图解析一个文本文件,并使用setw()将其格式化后的内容输出到控制台。我的问题是只有第一行的格式正确,其余的默认回到左。

while (test) 
{
    cout << setw(20) << right;
    string menu;
    price = 0;

    getline(test, menu, ',');
    test >> price;

    cout << setw(20) << right << menu;;

    if (price)
        cout << right << setw(10) << price;


}
Run Code Online (Sandbox Code Playgroud)

我的目标是使输出与右边的最长单词(长度为20个空格)对齐,但是我的输出最终像这样:

           WordThatAlignsRight
notAligning
my longest sentence goal align
notAligning
Run Code Online (Sandbox Code Playgroud)

我希望每个句子在循环中右对齐20个空格。任何帮助表示赞赏,谢谢!

c++ setw iomanip

2
推荐指数
1
解决办法
73
查看次数

用setw证明文本

我想证明这样的输出文本

 0x29823d80    0x10019b8         /    0 00000000000000000000000000000001
 0x37449e60    0x10dfc           /   12 00000000000000000001000000000000
Run Code Online (Sandbox Code Playgroud)

但是有了这个说法

  fout << std::setw(5) << std::hex << "0x" << (*it).addr << " " 
       << std::setw(5) << std::hex << "0x" << (*it).pc << std::setw(10) << "/" << std::setw(5) << std::dec << (*it).off << "\t" 
       << std::setw(5) << (*it).layout << "\n";
Run Code Online (Sandbox Code Playgroud)

我明白了:

0x29823d80    0x10019b8         /    0  00000000000000000000000000000001
0x37449e60    0x10dfc         /   12    00000000000000000001000000000000 
Run Code Online (Sandbox Code Playgroud)

c++ setw

1
推荐指数
1
解决办法
612
查看次数

标签 统计

setw ×7

c++ ×6

iomanip ×3

alignment ×2

formatting ×2

c# ×1

iostream ×1

string ×1