简体   繁体   English

从字符串C ++中提取数字

[英]extract numbers from string 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. 因此,字符串中的某处是结果,紧随其后的是状态,我想将结果保存在一个列表中,并将状态保存在另一个列表中。

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. 我想我需要类似“从$ RESULT :(“至”)中读取”之类的东西来获取每个数字并推送至列表,这与各州相同,但是我不知道如何从“ a”到“ b”读取字符串并将其标记化内容。

您可以使用boost tokenizer :这是仅标头的库,使用方便

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);

}

Tokenization in C++ is often done with getline, used so: getline(input stream, string where to save it, seperator character); C ++中的标记化通常是使用getline来完成的,因此可以这样使用:getline(输入流,保存它的字符串,分隔符);

Try building a class for reading, that saves every line to a collection, then tokenize each line as needed and send to needed collections in an algorithm. 尝试构建一个用于读取的类,该类将每一行保存到一个集合中,然后根据需要标记每行并通过算法发送到所需的集合。

您可以使用strtok()库函数- http://www.cplusplus.com/reference/clibrary/cstring/strtok

Find the first acurance of the '(' and then the first of ')' sign and get the substring between the two indexes (first is the start and the length is end - start) and then you can do the same for the substring after the first ')' sign (for the states). 找到'(',然后是')'的第一个递归,获得两个索引之间的子字符串(第一个是开始,长度是结束-开始),然后可以对之后的子字符串执行相同的操作第一个“)”符号(用于各州)。

temp_str = input_str

do twice {
    start    = findChar(temp_str, '(');
    end      = findChar(temp_str, ')')
    len      = end - start + 1
    result   = substr(temp_str, start, len);  

    save_result_to_file(result)

    temp_str = substr(temp_str, end + 1);
}

Don't remember the exact c++ commands but you will have them for sure. 记不清确切的c ++命令,但是您肯定会使用它们。

#include <string>
#include <vector>
using namespace std;

int main()
{
  //This is your source string
  string Source = "foo $RESULT :(0.2374742, 0.267722) $STATES :{1, 3, 5} fo0";
  //Get the $RESULT section of the string, encapsulated by ( )
  string Results = Source .substr(Source .find("(") + 1, (Source .find(")") - Source .find("(")) - 1);

  //Get the $STATES section of the string, encapsulated by { }
  string States = Source .substr(Source .find("{") + 1, (Source .find("}") - Source .find("{")) - 1);

  vector<double> ResultsList;
  vector<int> StatesList;

  //While the Results string still has remaining ", " token/seperators in it
  while(Results.find(", ") != string::npos)  
  {
    //Get the next value and insert it into the vector (converting it from string to float using atof)
    ResultsList.push_back(atof(Results.substr(0, Results.find(", ")).c_str()));
    //Crop that off the oringal string
    Results = Results.substr(Results.find(", ") + 2);  
  }
  //Push the final value (no remaning tokens) onto the store
  ResultsList.push_back(atof(Results.c_str()));

  //Exactly the same operation with states, just using atoi to convert instead
  while(States .find(", ") != string::npos)  
  {  
    StatesList.push_back(atoi(States.substr(0, States .find(", ")).c_str()));  
    States = States.substr(States.find(", ") + 2);  
  }  
  StatesList.push_back(atoi(States.c_str()));
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM