|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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))); }
|
|
#2
|
|||||
|
|||||
|
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). Last edited by Jared Russell : 19-02-2014 at 21:18. |
|
#3
|
||||
|
||||
|
Re: Function-in-a-line!!! (C/C++)
Quote:
Code:
template<typename T>
string as_string(T const& t){
stringstream ss;
ss<<t;
return ss.str();
}
|
|
#4
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|