string to int

Comment from Sys_Prog 
Date: 03/08/2004 08:59PM PST 
Comment   


I doubt if there is some STL function for that 

However u can simple use the ascii conversion 

Example 
Suppose I have a numeric digit in char and I want to convert it to an int, then just subtract '0' from the char and assign the result of subtraction to the int 

example 

char c = '2' ; 
int  no ; 

no = c - '0' ; 
cout << no ;
 

Similarly, You will have to do it for multiple digits using a loop and manipulating 


Since u have a std::string type of object, you can use the c_str() function of std::string to get a char array format of your string


Link for std::string reference 
http://www.cppreference.com/cppstring.html 



HTH 

Amit 






Comment from Sys_Prog 
Date: 03/08/2004 09:08PM PST 
Comment   


The foll would be the manipulation of the complete char array 

int main() 
{                         
    char str[] = "123" ; 

      int no = 0 ; 
      int i = 0 ; 
    
      while ( str [ i ] != '{row.content}' ) { 
            no = no * 10 ; 
                no = no + ( str [ i ] - '0' ) ; 
            i++ ;           
      } 
       cout << no ; 
    system ( "PAUSE" ) ; 
    return 0; 
}


Just add the code for converting the string object to a char array (as I posted in the link above) 

HTH 

Amit 

Assisted Answer from roshmon 
Date: 03/08/2004 09:38PM PST 
Grade: A 
Assisted Answer   


What about this 

string a="321" 
int b = atoi(a.c_str());


Rosh   

Accepted Answer from Bird__ 
Date: 03/08/2004 10:08PM PST 
Grade: A 
Accepted Answer   


as you want to use the stl, forget about atoi because it's not part of the stl 
instead, use stringstreams 

i.e. 

#include <iostream> 
#include <sstream> 

int main( ) 

    std::string a("321"); 
    int b; 
    std::stringstream ss(a); 
    ss >> b; 
    std::cout << b; 
    return 0; 


you could have also used 

std::stringstream ss; 
ss << a; 

to input the string into the stringstream, and this can be used with any built in type (i.e. for converting ints to strings, etc) as well as objects (if set up correctly). 

Comment from Bronek-K 
Date: 03/13/2004 07:48AM PST 
Comment   


Hi Carl3003, try this: 

#include <sstream> 
#include <iostream> 
#include <cstdio> 

template <typename Source> 
std::string make_string(Source s) 

 std:: ostringstream buf; 
 buf << s; 
 return buf.str(); 


int main() 

 int t1 = 123; 
 std::string s1 = make_string(t1); 
 std::cout << s1 << std::endl; 
 printf("%s\n", s1.c_str()); 

 long t2 = 456; 
 std::string s2 = make_string(t2); 
 std::cout << s2 << std::endl; 
 printf("%s\n", s2.c_str()); 

 double t3 = 789.01; 
 std::string s3 = make_string(t3); 
 std::cout << s3 << std::endl; 
 printf("%s\n", s3.c_str()); 




Comment from cool_alok 
Date: 03/17/2004 01:58AM PST 
Comment   


hellooo mr carl 
 there is very simple function defined in stdlib.h 
named 
itoa: which convert integer to string 
ultoa: which convert long to string
 
and viceversa 
atoi for converting string to integer 
also 
_fcvt for float 
thanks 
ALok 

Comment from jcac2000 
Date: 05/19/2004 11:48AM PDT 
Comment   


c big number calculator  



from : http://ssmir.ycool.com/post.297034.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值