Created
July 24, 2013 16:01
-
-
Save jackeylu/6071945 to your computer and use it in GitHub Desktop.
a stringstream example for converting any built-in type or self-defined with _str method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sstream> // for stringstream | |
#include <iostream> | |
template <class T> | |
T convert(const char* arg) | |
{ | |
T result; | |
std::stringstream ss; | |
ss << arg; | |
ss >> result; | |
return result; | |
} | |
int test_convert(int argc, const char* argv[]) | |
{ | |
int p1 = convert<int>(argv[1]); | |
double p2 = convert<double>(argv[2]); | |
std::cout << "parameter 1 = " << p1 << std::endl; | |
std::cout << "parameter 2 = " << p2 << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment