470,815 Members | 1,104 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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;
Jun 27 '08 #1
5 3809
On Mon, 19 May 2008 01:40:59 -0700, rtrujillor wrote:
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
Jun 27 '08 #2
rt********@gmail.com wrote:
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
ta*******@rocketmail.com
Jun 27 '08 #3
Hi!

Jim Langston schrieb:
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
Jun 27 '08 #4
Frank Birbacher wrote:
Jim Langston schrieb:
> 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
Jun 27 '08 #5
Hi!

Christian Hackl schrieb:
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
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Karthik | last post: by
16 posts views Thread by Khuong Dinh Pham | last post: by
10 posts views Thread by sposes | last post: by
2 posts views Thread by Abhishek | last post: by
7 posts views Thread by varsha.gadekar | last post: by
11 posts views Thread by Sudzzz | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.