首页 > C/C++如何将string内容分离?

C/C++如何将string内容分离?

假设有以下字符串:

string str="123 45 56 89";

现把其中连在一起的数字以整型格式单独取出来(int a=123,int b=45),有没有什么好方案?


直接上函数?调用库函数,直接分离,或者


for() {
if(a[] == ' ')
while
b[] = i--;
}

int a, b, c, d;
sscanf("123 45 56 89", "%d %d %d %d", &a, &b, &c, &d);

http://www.cplusplus.com/reference/cstdio/sscanf/


不知道有几个数的情况下,可以用istringstream

【热门文章】
【热门文章】