将字符串转换为size_t [英] cast string to size_t

查看:82
本文介绍了将字符串转换为size_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为0 | first_name | last_name | ...的字符串我可以将
拆分为向量< stringby" |"令牌。


目前获取行号我正在使用size_t row_number =

atoi(buf_string [0] .s_scr())抛出警告。


转换字符串2的正确方法是什么? to size_t 2?


提前致谢。

I have a string with format "0|first_name|last_name|..." which i can
split to a vector<stringby the "|" token.

At the moment to get the row number I''m using size_t row_number =
atoi(buf_string[0].s_scr()) which throws warnings.

What is the correct way to convert a string "2" to size_t 2?

Thanks in advance.

推荐答案

#include< sstream> ;


std :: istringstream iss(" a");

size_t size;

iss> size;


您可以通过调用iss.fail来检查转换是否失败()

#include <sstream>

std::istringstream iss("a");
size_t size;
iss >size;

You can check whether conversion failed by calling iss.fail()


您应该替换std的参数: :istringstream iss(a)带有你要转换的

值。

You should replace parameter of std::istringstream iss("a") with a
value you want to convert.


pkirk25写道:
pkirk25 wrote:

我有一个格式为0 | first_name | last_name | ...的字符串我可以将
拆分为向量< stringby" |"令牌。


目前获取行号我正在使用size_t row_number =

atoi(buf_string [0] .s_scr())抛出警告。
I have a string with format "0|first_name|last_name|..." which i can
split to a vector<stringby the "|" token.

At the moment to get the row number I''m using size_t row_number =
atoi(buf_string[0].s_scr()) which throws warnings.



我猜你的意思是s_scr()实际上是c_str()。如果我的猜测

是正确的,那么以下内容应该给你一个数字


size_t row_number = size_t(atoi(buf_string [0] .c_str())) ;


注意int-to-size_t转换(不是强制转换。)

I am guessing what you mean by s_scr() is in fact c_str(). If my guess
is correct then the following should get you a number

size_t row_number = size_t(atoi(buf_string[0].c_str()));

Notice the int-to-size_t conversion (not a cast.)


>

转换字符串2的正确方法是什么? to size_t 2?


提前致谢。
>
What is the correct way to convert a string "2" to size_t 2?

Thanks in advance.



Ben

Ben


这篇关于将字符串转换为size_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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