在c++中对齐右侧的输出
Aligning output on the right in c++
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << right << "Hello" << setw(10) << "Worldn";
cout << right << "Goodbye" << setw(10) << "Worldn";
}
为什么这会导致输出类似:
Hello World
Goodbye World
而不是:
Hello World
Goodbye World
我在这里做错了什么?
编辑:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << "Hello" << " World";
cout << right << setw(10) << "todayn";
cout << "Goodbye" << " World";
cout << right << setw(10) << "todayn";
}
如果我尝试这样做,为什么"今天"部分会错位?
更改运算符的顺序以解决此问题:
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::left << std::setw(10) << "Hello" << "Worldn";
std::cout << std::left << std::setw(10) << "Goodbye" << "Worldn";
return 0;
}
- 您必须将所有运算符放在要格式化的值之前
- 避免使用
using namespace std
std::setw()
运算符将字段设置为下一个值。并且std::left
或std::right
运算符设置该值在该字段中的位置。
此示例
std::cout << std::left << std::setw(10) << "word1"
<< std::right << std::setw(20) << "word2" << std::endl;
将创建这种格式:
AAAAAAAAAABBBBBBBBBBBBBBBBBBBB
word1 word2
您可以看到,第一个"字段"包含10个字符,第一个文本位于其中,第二个"字段"包含20个字符,第二单词位于右对齐位置。但是,如果第一个字段中的文本比字段长,就会发生这种情况:
AAAAAAAAAA....BBBBBBBBBBBBBBBBBBBB
word1istoolong word2
第二个字段只是移动了字符数。流从不跟踪当前位置,它只是使用给定大小的"字段"构建输出。
要用来证明给定页面的文本,请使用以下代码:
#include <iostream>
#include <sstream>
#include <list>
const int pageWidth = 78;
typedef std::list<std::string> WordList;
WordList splitTextIntoWords( const std::string &text )
{
WordList words;
std::istringstream in(text);
std::string word;
while (in) {
in >> word;
words.push_back(word);
}
return words;
}
void justifyLine( std::string line )
{
size_t pos = line.find_first_of(' ');
if (pos != std::string::npos) {
while (line.size() < pageWidth) {
pos = line.find_first_not_of(' ', pos);
line.insert(pos, " ");
pos = line.find_first_of(' ', pos+1);
if (pos == std::string::npos) {
pos = line.find_first_of(' ');
}
}
}
std::cout << line << std::endl;
}
void justifyText( const std::string &text )
{
WordList words = splitTextIntoWords(text);
std::string line;
for (WordList::const_iterator it = words.begin(); it != words.end(); ++it) {
if (line.size() + it->size() + 1 > pageWidth) { // next word doesn't fit into the line.
justifyLine(line);
line.clear();
line = *it;
} else {
if (!line.empty()) {
line.append(" ");
}
line.append(*it);
}
}
std::cout << line << std::endl;
}
int main()
{
justifyText("This small code sample will format a paragraph which "
"is passed to the justify text function to fill the "
"selected page with and insert breaks where necessary. "
"It is working like the justify formatting in text "
"processors.");
return 0;
}
这个例子证明了每一行在给定页面的开头都是正确的。它的工作原理是将文本拆分为多个单词,用这些单词填充行,并将每行对齐以与宽度完全匹配。
问题是,在调用setw
时,输出流不记得打印"Hello"
或"Goodbye"
。
试试这个来产生你想要的输出:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << left << setw(10) << "Hello" << "Worldn";
cout << left << setw(10) << "Goodbye" << "Worldn";
}
这是一个使用std::format(C++20(:的解决方案
#include <format>
#include <iostream>
int main() {
std::cout << std::format("{:10}{:}n", "Hello", "World");
std::cout << std::format("{:10}{:}n", "Goodbye", "World");
}
试试这个-
cout << right << "Hello" << "t" << "Worldn";
cout << right << "Goodbye" << "t" << "Worldn";
相关文章:
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 64位机器上的C++内存对齐
- C++ cout 对齐输出数字
- cout 不能一致地打印字符串和变量值,导致输出未对齐
- 如何对齐输出?
- 在输出输入文本时对齐输入的文本
- 背诵和提示输出对齐C++,输出格式
- C 输出中的总右对齐
- 将输出文本与 cout 对齐到右侧
- C++输出对齐和小数位数保持器
- 如何对齐来自数组的不同长度的输出
- 如何对齐不同长度的输出C++
- 如何将输出对齐到屏幕的中心- c++
- Fstream和setw没有正确对齐输出
- c++:右对齐的字段输出
- 在c++中对齐(格式化)输出//多个变量在一行中
- 为什么clang输出对齐特定的东西
- 在c++中对齐右侧的输出