简体   繁体   English

以相反的顺序打印字符串输入

[英]Printing string inputs in reverse order

I'm trying to build a program which outputs the inputs in reverse order until the word "stop" is entered. 我正在尝试构建一个程序,以相反的顺序输出输入,直到输入单词“ stop”为止。 for example, if I enter 例如,如果我输入

dog cat monkey 
desk chair 
burger cheese ham 
stop 

it should be outputting: 它应该输出:

burger cheese ham
desk chair
dog cat monkey

So far I have the following: 到目前为止,我有以下内容:

{
string x, y;
do {
    cin >> x;
    y = y + x;
} while (x != "stop");

int reverse = y.getNumLines();

while (reverse >= 0) {
    cout << y.getLine(reverse) << "\n";
    reverse = reverse -1;
}
return 0;    
} 

It reads my input correctly, but for the y.getNumlines and y.getLine , I get error with no outputs. 它可以正确读取我的输入,但是对于y.getNumlinesy.getLine ,我得到没有输出的错误。 Can anyone help me out? 谁能帮我吗?

Why not swap the order you are appending? 为什么不交换您要追加的订单?

#include <iostream>

int main(int argc, const char * argv[])
{

    std::string x, y;
    while ( (std::getline(std::cin,x)) && x != "stop"){
        y = x + "\n" + y;  // "" + "foo bar"  2.) "second input" + "foo bar"
    }

    std::cout<< y;

    return 0;
}

Nico's answer might be a better path for you, but for those who prefer a more idiomatic approach, I recommend a vector of lines that you can easily print backwards once stored. Nico的答案可能对您来说是一条更好的途径,但是对于那些喜欢使用更惯用的方法的人,我建议使用行向量,一旦存储起来就可以轻松地向后打印。 The first thing would be a Line class that you can use to idiomatically read lines of input instead of stopping at spaces: 第一件事是Line类,您可以使用它来惯用地读取输入行,而不是停在空格处:

struct Line {
   std::string str;
};

std::istream &operator>>(std::istream &in, Line &line) {
   std::getline(in, line.str);

   if (line.str == "stop") {
       in.clear(std::ios::failbit);
   }

   return in;
}

std::ostream &operator<<(std::ostream &out, const Line &line) {
   out << line.str;
   return out;
}

All our Line needs is a string with the actual text, and I've added input and output support for it, for the reason you'll see next. Line需要的只是一个带有实际文本的字符串,我已经添加了对它的输入和输出支持,原因是您接下来将要看到的原因。 Notice that the input is through getline rather than >> . 注意,输入是通过getline而不是>> This is the main purpose of the Line class. 这是Line类的主要目的。 I also made it fail when "stop" is entered. 当输入“停止”时,我也使它失败。 If that's left out, it will continue reading until the EOF. 如果没有,它将继续读取直到EOF。

Now you can reuse the line class with minimal effort in order to shape input however you want. 现在,您可以轻松地重用line类,以便根据需要调整输入的形状。 Next, we get to use what we just created (or possibly had left from something else): 接下来,我们将使用我们刚创建的(或可能已从其他内容中删除的内容):

int main() {
   std::istream_iterator<Line> in1(std::cin), in2;
   std::vector<Line> inputs(in1, in2);
   std::reverse_copy(std::begin(inputs), std::end(inputs), std::ostream_iterator<Line>(std::cout, "\n"));
}

The first thing we do is form a vector by reading input (using our input support for Line ). 我们要做的第一件事是通过读取输入(使用对Line的输入支持)形成向量。 When the end of file is reached, or "stop" is entered, it will stop, and every line input will be nicely stored in our vector. 当到达文件末尾或输入“停止”时,它将停止,并且每一行输入都将很好地存储在向量中。

Next, we call reverse_copy to copy everything in our vector (each line of input) to the standard output, separated by newlines, but all done backwards, so we end up with every line in the reverse order. 接下来,我们调用reverse_copy将向量(输入的每一行)中的所有内容复制到标准输出中,并以换行符分隔,但是所有操作都向后进行,因此我们以相反的顺序结束每一行。

And that's it! 就是这样! Once the Line class has been made, the algorithm for getting line inputs and reversing them can stay the same as if we were doing it with words, but the switch to lines only requires replacing a few instances of std::string with Line . 一旦创建Line类,获取行输入并将其反转的算法就可以像使用单词一样保持不变,但是切换到行仅需要用Line替换几个std::string实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM