### Converting Time string to float

I have a vector<string> of times that I want to convert to vector<double>. The time string is in the form 00:00.000. Is there a STL or something like it algorithm or function to do this, otherwise what would be the best way to do this with a function.

-Thank you
How do you expect the time string to be converted to a double? I can't possibly imagine what this means.
To its equivalent in seconds.
Use the std::transform from algorithms:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include #include #include #define STR(x) #x << '=' << x template std::ostream& operator<<(std::ostream& os, const std::vector& v) { os << '{'; for (typename std::vector::const_iterator it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ','; os << *it; } os << '}'; return os; } /** \brief convert a time string to seconds. * * Convert a string in format mm:ss.sss to seconds. * eg 12:34.567 => 754.567 * eg 34.567 => 34.567 * \todo this can be made more bullet-proof */ double string_to_double(const std::string &s) { std::string str = s; const char *cp = strchr(str.c_str(), ':'); if (cp == 0) { double seconds = atof(str.c_str()); return seconds; } else { unsigned minutes = atoi(str.c_str()); double seconds = atof(++cp); seconds += 60.0 * minutes; return seconds; } } int main(int,char** argv) { std::cout << argv[0] << " start" << std::endl; std::vector strings; strings.push_back("00:00.000"); strings.push_back("00:00.001"); strings.push_back("10:00.000"); std::cout << STR(strings) << std::endl; std::vector doubles; doubles.resize(strings.size()); std::transform(strings.begin(), strings.end(), doubles.begin(), string_to_double); std::cout << STR(doubles) << std::endl; std::cout << argv[0] << " finish" << std::endl; }``````

 ```./test_time_string_to_float start strings={00:00.000,00:00.001,10:00.000} doubles={0,0.001,600} ./test_time_string_to_float finish ```

Topic archived. No new replies allowed.