In this article, we will look at how we can convert a string to int in C++. Often, we may need to convert numeral strings into integers. We may need certain ways to quickly do the conversion

在本文中,我们将研究如何在C ++中将字符串转换为int。 通常,我们可能需要将数字字符串转换为整数。 我们可能需要某些方法来快速进行转换

Let’s take a look at various ways to convert a string to int.

让我们看一下将字符串转换为int的各种方法。



方法1 –使用std :: stoi (Method 1 – Using std::stoi)

To convert a std::string object to an integer, this is the recommended option in C++. This takes in a string as input and returns an integer.

要将std::string对象转换为整数,这是C ++中的推荐选项。 这将字符串作为输入并返回整数。

Here is a simple example:

这是一个简单的示例:


#include <iostream>
#include <string>using namespace std;int main() {string inp = "102";cout << "Input String: " << inp << endl;// Use std::stoi() to convert string to integerint res = stoi(inp);cout << "Integer: " << res << endl;return 0;
}

Output

输出量


Input String: 102
Integer: 102

This will work for negative value strings also.

这也适用于负值字符串。

However, this will throw a std::invalid_argument exception if the string is not of the proper format.

但是,如果字符串格式不正确,则会抛出std::invalid_argument异常。

Because of this, we must use Exception Handling in our code, if the string is not validated.

因此,如果未验证字符串,则必须在代码中使用异常处理。


#include <iostream>
#include <string>using namespace std;int main() {string inp = "Hello";cout << "Input String: " << inp << endl;// Use std::stoi() to convert string to integertry {// Wrap up code in try-catch block if string is not validatedint res = stoi(inp);cout << "Integer: " << res << endl;}catch(std::invalid_argument e) {cout << "Caught Invalid Argument Exception\n";}return 0;
}

Output

输出量


Input String: Hello
Caught Invalid Argument Exception

Hopefully that gives you a good idea of how you can convert a String into a Integer in C++.

希望这对如何在C ++中将String转换为Integer有所帮助。

We’ll also present one more way, using the C-style atoi() function.

我们还将使用C风格的atoi()函数展示另一种方法。



方法2 –使用atoi() (Method 2 – Using atoi())

We can convert a C string (char*) into an integer, using atoi(char* str).

我们可以使用atoi(char* str)将C字符串( char* )转换为整数。

We can apply this logic to our std::string object, by first converting into a C string.

通过首先转换为C字符串,可以将此逻辑应用于std::string对象。


#include <iostream>
#include <string>using namespace std;int main() {string inp = "120";cout << "Input String: " << inp << endl;// Convert std::string to C-stringconst char* c_inp = inp.c_str();// Use atoi() to convert C-string to integerint res = atoi(c_inp);cout << "Integer: " << res << endl;return 0;
}

Output

输出量


Input String: 120
Integer: 120

As you can see, this gives us the same result as before!

如您所见,这为我们提供了与以前相同的结果!



结论 (Conclusion)

In this article, we learned how we could convert a string to int in C++.

在本文中,我们学习了如何在C ++中将字符串转换为int。

For similar content, do go through our tutorial section on C++ programming.

对于类似的内容,请阅读我们有关C ++编程的教程部分 。

参考资料 (References)

  • StackOverflow Question on converting string to int in C++在C ++中将字符串转换为int的StackOverflow问题


翻译自: https://www.journaldev.com/41723/convert-string-int-c-plus-plus

在C ++中将字符串转换为int相关推荐

  1. python怎么将字符串变成int,如何在Python中将字符串转换为int?

    我的小示例应用程序的输出如下: Welcome to the Calculator! Please choose what you'd like to do: 0: Addition 1: Subtr ...

  2. 在Python中将十六进制字符串转换为int

    如何在Python中将十六进制字符串转换为int? 我可能将其设置为" 0xffff "或" ffff ". #1楼 在上述Dan的答案中加上:如果为int() ...

  3. scala字符串转int_如何在Scala中将十六进制字符串转换为int?

    scala字符串转int The hex string is also known as Hexadecimal Number String is a number created using hex ...

  4. linux 字符转int,如何在Linux内核中将char []字符串转换为int?

    如何在linux内核中将char []转换为int 验证输入的文本实际上是一个int? int procfile_write(struct file *file, const char *buffer ...

  5. 如何在Java中将String转换为int?

    如何在Java中将String转换为int ? 我的字符串仅包含数字,我想返回它代表的数字. 例如,给定字符串"1234" ,结果应为数字1234 . #1楼 好吧,要考虑的一个非 ...

  6. 如何在Java中将String转换为int

    在本教程中,我们将看到将Java中的String转换为int(或Integer)的各种方法. 您可以使用以下任何一种方式: –使用Integer.parseInt(string) –使用Integer ...

  7. android 字符串 转公式,java – 在android中将字符串转换为bigdecimal

    嗨我怎么能在android中将字符串转换为bigdecimal. 这是我的第一项活动: public class ViewCartActivity extends Activity { String ...

  8. 将Python字符串转换为Int,将Int转换为String

    In this tutorial, we will learn how to convert python String to int and int to String in python. In ...

  9. C++中将字符串转换为数字

    C++中将字符串转换为数字 法一: int t = s[len - 1]-'0';//减去一个 '0' 是 将最后一位字母转换成数字 例如:这个判断基偶性就是为了防止越界所以将数组转换为数组传入,再将 ...

最新文章

  1. mongodb的简单使用
  2. 2020 图算法工程师面试基础、要点
  3. android自定义进度条渐变色View,不使用任何图片资源
  4. linux中 ln -s 软链接
  5. 计算机应用基础形考作业3Excel,国家开放大学《计算机应用基础》考试与答案形考任务模块3 模块3 Excel 电子表格系统—客观题答案(2020年整理).pptx...
  6. @Transactional 实现原理
  7. Python笔记-使用U2自动登录某APP及watcher使用
  8. 外网DNS系统外网访问及邮件系统外网域名访问问题
  9. linux 添加deepin ppa,Deepin 添加PPA源问题
  10. java class类
  11. mybatis实体类类型别名
  12. 计算机配色在纺织中的应用,计算机配色在印染行业的应用
  13. Apache的安装教程
  14. 游泳池 (Standard IO)
  15. DirectX12 - Triangle Culling and Winding Order(三角形的剔除与绕序)
  16. 用c语言怎么打e的x次方
  17. node版本更新和npm版本更新
  18. 深入分析Java Web技术内幕(修订版) 读书笔记
  19. 20 21九死一生、22上半年读20本书(含15本管理书单/笔记):继续百年征程
  20. 写代码的时候,竟然发现了10+个【了不得】的网站,必须分享

热门文章

  1. HTML5之本地存储localstorage
  2. Scrum Meeting day 2
  3. php -- 目录、路径、磁盘
  4. 如何“打败”CAP定理 【转】
  5. nhibernate源码分析之六: Criteria数据加载
  6. mysql 8.0以上重置密码
  7. [转载] python缩进报错_python缩进报错
  8. [转载] python中@property装饰器
  9. [转载] python数字类型(一)
  10. Vivado中的Incremental Compile增量编译技术详解