【问题标题】:Boost Tokenizer: Extra Space?Boost Tokenizer:额外空间?
【发布时间】:2015-12-13 04:34:32
【问题描述】:

我正在使用 Boost Tokenizer 来删除格式化的坐标,例如 (x,y)。但是,它会在 删除之后添加一个额外的空间。没有空格,但我不知道如何摆脱它。

while (std::getline(input, line)) {
    boost::char_separator<char> sep("(),");
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    tokenizer tok(line, sep);
    for (auto &i : tok) {
        _edges.push_back(i);
    }
}

在向量中,结果如下:

[x][y][空格]

【问题讨论】:

    标签: c++ boost boost-tokenizer


    【解决方案1】:

    “我不知道如何摆脱它。”

    一旦您从文件中提取了一行文本,但在开始解析标记之前,您可以使用 boost::trim() 从提取的行中删除任何前导和尾随空格:

    std::getline(iss, line);
    boost::trim(line);  // <== added
    

    【讨论】:

      【解决方案2】:

      如果行尾表示为\r\n(例如在 Windows 机器上),您将拥有您提到的行为。 getline 使用 \n 作为默认分隔符。

      #include <iostream>
      #include <vector>
      #include <sstream>
      #include <boost/tokenizer.hpp>
      
      int main() {
      
        std::string line;;
        std::istringstream iss("(1,2)\r\n");
      
        std::getline(iss, line);
        std::cout << "length: " << line.length() << std::endl; // 6, includes '\r'
      
        boost::char_separator<char> sep("(),");
        typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
        tokenizer tok(line, sep);
        for (auto &i : tok) {
          std::cout << "ith tok: " << i << std::endl;
        }
      
        return 0;
      }
      

      打印:

      length: 6
      ith tok: 1
      ith tok: 2
      ith tok: 
      

      要解决此问题,您可以更改分隔符或编写从流中解析坐标的方法,如下所示:

      #include <iostream>
      #include <algorithm>
      #include <vector>
      #include <sstream>
      #include <iterator>
      
      template <typename CharT, typename CharTraits, typename T>
      std::basic_istream<CharT, CharTraits>& operator >>(std::basic_istream<CharT, CharTraits>& is, std::vector<T>& v)
      {
        typedef typename std::vector<T> vector_type;
        typedef typename vector_type::size_type size_type;
      
        CharT ch;
        const size_type size = 2;
      
        vector_type s{0,0};
        if(is >> ch && ch != '(')
        {
          is.putback(ch);
          is.setstate(std::ios_base::failbit);
        }
        else if(!is.fail())
        {
          for(size_type i = 0; i != size; ++i)
          {
            if(is >> std::ws >> s[i] >> ch && ch != ',')
            {
              is.putback(ch);
              if(i < size - 1)
                is.setstate(std::ios_base::failbit);
              break;
            }
          }
      
          if(is >> ch && ch != ')')
          {
            is.putback(ch);
            is.setstate(std::ios_base::failbit);
          }
        }
      
        if(!is.fail())
          v.swap(s);
      
        return is;
      }
      
      int main() {
      
        std::vector<int> v;
        std::istringstream is("(1, 2)\r\n");
        is >> v;
      
        std::copy(std::begin(v), std::end(v), std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
      
        return 0;
      }
      

      打印

      1 2
      

      运行online

      【讨论】:

        猜你喜欢
        • 2011-12-17
        • 2012-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多