HOG2
StringUtils.cpp
Go to the documentation of this file.
1 #include "StringUtils.h"
2 #include <cmath>
3 
4 std::string int_to_string(int i) {
5  std::stringstream s;
6  s << i;
7  return s.str();
8 }
9 
10 std::string double_to_string(double i) {
11  std::stringstream s;
12  s << i;
13  return s.str();
14 }
15 
16 std::vector<std::string> split(const std::string &s, char delim) {
17  std::vector<std::string> elems;
18  std::stringstream ss(s);
19  std::string item;
20  while(std::getline(ss, item, delim)) {
21  elems.push_back(item);
22  }
23  return elems;
24 }
25 
26 std::string to_string_separator(int i)
27 {
28  if (i == 0)
29  return std::string("0");
30  if (i < 0)
31  return std::string("-")+to_string_separator(-i);
32  std::string tmp;
33  int numdigits = floor(log10(i))+1;
34  int divisor = pow(10, numdigits-1);
35  int currdigit = 0;
36  while (currdigit != numdigits)
37  {
38  tmp += ('0' + (i/divisor)%10);
39  currdigit++;
40  divisor/= 10;
41  if ((0 == (numdigits-currdigit)%3) && (numdigits != currdigit))
42  tmp += ',';
43  }
44  return tmp;
45 }
to_string_separator
std::string to_string_separator(int i)
Definition: StringUtils.cpp:26
StringUtils.h
double_to_string
std::string double_to_string(double i)
Converts a double to a string.
Definition: StringUtils.cpp:10
split
std::vector< std::string > split(const std::string &s, char delim)
Splits a string into elements.
Definition: StringUtils.cpp:16
int_to_string
std::string int_to_string(int i)
Converts an integer to a string.
Definition: StringUtils.cpp:4