trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
stl_to_string.hpp
1 #pragma once
2 #include <string>
3 #include <iostream>
4 #include <vector>
5 #include <sstream>
6 #include <map>
7 #include <set>
8 namespace util {
9 
10 using std::string;
11 using std::vector;
12 
13 //std::string Str(const vector<double>& x);
14 //std::string Str(const vector<float>& x);
15 //std::string Str(const vector<int>& x);
16 
17 template<class T>
18 std::string Str(const vector<T>& x) {
19  std::stringstream ss;
20  ss << "(";
21  if (x.size() > 0) ss << x[0];
22  for(size_t i = 1; i < x.size(); ++i)
23  ss << ", " << x[i];
24  ss << ")";
25  return ss.str();
26 }
27 
28 
29 template<class T>
30 std::string Str(const std::set<T>& x) {
31  std::stringstream ss;
32  ss << "{";
33  typename std::set<T>::const_iterator it = x.begin();
34  if (x.size() > 0) {
35  ss << *it;
36  ++it;
37  for( ; it != x.end(); ++it)
38  ss << ", " << *it;
39  }
40  ss << "}";
41  return ss.str();
42 }
43 
44 template<class T>
45 std::string Str(const T& x) {
46  std::stringstream ss;
47  ss << x;
48  return ss.str();
49 }
50 #define CSTR(x) util::Str(x).c_str()
51 
52 
53 template<class K, class V>
54 std::string Str(const typename std::map<K,V>& x) {
55  std::stringstream ss;
56  ss << "{";
57  typename std::map<K,V>::const_iterator it = x.begin();
58  if (x.size() > 0) {
59  ss << Str(it->first) << " : " << Str(it->second);
60  ++it;
61  for( ; it != x.end(); ++it)
62  ss << ", " << Str(it->first) << " : " << Str(it->second);
63  }
64  ss << "}";
65  return ss.str();
66 }
67 
68 
69 
70 
71 }
72