【问题标题】:Get multiple strings from console and write them down as a list in C++从控制台获取多个字符串并将它们写成 C++ 中的列表
【发布时间】:2019-05-25 15:45:15
【问题描述】:

只要用户继续输入文本,程序就会从用户那里得到几个单词(数字未知),然后将它们打印成一个列表。

假设用户输入了一些单词:

Aang Kyoshi Shirin Farhad Parand Kamran  

输出应该是:

[Aang, Kyoshi, Shirin, Farhad, Parand, Kamran]

我已经写下了这段代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string current;
    int counter = 1;
    while (cin >> current)
    {
        if (counter == 1)
            cout << '[' << current << ", ";
        else
            cout << current << ", ";
        counter = counter + 1;
    }
    cout << ']' << endl;
    return 0;
}  

结果如下:

第 14 行:

cout << current << ", ";  

我应该怎么做才能不打印最后一个,

对于第 17 行:

cout << ']' << endl;  

代码如何退出while循环?它不会以EnterCtrl+ZCtrl+D 退出循环,因此第 17 行没有执行?!

【问题讨论】:

  • 如果在同一行输入单词,首先读取该行,然后使用字符串流提取单词
  • 如果您在 Windows 上使用 Ctrl+Z 来结束标准输入,您需要在此之后按 Enter,因为 Ctrl+Z 被视为一个字符,只有在输入行。

标签: c++


【解决方案1】:

这是一个没有ifs、没有标志、没有分支的实现:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::istringstream stream{line}; 
    std::string s;
    stream >> s;
    std::cout << "[ " << s;
    while (stream >> s)
    {
        std::cout << ", " << s;
    }
    std::cout << " ]";
}

【讨论】:

    【解决方案2】:

    以这种方式执行 while 循环以打印逗号。

    while (cin >> current)
    {
        if (counter == 1)
            cout << '[' << current;
        else
            cout << ", " << current;
        counter = counter + 1;
    }
    

    【讨论】:

    • 您可能永远不会看到它,但在数万亿字之后,计数器将回滚到 1。如果您不需要计数器来处理其他事情,请测试 isfirst 标志并在处理完第一个单词后将其设置为 false。
    • @user4581301 万亿?更像是几十亿:)
    • 计数器可以用bool useful = false;替换并在循环中设置为true,并且可能在循环后添加if (useful) cout &lt;&lt; ']' &lt;&lt; endl;?
    • @Ayxan 可能只有 64k,在这个用例中仍然不太可能遇到,但没有指定上限,所以我只选择了一个通用的、可笑的大数字。
    【解决方案3】:

    你可以使用stringstream:

    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    int main()
    {
       string t;
       getline(cin,t);
    
       istringstream iss(t);
       string word;
       iss >> word;
       cout << "[" << word ;
       while(iss >> word) {
          cout<< " ," <<word;
        }
        cout<<"]\n";
    }
    

    taking input of a string word by word

    【讨论】:

      【解决方案4】:

      如果你可以使用特定的词来结束进程,例如end,这可以工作:

      #include <iostream>
      #include <string>
      using namespace std;
      
      int main()
      {
          string current, output;
          bool first = true;
      
          output = "[";
          while (cin >> current)
          {
              if(current != "end"){
                  if(first){
                      output += current;
                      first = false;
                  } else {
                      output += ", " + current;
                  }
              } else {
                  break;
              }
          }
          output += "]";
          cout << output << endl;
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        相关资源
        最近更新 更多