我一辈子都不能把它放在C++



一直在努力让它对齐,每个源都告诉我使用left和setw,但无论我如何格式化它,我似乎都无法让它工作。有人有什么想法吗?

#include <iostream>
#include <string>
#include <iomanip> using namespace std;
int main() {   string name[5] = {"able", "beehive", "cereal", "pub", "deck"};   string lastName[5] = {"massive", "josh", "open", "nab", "itch"};   float score[5] = {12, 213, 124, 123, 55};   char grade[5] = {'g', 'a', 's', 'p', 'e'};   int counter = 5;    for (int i = 0; i < counter; i++){
cout << left
<< name[i] << " " << setw(15) << left
<< lastName[i] << setw(15) << left
<< score[i] << setw(20)
<< grade[i];     cout << endl;    } }

这是输出:

able massive        12             g                   
beehive josh           213            a                   
cereal open           124            s                   
pub nab            123            p                   
deck itch           55             e

setw设置下一个输出的宽度。它不会追溯更改以前输出的格式。您需要的不是... << someoutput << setw(width)而是... << setw(width) << someoutput:

#include <iostream>
#include <string>
#include <iomanip> 
using namespace std;
int main() {   
string name[5] = {"able", "beehive", "cereal", "pub", "deck"};   
string lastName[5] = {"massive", "josh", "open", "nab", "itch"};  
float score[5] = {12, 213, 124, 123, 55};   
char grade[5] = {'g', 'a', 's', 'p', 'e'};   
int counter = 5;   
for (int i = 0; i < counter; i++){
cout << left <<  " " << setw(15) << left << name[i] 
<<  setw(15) << left << lastName[i] 
<<  setw(20) << score[i]
<< grade[i];   
cout << endl;    
} 
}

实时:

able           massive        12                  g
beehive        josh           213                 a
cereal         open           124                 s
pub            nab            123                 p
deck           itch           55                  e

以下是经过一些重构的代码:

#include <iostream>
#include <iomanip>
#include <string>
#include <array>

int main( )
{
// use std::array instead of antiquated raw arrays, it does
// the same job but more conveniently
// also use value initialization like `{ }` instead of this `= { }`
std::array<std::string, 5> name { "able", "beehive", "cereal", "pub", "deck" };
std::array<std::string,5> lastName { "massive", "josh", "open", "nab", "itch" };
std::array<float, 5> score { 12, 213, 124, 123, 55 };
std::array<char, 5> grade { 'g', 'a', 's', 'p', 'e' };

constexpr std::size_t counter { name.size( ) }; // `counter` can be a
// constant expression so
// declare it constexpr
for ( std::size_t idx { }; idx < counter; ++idx ) // std::size_t for the
{                                                 // loop counters
std::cout << std::left << ' '
<< std::setw(15) << std::left << name[idx]
<< std::setw(15) << std::left << lastName[idx]
<< std::setw(20) << std::left << score[idx]
<< std::setw(1) << std::left << grade[idx];
std::cout << 'n';
} 
}

输出

able           massive        12                  g
beehive        josh           213                 a
cereal         open           124                 s
pub            nab            123                 p
deck           itch           55                  e

相关内容

  • 没有找到相关文章

最新更新