简体   繁体   English

C ++解析,从字符串中获取可变数量的整数

[英]C++ parse, fetch a variable number of integers from a string

I have a set of strings which looks like 我有一组看起来像的字符串
"4 7 14 0 2 blablabla" “4 7 14 0 2 blablabla”
"3 8 1 40 blablablablabla" “3 8 1 40 blablablablabla”
... ...

The first number N correspond to how many numbers will follow. 第一个数字N对应于将跟随多少个数字。
Basically, the format of a string is N+1 numbers, separated with a space, followed by an unknown number of irrelevant characters at the end that I don't need. 基本上,字符串的格式是N + 1个数字,用空格分隔,后面跟着我不需要的未知数量的无关字符。

How can I get all the numbers in variables or a dynamic structure given that I don't know the number N in advance ? 考虑到我事先不知道数字N,我如何得到变量或动态结构中的所有数字?

In other words, I'd like something like: 换句话说,我想要像:

sscanf(s, "%d %d %d %d",&n,&x,&y,&z);

which would work no matter how many numbers in the string. 无论字符串中有多少个数字都可以使用。

The first thing would be to break up the input into lines, by using getline to read it. 首先是通过使用getline来读取它,将输入分解为行。 This will greatly facilitate error recovery and resynchronization in case of error. 这将极大地促进错误恢复和重新同步,以防出现错误。 It will also facilitate parsing; 它还有助于解析; it's the strategy which should be adopted almost every time a newline in the input is significant. 这是几乎每次输入中的换行都很重要时应该采用的策略。 After that, you use a std::istringstream to parse the line. 之后,使用std::istringstream来解析该行。 Something like: 就像是:

std::vector<std::vector<int> > data;
std::string line;
while ( std::getline( input, line ) ) {
    std::istringstream l( line );
    int iCount;
    l >> iCount;
    if ( !l || iCount < 0 ) {
        //  format error: line doesn't start with an int.
    } else {
        std::vector<int> lineData;
        int value;
        while ( lineData.size() != iCount && l >> value ) {
            lineData.push_back( value ) ;
        }
        if ( lineData.size() == iCount ) {
            data.push_back( lineData );
        } else {
            //  format error: line didn't contain the correct
            //  number of ints
        }
    }
}
int input;
std::vector<int> ints;
while(std::cin >>  input)  //read as long as there is integer
       ints.push_back(input);
std::cin.clear(); //clear the error flag
//read the remaining input which most certainly is non-integer
std::string s = "3 54 -4 42 fdsvadsdsberwte";
std::istringstream source(s);

std::vector<int> ints;

int num = 0;
int temp = 0;

if (source >> num)  // read the first number and if it succeeds, read the rest
{
    while(source >> temp) ints.push_back(temp);
    // Here, I'd compare the size of the vector with what was
    // expected (num) and act accordingly.
}
else
{
     // format error 
} 

Initialize an istringstream with your string, and read from it: 使用您的字符串初始化istringstream ,并从中读取:

std::istringstream ss(s);

int N;
ss >> N;

std::vector<int> a(N); //int* a = new int[N];
for (int i=0;i<N;i++)
    ss >> a[i];

You could do it something like this: 你可以这样做:

std::string buffer;
while(std::getline(in, buffer)) {    // read a line
    std::istringstream str(buffer);  // put line in a stringstream for parsing
    int count;
    str >> count;                    // read first number
    std::vector<int> numbers(count); // reserve space for other numbers
    for(int i = 0; i < count; i++) { // read other numbers
        str >> numbers[i];
    }
}    

You can use dynamic array and a linked list, of dynamic array type. 您可以使用动态数组和动态数组类型的链表。 After reading a line initialize the array using number N and insert the numbers sequentially on each index. 读取一行后,使用数字N初始化数组,并在每个索引上按顺序插入数字。 Insert the node containing line data to linked list. 将包含行数据的节点插入链接列表。

Pseudo code: 伪代码:

int *lineNumbers;
lineNumbers = new int[N];

List<int*> data;
data.insert(lineNumbers);

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

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