Connecting Tech Pros Worldwide Help | Site Map

Convert from int to std::string

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 27th, 2008, 04:43 PM
rtrujillor@gmail.com
Guest
 
Posts: n/a
Default Convert from int to std::string

int iAnno = 2005;
int iMes = 7;
int iDia = 23;

std::stringstream ssAnno;
ssAnno << iAnno;
std::string anno = ssAnno.str();

std::stringstream ssMes;
ssMes << iMes;
std::string mes = ssMes.str();

std::stringstream ssDia;
ssDia << iDia;
std::string dia = ssDia.str();

std::string fecha= anno + mes + dia;

  #2  
Old June 27th, 2008, 04:43 PM
utab
Guest
 
Posts: n/a
Default Re: Convert from int to std::string

On Mon, 19 May 2008 01:40:59 -0700, rtrujillor wrote:
Quote:
int iAnno = 2005;
int iMes = 7;
int iDia = 23;
>
std::stringstream ssAnno;
ssAnno << iAnno;
std::string anno = ssAnno.str();
>
std::stringstream ssMes;
ssMes << iMes;
std::string mes = ssMes.str();
>
std::stringstream ssDia;
ssDia << iDia;
std::string dia = ssDia.str();
>
std::string fecha= anno + mes + dia;
So what is the question from your side?

--
Umut
  #3  
Old June 27th, 2008, 04:43 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: Convert from int to std::string

rtrujillor@gmail.com wrote:
Quote:
int iAnno = 2005;
int iMes = 7;
int iDia = 23;
>
std::stringstream ssAnno;
ssAnno << iAnno;
std::string anno = ssAnno.str();
>
std::stringstream ssMes;
ssMes << iMes;
std::string mes = ssMes.str();
>
std::stringstream ssDia;
ssDia << iDia;
std::string dia = ssDia.str();
>
std::string fecha= anno + mes + dia;
template<typename T, typename F T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}

template<typename Fstd::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}

int iAnno = 2005;
int iMes = 7;
int iDia = 23;

std::string fecha = StrmConvert(iAnno) + StrmConvert(iMes) +
StrmConvert(iDia);


--
Jim Langston
tazmaster@rocketmail.com


  #4  
Old June 27th, 2008, 04:43 PM
Frank Birbacher
Guest
 
Posts: n/a
Default Re: Convert from int to std::string

Hi!

Jim Langston schrieb:
Quote:
template<typename T, typename F T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}
>
template<typename Fstd::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}
Which is a simple copy of boost::lexical_cast :)

Frank
  #5  
Old June 27th, 2008, 04:43 PM
Christian Hackl
Guest
 
Posts: n/a
Default Re: Convert from int to std::string

Frank Birbacher wrote:
Quote:
Jim Langston schrieb:
Quote:
> template<typename T, typename F T StrmConvert( const F from )
> {
> std::stringstream temp;
> temp << from;
> T to = T();
> temp >to;
> return to;
> }
>>
> template<typename Fstd::string StrmConvert( const F from )
> {
> return StrmConvert<std::string>( from );
> }
>
Which is a simple copy of boost::lexical_cast :)
boost::lexical_cast also does error checking so that it can throw
boost::bad_lexical_cast. IIRC it checks failbit and whether the stream
has arrived at EOF or not (to catch cases such as "12a").


--
Christian Hackl
  #6  
Old June 27th, 2008, 04:43 PM
Frank Birbacher
Guest
 
Posts: n/a
Default Re: Convert from int to std::string

Hi!

Christian Hackl schrieb:
Quote:
boost::lexical_cast also does error checking so that it can throw
boost::bad_lexical_cast. IIRC it checks failbit and whether the stream
has arrived at EOF or not (to catch cases such as "12a").
Yes, correct. It is much more sophisticated and also supports conversion
from A to B where neither is a string.

Regards, Frank
 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.