Sign in to follow this  
Tradone

Adding methods to STL?

Recommended Posts

Tradone    100

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[p] =  quotient+48;
		temp = remainder;
	}
	aray[p] = 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

Share this post


Link to post
Share on other sites
Moomin    332

#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

Share this post


Link to post
Share on other sites
Scet    960
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;
}


Share this post


Link to post
Share on other sites
Moomin    332
@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.

Share this post


Link to post
Share on other sites
Fruny    1658
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this