Stroustrup 5.9运动11 [英] Stroustrup 5.9 exercise 11
问题描述
它没有任何问题。我希望从可读性,维护等任何角度来改进
代码的建议:
---------- PROGRAM - -----------
/ * Stroustrup,5.9,练习11
声明:
阅读来自输入的一系列单词。使用退出作为单词
来终止输入。按照输入的顺序打印单词
。不要打印两次单词。修改程序,在打印之前对
单词进行排序。
* /
#include< iostream>
#include< string>
#include< vector>
int main( )
{
std :: vector< std :: stringcollect_input;
std :: string input_word;
std :: cin> input_word;
for(int i = 0; input_word!=" quit" ;; ++ i)
{
collect_input.push_back(input_word);
std :: cin> input_word;
}
std :: cout<< " \\\
*** Printing WOrds *** \ n";
for(unsigned int i = 0; i< collect_input.size(); ++ i )
std :: cout<< collect_input [i]
<< ''\ n'';
返回0;
}
------ ----- OUTPUT ----------------
[arch @ voodo tc ++ pl] $ g ++ -ansi -pedantic -Wall -Wextra - O
ex_5.9-11.cpp
[arch @ voodo tc ++ pl] $ ./a.out
喜欢这个< br $>
退出
相当
和morq quotwe完成退出
***打印WOrds ** *
喜欢
这个
退出
相当
和
morq
quotwe
结束
quiT
[arch @ voodo tc ++ pl] $
it works fine without any trouble. i want to have advice on improving
the code from any angle like readability, maintenance etc:
---------- PROGRAMME ------------
/* Stroustrup, 5.9, exercise 11
STATEMENT:
Read a sequence of words from the input. use "quit" as the word
to terminate the input. Print the words in the order they were
entered. don''t print a word twice.modify the programme to sort the
words before printing them.
*/
#include<iostream>
#include<string>
#include<vector>
int main()
{
std::vector<std::stringcollect_input;
std::string input_word;
std::cin >input_word;
for(int i=0; input_word != "quit"; ++i)
{
collect_input.push_back(input_word);
std::cin >input_word;
}
std::cout << "\n *** Printing WOrds ***\n";
for(unsigned int i=0; i < collect_input.size(); ++i)
std::cout << collect_input[i]
<< ''\n'';
return 0;
}
----------- OUTPUT ----------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O
ex_5.9-11.cpp
[arch@voodo tc++pl]$ ./a.out
like this
quitting
quite
and morq quotwe FINISHED quiT quit
*** Printing WOrds ***
like
this
quitting
quite
and
morq
quotwe
FINISHED
quiT
[arch@voodo tc++pl]$
推荐答案
g ++ -ansi -pedantic -Wall -Wextra -O
ex_5.9-11。 cpp
[arch @ voodo tc ++ pl]
g++ -ansi -pedantic -Wall -Wextra -O
ex_5.9-11.cpp
[arch@voodo tc++pl]
./ a.out
赞这样
退出
相当
和morq quotwe完成quiT退出
***打印WOrds ***
喜欢
这个
退出
相当
并且
morq
quotwe
结束
quiT
[arch @ voodo tc ++ pl]
./a.out
like this
quitting
quite
and morq quotwe FINISHED quiT quit
*** Printing WOrds ***
like
this
quitting
quite
and
morq
quotwe
FINISHED
quiT
[arch@voodo tc++pl]
这篇关于Stroustrup 5.9运动11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!