Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Function-in-a-line!!! (C/C++) (http://www.chiefdelphi.com/forums/showthread.php?t=126898)

yash101 19-02-2014 21:07

Function-in-a-line!!! (C/C++)
 
Since I have started vision processing, these are nifty functions, that I've consicified into a single line so they are out of the way of your real code!

Code:

//string funcs, requiring string and sstream
string bool2str(bool in) { stringstream str; str << in; return str.str(); }

string int2str(int inp) { stringstream str; str << inp; return str.str(); }

string double2str(double in) { stringstream str; str << in; return str.str(); }


//math functions!
double tanDeg(double input) { return tan((input) * pi / 180); }
double hypotenuse(double legA, double legB) { return sqrt(((legA*legA) + (legB*legB))); }
double leg(double hypotenuse, double leg) { return sqrt(((hypotenuse*hypotenuse) - (leg*leg))); }

Give me any feedback and post more of these single-liner c functions!

Jared Russell 19-02-2014 21:16

Re: Function-in-a-line!!! (C/C++)
 
There is a <cmath> function for computing the length of the hypotenuse.

http://www.cplusplus.com/reference/cmath/hypot/

For doing numeric type to string conversions, C++11 supports std::to_string, but without C++11 you could still do your stringstream method with a templated function (since the function body is exactly the same regardless of the argument type).

SoftwareBug2.0 19-02-2014 22:31

Re: Function-in-a-line!!! (C/C++)
 
Quote:

Originally Posted by yash101 (Post 1346557)
Since I have started vision processing, these are nifty functions, that I've consicified into a single line so they are out of the way of your real code!

Code:

//string funcs, requiring string and sstream
string bool2str(bool in) { stringstream str; str << in; return str.str(); }

string int2str(int inp) { stringstream str; str << inp; return str.str(); }

string double2str(double in) { stringstream str; str << in; return str.str(); }


//math functions!
double tanDeg(double input) { return tan((input) * pi / 180); }
double hypotenuse(double legA, double legB) { return sqrt(((legA*legA) + (legB*legB))); }
double leg(double hypotenuse, double leg) { return sqrt(((hypotenuse*hypotenuse) - (leg*leg))); }

Give me any feedback and post more of these single-liner c functions!

Use a template:
Code:

template<typename T>
string as_string(T const& t){
        stringstream ss;
        ss<<t;
        return ss.str();
}


AlexBrinister 23-02-2014 19:45

Re: Function-in-a-line!!! (C/C++)
 
Templates are the best way to go there. C++11 reportedly has a to_string function in namespace std but it is absent from GCC 4.8.2. Kind of irrelevant seeing as WindRiver uses GCC 3.4.4...

Alex Brinister


All times are GMT -5. The time now is 02:31.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi