Jump to content
  • Advertisement
Sign in to follow this  
Tradone

Adding methods to STL?

This topic is 4496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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

Share this post


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

Share this post


Link to post
Share on other sites
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
@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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!