【问题标题】:Need to separate numbers from a string on a line, separated by ';', (25;16;67;13) in c++需要将数字与字符串分隔在一行上,用';'分隔,c++中的(25;16;67;13)
【发布时间】:2020-06-26 07:28:46
【问题描述】:

我们有一个字符串 (25;16;67;13;14;.......)

我们需要单独打印出数字。最后一个数字后面没有分号。

输出应该是这样的:

25
16
67
13
14
......

假设我们使用str.findstr.substrsize_t 变量current_pos, prev_pos,我们用来浏览行的while 循环的条件是什么,以便打印出所有数字,而不仅仅是第一个?

【问题讨论】:

标签: c++ string loops while-loop find


【解决方案1】:

您可以使用std::istringstream:

#include <sstream>
#include <iostream>

int main() {

    std::string text("25;16;67;13;14");
    std::istringstream ss(text); 
    std::string token;

    while(std::getline(ss, token, ';'))
    {
        std::cout << token << '\n';
    }

    return 0;
}

Running the above code online 产生以下输出:

25
16
67
13
14

【讨论】:

    【解决方案2】:

    如果您只需要打印字符串中的数字(而不是在数据结构中表示它们),则解决方案非常简单。只需读取整个字符串,然后逐个字符地打印它。如果字符是分号,则打印一个新行。

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
        string input;
        cin >> input;
        for(int i = 0; i < input.length(); i++){
            if(input.at(i) == ';') cout << endl;
            else cout << input.at(i);
        }
    }
    

    【讨论】:

      【解决方案3】:
      using namespace std;
      int main() {
      string a{ "1232,12312;21414:231;23231;22" };
      for (int i = 0; i < a.size(); i++) {
          if (ispunct(a[i])) {
              a[i] = ' ';
          }
      }
      stringstream line(a);
      string b;
      while (getline(line, b, ' ')) {
          cout << b << endl;
      
      }
      

      } //任何标点符号 ",/;:="

      【讨论】:

      • 需要的基本库:#include #include #include #include #include
      【解决方案4】:

      我将通过示例和单行替代解决方案为您的问题提供准确答案。

      请看

      #include <iostream>
      #include <string>
      #include <iterator>
      #include <algorithm>
      #include <regex>
      
      const std::regex re(";");
      
      int main() {
      
          std::string test("25;16;67;13;14;15");
      
          // Solution 1: as requested
          {
              size_t current_pos{};
              size_t prev_pos{};
              // Search for the next semicolon
              while ((current_pos = test.find(';', prev_pos)) != std::string::npos) {
      
                  // Print the resulting value
                  std::cout << test.substr(prev_pos, current_pos - prev_pos) << "\n";
                  // Update search positions
                  prev_pos = current_pos + 1;
              }
              // Since there is no ; at the end, we print the last number manually
              std::cout << test.substr(prev_pos) << "\n\n";
          }
      
          // Solution 2. All in one statement. Just to show to you what can be done with C++
          {
              std::copy(std::sregex_token_iterator(test.begin(), test.end(), re, -1), {}, std::ostream_iterator<std::string>(std::cout, "\n"));
          }
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-18
        • 2011-02-06
        • 2018-04-01
        • 2015-07-20
        • 1970-01-01
        • 2014-02-10
        相关资源
        最近更新 更多