c++ - 在 C/C++ 中两个定界符字符串之间是否有可用的内置函数两个获取字符串?

标签 c++

是否有任何内置函数可用于获取 C++ 中两个分隔符字符串之间的字符串?

输入字符串

(23567)=(58765)+(67888)+(65678)

预期输出

23567
58765
67888
65678

include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> tokenize(const std::string& input)
{
  std::vector<std::string> result;
  std::istringstream stream(input);
  std::string thingie; // please choose a better name, my inspiration is absent today
  while(std::getline(stream, thingie, '('))
  {
    if(std::getline(stream, thingie, ')'))
      result.push_back(thingie);
    else
      throw std::runtime_error("expected \')\' to match \'(\'.");
  }
  return result;
}

void rtc()
{
  ifstream myfile(test.txt);
  if(myfile.is_open())
  while (!myfile.eof())
{
  getline(myfile,line);
auto tokens = tokenize(line);
  for(auto&& item : tokens)
    std::cout << item << '\n';
}
Error C4430 missing type specifier int assumed note:c++ does not support default int
ErrorC2440initializing cannot convertfrom std::vector<_ty>to int
Error C2059syntac error empty declaration
Error C2143syntax error missing;before&&
Error C2059syntax error:')'

最佳答案

使用 std::getline :

#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> tokenize(const std::string& input)
{
  std::vector<std::string> result;
  std::istringstream stream(input);
  std::string thingie; // please choose a better name, my inspiration is absent today
  while(std::getline(stream, thingie, '('))
  {
    if(std::getline(stream, thingie, ')'))
      result.push_back(thingie);
    else
      throw std::runtime_error("expected \')\' to match \'(\'.");
  }
  return result;
}

int main()
{
  std::string test = "(23567)=(58765)+(67888)+(65678)";
  auto tokens = tokenize(test);
  for(auto&& item : tokens)
    std::cout << item << '\n';
}

Live example here .


对于那些不完全相信这个解决方案的强大稳健性的人,我将其专门用于 double在括号之间输入,并使用 boost::lexical_cast验证输入:

#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <vector>

#include <boost/lexical_cast.hpp>

std::vector<double> tokenize(const std::string& input)
{
  std::vector<double> result;
  std::istringstream stream(input);
  std::string thingie; // please choose a better name, my inspiration is absent today
  while(std::getline(stream, thingie, '('))
  {
    if(std::getline(stream, thingie, ')'))
    {
      try
      {
        result.push_back(boost::lexical_cast<double>(thingie));
      }
      catch(...)
      {
        throw std::runtime_error("This wasn't just a number, was it?");
      }
    }
    else
      throw std::runtime_error("expected \')\' to match \'(\'.");
  }
  return result;
}

int main()
{
  std::string test = "(23567)=(58765)+(67888)+(65678)";
  auto tokens = tokenize(test);
  for(auto&& item : tokens)
    std::cout << item << '\n';
  test = "(2h567)=(58765)+(67888)+(65678)";
  tokens = tokenize(test);
}

Live example here .现在去哭泣有多糟糕strtok真的是,或者一般有多糟糕/不便携<regex>目前的实现。另外,对于那些有疑问的人 boost::lexical_cast性能方面,please see the results for yourself .

关于c++ - 在 C/C++ 中两个定界符字符串之间是否有可用的内置函数两个获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261751/

相关文章:

c++ - 提取模板类默认参数

c++ - 在 node.js 绑定(bind)中实现继承

c++ - 处理 WM_ENDSESSION 的正确方法?

c++ - 有没有更简单的方法来编写这部分代码?

c++ - 在多种编程语言中寻找相似精度的数据类型,例如C/C++、D、围棋

c++ - 派生类对象构造后调用虚函数

c++ - 函数调用 CRegKey::QueryStringValue 出错

c++ - 如何在C中打印字符数组中的所有值

C++ 运行时数据类型识别和避免 switch-case

c++ - OpenGL For 循环(随机月亮生成)