Compdigitec Labs

« | Home | »

Converting between int and std::string in C++

By admin | November 20, 2010

#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;

int main() {
	// convert int to string
	int some_int = 52;
	stringstream ss;
	ss << some_int;
	string result_s;
	ss >> result_s;
	// result_s = 52
	cout << result_s << endl;

	// convert string to int
	string some_string = "1234";
	int result_i = atoi(some_string.c_str());
	// result_i = 1234
	cout << result_i << endl;
}

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Linux, Windows | 20 Comments »

20 Responses to “Converting between int and std::string in C++”

  1. Russell Says:
    September 14th, 2011 at 08:42

    What about converting from std::string back to int

  2. admin Says:
    April 28th, 2012 at 19:13

    @Russell:

    See atoi() or sscanf() in the C standard library.

  3. funkymedia Says:
    March 23rd, 2022 at 03:04

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  4. สล็อตวอเลท ไม่มีขั้นต่ำ Says:
    March 25th, 2022 at 20:58

    … [Trackback]

    […] Find More on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  5. good cvv shop Says:
    April 9th, 2022 at 00:02

    … [Trackback]

    […] Read More on on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  6. Derry call girl Says:
    June 28th, 2022 at 01:55

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  7. web design for psychologists Says:
    July 1st, 2022 at 19:46

    … [Trackback]

    […] Read More Information here on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  8. guardian armory Says:
    July 13th, 2022 at 06:32

    … [Trackback]

    […] Read More on to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  9. nova88 Says:
    July 15th, 2022 at 04:15

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  10. แทงบอลโลก Says:
    August 17th, 2022 at 07:12

    … [Trackback]

    […] Read More on on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  11. Post Says:
    August 17th, 2022 at 12:47

    … [Trackback]

    […] There you can find 65978 additional Info to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  12. benelli rifles for sale Says:
    August 26th, 2022 at 12:58

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  13. www.free-local-hookups.com/baltimore-personals/ Says:
    September 24th, 2022 at 14:57

    … [Trackback]

    […] Find More here to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  14. pebblecreek home for sale Says:
    December 14th, 2022 at 10:52

    … [Trackback]

    […] There you can find 12350 more Info on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  15. 7277433742 Says:
    December 21st, 2022 at 08:43

    … [Trackback]

    […] Here you can find 69737 more Info on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  16. tu peux vérifier Says:
    December 26th, 2022 at 14:39

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  17. https://y2mate.tools/en/youtube-mp3 Says:
    January 14th, 2023 at 21:04

    … [Trackback]

    […] Read More on to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  18. Dividend Says:
    January 28th, 2023 at 10:47

    … [Trackback]

    […] Find More on to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  19. click here Says:
    February 3rd, 2023 at 17:56

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

  20. พนันบอลออนไลน์ Says:
    March 9th, 2023 at 09:42

    … [Trackback]

    […] There you can find 88695 additional Info to that Topic: compdigitec.com/labs/2010/11/20/converting-between-int-and-string-in-c/ […]

Comments