简体   繁体   English

将字符串转换为int数组

[英]Converting string into int array

I have a following problem. 我有以下问题。 I want to convert a string like "10 15 30" to an int array with ints 10, 15, 30 . 我要像一个字符串转换"10 15 30"与整型一个int数组10, 15, 30 I searched in google a lot, but usually solutions included vectors (which I am not familiar with) or other more complex solutions. 我在Google中搜索了很多,但通常解决方案包括矢量(我不熟悉)或其他更复杂的解决方案。 I found a code like this: 我发现了这样的代码:

#include <cstdio>
void foo ( char *line ) {
    int num, i = 0, len;
    while ( sscanf( line, "%d%n", &num, &len) == 1 ) {
        printf( "Number %d is %d; ", i, num );
        line += len;    
        i++;            
    }
}
int main ( ) {
    char test[] = "12 45 6 23 100";
    foo( test );
    return 0;
}

It works and extracts numbers from string in a way I wanted, but I don't understand part with: 它以我想要的方式工作并从字符串中提取数字,但是我不理解其中的一部分:

line += len;

Can someone explain how it works? 有人可以解释它是如何工作的吗? Why are we adding len (which is int ) to the string? 我们为什么要在字符串中加上len (即int )?

Solution for C++: C ++解决方案:

#include <iostream>
#include <sstream>
#include <vector>

std::vector< int > foo ( char *c_str ) {
    std::istringstream line( c_str );
    std::vector< int > numbers;
    for ( int n; line >> n; )
        numbers.push_back( n );
    return numbers;
}

int main ( ) {
    char test[] = "12 45 6 23 100";
    std::vector< int > numbers = foo( test );
    for ( int n : numbers )
        std::cout << n << ' ';
    return 0;
}

Output: 输出:

12 45 6 23 100

%n specifies the number of characters accessed by sscanf in its one execution and saved it in variable len. %n指定sscanf在一次执行中访问的字符数,并将其保存在变量len中。 so line + =len; 所以line + =len; is incrementing variable line , with number of characters accessed by sscanf 是递增变量line ,由sscanf访问的字符数

line + =len; is nothing but line =line+ len; 只不过是line =line+ len;

sscanf( line, "%d%n", &num, &len) sscanf(line,“%d%n”,&num,&len)

This line reads a number from line, and put the digit into num and number of characters read into len. 该行从行中读取一个数字,并将数字放入num中,并将字符数放入len中。

We need len to move pointer to unread portion of the string. 我们需要len将指针移动到字符串的未读部分。

Does it make sense ? 是否有意义 ?

Useful links: scanf sscanf 有用的链接: scanf sscanf

line += len; is equivalent to line = line + len; 等价于line = line + len;

line is a char pointer, thus the code increases the pointer by len , so that the pointer points to the next number, every time the loop is executed. line是一个char指针,因此代码在每次执行循环时都会将指针增加len ,从而使该指针指向下一个数字。 Note than len is updated by the call to sscanf() . 请注意,通过调用sscanf()来更新len

The "string" is a pointer to a character buffer. “字符串”是指向字符缓冲区的指针。 You are performing pointer arithmetic to increment the sscanf to parse the next int characters from the buffer. 您正在执行指针算术,以增加sscanf来解析缓冲区中的下一个int字符。 So, in your example: 因此,在您的示例中:

char test[] = "12 45 6 23 100"; char test [] =“ 12 45 6 23 100”;

say *line points to test, and has some pointer value, we don't care what the value is per-se, so let's say it is 1000. After the 1st call to sscanf, you read "12" in. That is two characters, so len should return 2. line then gets set to 1002, and is now "pointing" to the next set of characters in the buffer. 说* line指向测试,并具有一些指针值,我们不在乎该值的本质,所以说它是1000。第一次调用sscanf之后,您读入“ 12”。这是两个字符,因此len应该返回2。然后将行设置为1002,现在“指向”缓冲区中的下一组字符。 sscanf reads that, and this continues until no more characters in the buffer. sscanf读取该内容,并一直持续到缓冲区中没有更多字符为止。

Look at sscanf docs . 查看sscanf docs It receive format and additional params. 它接收格式和其他参数。

In params you have "%d%n" which you explain using format table from docs . 在params中,您具有"%d%n" ,可使用docs中的格式表进行解释。

d or u du

Decimal integer 小数整数

Any number of decimal digits (0-9), optionally preceded by a sign (+ or -). 任意数量的十进制数字(0-9),可以选择在其后加上一个符号(+或-)。 d is for a signed argument, and u for an unsigned. d代表有符号参数,u代表无符号参数。

n ñ

Count 计数

No input is consumed. 不消耗任何输入。 The number of characters read so far from stdin is stored in the pointed location. 到目前为止,从stdin读取的字符数存储在指定的位置。

So you read integer from string and number of chars this integer consist from. 因此,您从字符串和整数组成的字符数中读取整数。

After reading part of string you move pointer into number of read chars. 读取字符串的一部分后,将指针移到读取字符数中。

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

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