Connecting Tech Pros Worldwide Help | Site Map

Convert from int to std::string

  #1  
Old June 27th, 2008, 05:43 PM
rtrujillor@gmail.com
Guest
 
Posts: n/a
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, 05:43 PM
utab
Guest
 
Posts: n/a

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, 05:43 PM
Jim Langston
Guest
 
Posts: n/a

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, 05:43 PM
Frank Birbacher
Guest
 
Posts: n/a

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, 05:43 PM
Christian Hackl
Guest
 
Posts: n/a

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, 05:43 PM
Frank Birbacher
Guest
 
Posts: n/a

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help with bug in my function - converts int to string Ivar answers 13 November 22nd, 2005 04:14 AM
Conversion from/to String* to/from std::string Wild Wind answers 1 November 17th, 2005 02:36 PM
int to char wak answers 7 July 22nd, 2005 05:08 AM
INT to STR Marcel answers 3 July 19th, 2005 09:00 PM