Adding methods to STL?

Started by
5 comments, last by RDragon1 18 years ago


double ToInt(std::string);
char* ToString(int);

char* ToString(int intdata) {
	int p;
	int q;
	int temp = intdata, quotient, remainder;
	int count = 0;
	do {
		quotient = temp/10;
		temp = quotient;
		count++;
	}
	while(quotient != 0);

	count++;
	temp = intdata;
	char *aray = new char[count];
	for(p=0, q=count-2; p<count-1; p++, q--) {
		quotient = temp / (int)pow( (float)10, q);
		remainder = temp % (int)pow( (float)10, q);
		aray =  quotient+48;
		temp = remainder;
	}
	aray = 0;//giving null character to last element of aray.

	return aray;
}

double ToInt(std::string is) {
	double ans=0;
	int b;
	int a=is.size();	// get the size
	int i=0;

	while(a>0) {
		b=is[a-1]-'0'; // convert to integer
		ans = ans + b*pow( (float)10,i); 	
		a--;
		i++;
	}

	return ans;
}



okay.. I want to do something like this..

int foo_int=10;
string foo_str="";
foo_str=10.ToString();
std::cout << foo_str << std::endl;

int bar_int;
bar_int=foo_str.ToInt();
std::cout << bar_int << std::endl;

results: 10 10
Advertisement
#include <sstream>int ToInt(std::string);std::string ToString(int);std::string ToString(int intdata){    std::stringstream Stream;    std::string ReturnValue;    Stream << intdata;    Stream >> ReturnValue;    return ReturnValue;}int ToInt(std::string is){    std::stringstream Stream;    int ReturnValue;    Stream << is;    Stream >> ReturnValue;    return ReturnValue;}

then ...
int foo_int=10;string foo_str="";foo_str=ToString(foo_int);std::cout << foo_str << std::endl;int bar_int;bar_int=ToInt(foo_str);std::cout << bar_int << std::endl;

results:
10
10
Why not just use string streams?

	std::stringstream Stream;			int foo_int=10;	Stream << foo_int;	std::cout << Stream.str() << std::endl;	int bar_int;	Stream >> bar_int;	std::cout << bar_int << std::endl;


Edit: Beaten, but I still prefer mine.

Edit 2: If you still want a seperate function for conversion you can make your own LexicalCast fucntion like so:

template< typename Output, typename Input >Output LexicalCast( const Input &Value ) {	Output ReturnValue;	std::stringstream Stream;	Stream << Value;		Stream >> ReturnValue;	return ReturnValue;}int main(int argc, char *argv[]){		int foo_int = 10;	std::string foo_str = LexicalCast<std::string,int>(foo_int);	std::cout << foo_str << std::endl;	int bar_int = LexicalCast<int,std::string>(foo_str);	std::cout << bar_int << std::endl;		system("PAUSE");	return 0;}
Well they both beat me to it ;)
Stringstream :D
@Scet - Yeah your way is nicer, the only reason I used the functions was because he seemed to want to use them in his original post.
C++ fundamental types, such as int are not objects, cannot be inherited from, and do not have and method you can call.

Allowing something like foo_str=10.ToString(); would require major changes to the language itself, which are very unlikely to happen anytime soon.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
boost::lexical_cast<std::string>( some_int );

This topic is closed to new replies.

Advertisement