CASM  1.1.0
A Clusters Approach to Statistical Mechanics
string_io.hh
Go to the documentation of this file.
1 #ifndef CASM_string_io
2 #define CASM_string_io
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 
9 namespace CASM {
10 
11 std::string to_string(const std::string &s, std::string begin = "\"",
12  std::string end = "\"") {
13  return begin + s + end;
14 }
15 
16 template <typename T1, typename T2>
17 std::string to_string(const std::pair<T1, T2> &value) {
18  using namespace std;
19  return to_string(value.first) + ":" + to_string(value.second);
20 }
21 
22 template <typename Iterator>
23 std::string container_to_string(Iterator begin_it, Iterator end_it,
24  std::string begin = "[", std::string end = "]",
25  std::string delim = ", ") {
26  std::stringstream out;
27  using namespace std;
28  out << begin;
29  while (begin_it != end_it) {
30  out << to_string(*begin_it);
31  ++begin_it;
32  if (begin_it != end_it) {
33  out << delim;
34  }
35  }
36  out << end;
37  return out.str();
38 }
39 
40 template <class T>
41 std::string to_string(const std::vector<T> &container, std::string begin = "[",
42  std::string end = "]", std::string delim = ", ") {
43  return container_to_string(container.begin(), container.end(), begin, end,
44  delim);
45 }
46 
47 template <class T, class Compare>
48 std::string to_string(const std::set<T, Compare> &container,
49  std::string begin = "[", std::string end = "]",
50  std::string delim = ", ") {
51  return container_to_string(container.begin(), container.end(), begin, end,
52  delim);
53 }
54 
55 template <class Key, class T, class Compare>
56 std::string to_string(const std::map<T, Key, Compare> &container,
57  std::string begin = "{", std::string end = "}",
58  std::string delim = ", ") {
59  return container_to_string(container.begin(), container.end(), begin, end,
60  delim);
61 }
62 
63 } // namespace CASM
64 
65 #endif
std::set< std::string > & s
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
Main CASM namespace.
Definition: APICommand.hh:8
std::string container_to_string(Iterator begin_it, Iterator end_it, std::string begin="[", std::string end="]", std::string delim=", ")
Definition: string_io.hh:23
Definition: stream_io.hh:24