使用空格作为boost::char_separator中的分隔符

Using space as delimiter in boost::char_separator

本文关键字:separator 分隔符 char 空格 boost      更新时间:2023-10-16

我只是想用Boost标记一些时间。输入5:00 PM,输出<5> <00> <PM>。问题是,我已经尝试了各种各样的东西,但它只输出<5> <00>。然而,如果我输入5:00PM,我得到的输出是<5> <00PM>。我如何使boost接受空间作为令牌(旁边:)?当PM和00之间有空格分隔时,它会继续抛出PM。

#include "stdafx.h"
#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
using namespace std;
int main(){
    string str1;
cin >>  str1;
typedef boost::tokenizer<boost::char_separator<char> > 
tokenizer;
boost::char_separator<char> sep(":t ");
string t1 (str1);
tokenizer tokens1(t1, sep);
  for (tokenizer::iterator tok_iter = tokens1.begin();
   tok_iter != tokens1.end(); tok_iter++)
  {cout << "<" << *tok_iter << "> ";}

system("pause");
return 0;
}

当PM与00之间有空格分隔时,它会继续抛出PM。

这不是投掷。PMstr1中不存在,因为std::cin也使用空间作为分隔符。替换

cin >>  str1;

std::getline(std::cin, str1);