string str;
str = " General Knowledge Test ";
cout << string( 39, ' ' ) << string ( 37 , '-') << "\n";
cout << string( 39, ' ' ) << "|" << str<< "|\n ";
cout << string( 38, ' ' ) << string ( 37 , '-') << "\n";
cout << string( 39, ' ' ) << "|"<< string ( 35 , ' ') << "|"<< "\n";
cout << string( 39, ' ' ) << "|" << " > Pentru a continua apasa 'k' |\n ";
cout << string( 38, ' ' ) << "|" << " > Pentru ajutor apasa 'a' |\n ";
cout << string( 38, ' ' ) << "|" << " > Pentru raspunsuri apasa 'r' |\n ";
cout << string( 38, ' ' ) << "|" << " > Pentru high score apasa 'h' |\n ";
cout << string( 38, ' ' ) << "|" << " > Pentru a iesi apasa 'q' |\n ";
cout << string( 38, ' ' ) << "|"<< string ( 35 , ' ') << "|"<< "\n";
cout << string( 39, ' ' ) << string ( 37 , '-') << "\n\n";
答案 0 :(得分:1)
您可以使用原始字符串文字来提高静态输出的可读性和可维护性:
#include <iostream>
#include <string>
int main(){
std::string str =
R"( -------------------------------------
| General Knowledge Test |
-------------------------------------
| |
| > Pentru a continua apasa 'k' |
| > Pentru ajutor apasa 'a' |
| > Pentru raspunsuri apasa 'r' |
| > Pentru high score apasa 'h' |
| > Pentru a iesi apasa 'q' |
| |
-------------------------------------)";
std::cout << str;
}
查看live demo。
当然你也可以使用std::vector<std::string>
(但这不一定是优化):
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
int main(){
std::vector<std::string> strs {
"-------------------------------------",
"| General Knowledge Test |",
"-------------------------------------",
"| |",
"| > Pentru a continua apasa 'k' |",
"| > Pentru ajutor apasa 'a' |",
"| > Pentru raspunsuri apasa 'r' |",
"| > Pentru high score apasa 'h' |",
"| > Pentru a iesi apasa 'q' |",
"| |",
"-------------------------------------"
};
for(auto str : strs) {
std::cout << std::setw(70) << std::right << str << '\n';
}
}
这里又是live demo。