[C++] Conversie din std::string la orice tip si invers

Despre limbajul de programare C++ si biblioteca standard STL (forum moderat)
Post Reply
User avatar
Silviu Ardelean
Senior
Senior
Posts: 1175
Joined: 12 Jul 2007, 09:22
Judet: Timiş
Location: Timisoara
Contact:

[C++] Conversie din std::string la orice tip si invers

Post by Silviu Ardelean » 06 Aug 2008, 21:22

De multe ori, in cadrul aplicatiilor reale, trebuie sa convertim string-uri in variabile intregi sau reale, etc, ori invers din variabile reale in stringuri.
Totusi, functiile curente existente in librariile standard nu ne ofera support complet pentru orice fel de conversie.

Spre exemplu, daca am incerca sa punem intr-un string un intreg prin cunoscuta functie itoa() am obtine urmatoarea eroare:

Code: Select all

	int x1 = 230;
	std::string s1;

	itoa(x1, s1, 10);
// error C2664: 'itoa' : cannot convert parameter 2 from 'std::string' to 'char *'
O abordare in stil C pentru a evita aceasta eroare implica utilizarea unui buffer intermediar suficient de mare:

Code: Select all

	int x1 = 230;
	std::string s1;
	char szBuff[64]={0};
	itoa(x1, szBuff, 10);
	s1 = szBuff;
In caz invers, cand am dorii sa facem conversia dintr-un string la int am intamplina din nou probleme:

Code: Select all

	std::string s3 = "442";
	int x3 = atoi(s3);

// error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'
Putem evita eroarea apeland functia c_str() ce intoarce un pointer constant la char.

Code: Select all

int x3 = atoi(s3.c_str());
O modalitate eleganta de a scapa de astfel de probleme e sa construim doua functii de conversie, bazandu-ne pe stream-urile din C++.
Spre exemplu, pentru conversia din orice tip (intreg, real, etc) la std::string, propun utilizarea functiei TypeToString() din exemplul de mai jos, iar pentru conversia din std::string la orice tip utilizam functia StringToType().

Code: Select all

#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>

class CBadConversion : public std::runtime_error {
public:
	CBadConversion(const std::string& s)
		: std::runtime_error(s)
	{ }
};

template <class TypeT>
std::string TypeToString(TypeT x)
{
	std::ostringstream o;
	if (!(o << x))
		throw CBadConversion("TypeToString(TypeT)");

	return o.str();
} 

template <class TypeT>
TypeT	StringToType(std::string s)
{
	std::istringstream i(s);

	TypeT x;
	if (!(i >> x))
		throw CBadConversion("StringToType(TypeT)");

	return x;
}


int _tmain(int argc, _TCHAR* argv[])
{
	int x1 = 230, x2 = 1212;
	double y1 = 0.2312, y2 = 1.0123;  
	std::string s1 , s2;

  try
  {
	s1 = TypeToString<double>(y1);	
	std::cout << "s1: "<< s1 << std::endl;
	s2 = TypeToString<int>(x2);
	std::cout << "s2: "<< s2 << std::endl;

	std::string s3 = "442";
	x2 = StringToType<int>(s3);
	std::cout << "x2: "<< x2 << std::endl;
	y2 = StringToType<double>(s3);
	std::cout << "y2: "<< y2 <<std::endl;
   }
   catch (CBadConversion &eBC)
   {
     std::cout << "An exception has been thrown: " << eBC.what() << std::endl;
   }

   return 0;
}
Aceste functii se pot extinde si pentru wide-string (std::wstring) folosindu-se stream-urile adecvate std::wostringstream si std::wistringstream.

Next FAQ >>



raduv
Junior
Junior
Posts: 2
Joined: 19 Jun 2008, 16:38

Re: [C++] Conversie din std::string la orice tip si invers

Post by raduv » 11 Aug 2008, 17:19

O alta modalitate eleganta de a face conversie din std::string la orice tip si invers o reprezinta folosirea librariei boost, mai specific boost::lexical_cast

Code: Select all

int number = 340;
std::string str_number = "350";

std::string string = boost::lexical_cast<std::string>(number);
int int_str = boost::lexical_cast<int>(str_number);

Next FAQ >>
Last edited by Ovidiu Cucu on 24 Dec 2008, 19:52, edited 2 times in total.
Reason: L-am aranjat putin conform cerintelor de la FAQ.

Post Reply