【问题标题】:Parse a string into two double将一个字符串解析为两个双精度
【发布时间】:2013-04-25 19:18:27
【问题描述】:

我有一个格式为########s###.## 的字符串 其中 #### 只是几个数字,第二部分通常是小数,但并非总是如此。

我需要将两个数字分开,并将它们设置为两个双精度数(或其他一些有效的数字类型。

我只能为此使用标准方法,因为运行它的服务器只有标准模块。

我目前可以使用 find 和 substr 获取第二块,但不知道如何获取第一块。我还没有做任何将第二部分更改为数字类型的操作,但希望这会容易得多。

这是我所拥有的:

    string symbol,pieces;

    fin >> pieces; //pieces is a string of the type i mentioned #####s###.##
    unsigned pos;
    pos = pieces.find("s");
    string capitals = pieces.substr(pos+1);
    cout << "Price of stock " << symbol << " is " << capitals << endl;

【问题讨论】:

  • 从索引0开始取长度为pos的子串怎么样?

标签: c++ string parsing double


【解决方案1】:

istringstream 让事情变得简单。

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

int main(int argc, char* argv[]) {
  std::string input("123456789s123.45");

  std::istringstream output(input);

  double part1;
  double part2;

  output >> part1;

  char c;

  // Throw away the "s"
  output >> c;

  output >> part2;

  std::cout << part1 << ", " << part2 << std::endl;

  return 0;
}

【讨论】:

    【解决方案2】:

    您可以在调用substr 时指定计数和偏移量:

    string first = pieces.substr(0, pos);
    string second = pieces.substr(pos + 1);
    

    【讨论】:

      【解决方案3】:

      您可以执行与第二部分相同的操作:

       unsigned pos;
       pos = pieces.find("s");
       string firstPart = pieces.substr(0,pos);
      

      【讨论】:

        【解决方案4】:

        此代码将根据需要拆分string 并将它们转换为double,也可以轻松地将其更改为转换为float

        #include <iostream>
        #include <sstream>
        #include <string>
        #include <stdexcept>
        
        class BadConversion : public std::runtime_error {
        public:
          BadConversion(std::string const& s)
            : std::runtime_error(s)
            { }
        };
        
        inline double convertToDouble(std::string const& s,
                                      bool failIfLeftoverChars = true)
        {
          std::istringstream i(s);
          double x;
          char c;
          if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
            throw BadConversion("convertToDouble(\"" + s + "\")");
          return x;
        }
        
        int main()
        {
          std::string symbol,pieces;
        
            std::cin >> pieces; //pieces is a string of the type i mentioned #####s###.##
            unsigned pos;
            pos = pieces.find("s");
            std::string first = pieces.substr(0, pos);
            std::string second = pieces.substr(pos + 1);
            std::cout << "first: " << first << " second " << second << std::endl;
            double d1 = convertToDouble(first), d2 = convertToDouble(second) ;
            std::cout << d1 << " " << d2 << std::endl ;
        }
        

        仅供参考,我从我的previous answers 中获取了转换代码。

        【讨论】:

          【解决方案5】:

          抢第一块很容易:

          string firstpiece = pieces.substr(0, pos);
          

          至于转换为数字类型,我发现sscanf() 对此特别有用:

          #include <cstdio> 
          
          std::string pieces;
          fin >> pieces; //pieces is a string of the type i mentioned #####s###.##
          
          double firstpiece = 0.0, capitals = 0.0;
          std::sscanf(pieces.c_str() "%lfs%lf", &firstpiece, &capitals);
          ...
          

          【讨论】:

            【解决方案6】:

            有些人会抱怨这不是 C++-y 但这是有效的 C++

            
              char * in = "1234s23.93";
              char * endptr;
              double d1 = strtod(in,&endptr);
              in = endptr + 1;
              double d2 = strtod(in, &endptr); 
            

            【讨论】:

              猜你喜欢
              • 2012-10-21
              • 2010-11-27
              • 1970-01-01
              • 2012-03-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多