Convert from int to std::string

R

rtrujillor

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;
 
U

utab

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?
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
int iAnno = 2005;
int iMes = 7;
int iDia = 23;

std::eek:stringstream os;
os<<iAnno<<"-"<<iMes<<"-"<<iDia;
std::string fecha(os.str());

And I might add <silence>.
 
J

Jim Langston

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 F> std::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);
 
F

Frank Birbacher

Hi!

Jim said:
template<typename T, typename F > T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

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

Which is a simple copy of boost::lexical_cast :)

Frank
 
C

Christian Hackl

Frank said:
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").
 
F

Frank Birbacher

Hi!

Christian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,484
Latest member
JackRichard

Latest Threads

Top