从字符串 C++ 中提取数字 [英] extract numbers from string c++

查看:62
本文介绍了从字符串 C++ 中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,看起来像这样:

I have a string, which looks like this:

foo
$RESULT :(0.2374742, 0.267722, ...up to a million more)
$STATES :{1, 3, 5, ...}
foo 

所以字符串中的某处是结果,紧随其后的是状态,我想将结果保存在一个列表中,将状态保存在另一个列表中.

so somewhere in the string are results and directly after them are the states and I want to save the Results in a list and the states in another list.

我想我需要类似从 $RESULT :("到)"读取每个数字并推送到列表之类的东西,对于国家来说也是如此,但我不知道如何从a"读取字符串到b"并将其内容标记化.

I think I need something like "read from $RESULT :(" to ")" get every number and push to list, same for States, but I dont know how to read a String from "a" to "b" and tokenize its content.

推荐答案

int index = s.find("RESULT: (");
int index2 = s.find("$STATE");

int length = index2 - index;

if (index != string::npos) {
    temp = s.substr(index + 7, length - 8);
}
typedef tokenizer<char_separator<char> > tokenizer;
char_separator<char> sep(",() ");
tokenizer tokens(temp, sep);
for (tokenizer::iterator tok_iter = tokens.begin();
        tok_iter != tokens.end(); ++tok_iter) {
    basic_string<char> tempValue = *tok_iter;

    values.push_back(tempValue);

}

这篇关于从字符串 C++ 中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆