LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2004, 09:40 PM   #1
Slaxx
LQ Newbie
 
Registered: Sep 2004
Location: Purdue University
Distribution: Slackware 10.0
Posts: 11

Rep: Reputation: 0
C++ std::string to int


Is there a simple std::string member function that I can use to quickly convert to an int? In other words, I want to know if there's a FUNC such that:

std::string s = new std::string("1234");
int i = s->FUNC;

and i evaluates to 1234. If not, I can easily write my own; I'm just lazy, that's all. :^)

Thanks!
 
Old 10-30-2004, 10:03 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You can use either atoi or the STLs stringstream. I'd suggest using the stringstream since you're already using the STLs string class.

Code:
#include<string>
#include<iostream>
#include<sstream>

void UsingATOI()
{
  std::string strIntString("1234");
  int iConvertToMe;

  iConvertToMe = atoi( strIntString.c_str());

  printf("Int is: %d\n", iConvertToMe );

}

// Using the STL
void STLConvert()
{
  std::string strIntString("1234");
  int iConvertToMe;

  std::stringstream strStream(strIntString);

  strStream >> iConvertToMe;

  std::cout << iConvertToMe << std::endl;

}


main()
{
  UsingATOI();
  STLConvert();
}
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to access elements of std::vector<int>* markhod Programming 3 08-02-2005 10:17 AM
converting a int to string irfanhab Programming 6 07-30-2005 09:40 PM
int to string+sendto pantera Programming 3 08-19-2004 07:19 PM
string to int BadTaste Programming 11 08-18-2004 10:23 AM
string to int in C h/w Programming 2 12-05-2003 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration