- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to parse a string to an int in C++?
You can use a string stream to parse an int in c++ to an int. You need to do some error checking in this method.
example
#include<iostream> #include<sstream> using namespace std; int str_to_int(const string &str) { stringstream ss(str); int num; ss >> num; return num; } int main() { string s = "12345"; int x = str_to_int(s); cout << x; }
Output
This will give the output −
12345
In the new C++11, there are functions for that: stoi(string to int), stol(string to long), stoll(string to long long), stoul(string to unsigned long), etc.
Example
You can use these functions as follows −
#include<iostream> using namespace std; int main() { string s = "12345"; int x = stoi(s); cout << x; }
Output
This will give the output −
12345
- Related Articles
- How to parse a string into a nullable int in C#?
- How to parse a string to float or int in python?
- How to convert an int to string in C++?
- How to concatenate a std::string and an int in C++?
- How to check if a C/C++ string is an int?
- How to convert a String to an int in Java
- How to convert a Java String to an Int?
- How to convert a string into int in C#?
- How to parse json string in android?
- Concatenate string to an int value in Java
- How to create a tuple with string and int items in C#?
- How to convert a string to int in Lua programming?
- How to parse a string from a JavaScript array?
- How to convert int to String in java?
- How to convert int to string in Python?

Advertisements