c++ - 在 boost::char_separator 中使用空格作为分隔符

标签 c++ boost

我只是想使用 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;
}

最佳答案

It just keeps throwing the PM when the PM is separated from 00 by a space.

这不是 throw 。 PM 只是不存在于 str1 中,因为 std::cin 也使用空格作为分隔符。替换

cin >>  str1;

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

关于c++ - 在 boost::char_separator 中使用空格作为分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131040/

相关文章:

c++ - 在 block 中#defining 真的是一种代码味道吗?

c++ - 通过连续内存实现多态性

c# - 从枚举值中获取字符串的优雅方法?

c++ - 启用 boost::lexical_cast 以针对小于双范围的值抛出超出范围的错误

c++ - 在 C 代码中使用 boost::bind() 会起作用吗?

c++ - C/C++ : Inherent ambiguity of "\xNNN" format in literal strings

c++ - 通过 shell 脚本和 C++ 获取当前前台应用程序的名称

visual-studio - 使用 Visual Studio 测试适配器 boost 单元测试 - 设置工作目录

c++ - 实现上的巨大差异?

c++ - 为什么boost的interprocess_condition死锁在notify_one?