如何将c ++ str转换为int? [英] how can i convert c++ str to int?

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

问题描述

可能重复:
c ++将字符串转换为int

我让用户依次输入9个数字.我需要将字符串数字转换为int

I have the user input 9 numbers in sequence. I need to convert the string numbers to an int

string num;
int num_int, product[10];

cout << "enter numbers";
cin >> num;

for(int i =0; i<10; i++){
   product[i] = num[i] * 5; //I need the int value of num*5
}

推荐答案

到目前为止,转换为字符串并再次返回的最简单方法是使用转换函数.

The easiest way by far to convert to a string and back again is to use the conversion functions.

 std::string s="56";
 int i=std::stoi(s);

http://en.cppreference.com/w/cpp/string/basic_string/stol

然后返回

 int i=56;
 std::string s=std::to_string(i);

http://en.cppreference.com/w/cpp/string/basic_string/to_string

当然,如果您正在阅读输入内容,那么您也可以在那里进行输入

of course if you are reading input you might as well do it then and there too

 int i;
 std::cin >> i;

这篇关于如何将c ++ str转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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