【问题标题】:Split String using C++ [duplicate]使用 C++ 拆分字符串 [重复]
【发布时间】:2013-04-26 18:20:26
【问题描述】:

我在使用纯 C++ 拆分字符串时遇到问题

字符串总是这样

12344//1238

第一个 int 然后 // 然后是第二个 int。

需要帮助来获取两个 int 值并忽略 //

【问题讨论】:

  • 什么样的问题?您可能想查看右侧的第一个相关链接。
  • 我不知道如何拆分字符串以获得两个 int 并忽略 //
  • 你想用这个字符串做什么?有代码吗?

标签: c++ string split


【解决方案1】:
string org = "12344//1238";

size_t p = org.find("//");
string str2 = org.substr(0,p);
string str3 = org.substr(p+2,org.size());

cout << str2 << " "<< str3;

【讨论】:

  • 将分隔符定义为字符串似乎更好。然后做p+sep.size() 而不是(脆弱的)p+2
  • 谢谢,效果很好!它成功了!!!!
  • @Madbreaks 我这样做是因为 OP 说字符串始终采用这种格式。只是为了简单起见。
【解决方案2】:

为什么我们不能使用 sscanf?

char os[20]={"12344//1238"};
int a,b;
sscanf(os,"%d//%d",a,b);

Reference

【讨论】:

    【解决方案3】:

    看看strtok函数

    【讨论】:

    • 我不介意被否决,但请说出原因
    • 它可能比 C++ 更多 C,并且可能不是线程安全的,但它确实拆分字符串。
    • 感谢@chris,我同意更标准的 C。但由于 C++ 是 C 的超集,因此使用它是完全合法的。
    【解决方案4】:

    这应该拆分并转换为整数:

    #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 convertToInt(std::string const& s,
                                  bool failIfLeftoverChars = true)
    {
      std::istringstream i(s);
      int x;
      char c;
      if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
        throw BadConversion("convertToInt(\"" + s + "\")");
      return x;
    }
    
    int main()
    {
        std::string pieces = "12344//1238";
    
        unsigned pos;
        pos = pieces.find("//");
        std::string first = pieces.substr(0, pos);
        std::string second = pieces.substr(pos + 2);
        std::cout << "first: " << first << " second " << second << std::endl;
        double d1 = convertToInt(first), d2 = convertToInt(second) ;
        std::cout << d1 << " " << d2 << std::endl ;
    }
    

    【讨论】:

      【解决方案5】:

      我能想到的最简单的方法:

      #include <string>
      #include <sstream>
      #include <iostream>
      
      using namespace std;
      
      void main ()
      {
       int int1, int2;
       char slash1, slash2;
      
       //HERE IT IS:
       stringstream os ("12344//1238");
       os>> int1 >> slash1 >> slash2 >> int2;
       //You may want to verify that slash1 and slash2 really are /'s
      
       cout << "I just read in " << int1 << " and " << int2 << ".\n";
      
       system ("pause");
      }
      

      也很好,因为它很容易重写——比如说,如果你决定读取由其他东西分隔的整数。

      【讨论】:

        【解决方案6】:

        将整数作为字符串。 然后字符串将包含数字和 // 符号。 接下来,您可以运行一个简单的 for 循环来查找字符串中的“/”。 符号之前的值存储在另一个字符串中。 当 '/' 出现时,for 循环将终止。你现在有了第一个的索引 '/' 象征。 增加索引并使用另一个 for 循环复制字符串的其余部分,在另一个 细绳。 现在你有两个单独的字符串。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 2020-01-07
          • 2012-09-18
          • 2021-04-01
          • 2010-09-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多