Commit 24b80d560fec21a5c56357c193abbcf207e3a18c

Authored by Jordi Inglada
1 parent 30be6ac3
Exists in master

Add << operator for printing vectors of printable things

Showing 1 changed file with 20 additions and 12 deletions   Show diff stats
include/cbutils.h
... ... @@ -68,19 +68,19 @@ bool contains(std::string s, std::vector&lt;std::string&gt; patterns)
68 68 }
69 69 } //end ns string
70 70 // ----------------- System calls ----------------------------------------------
71   -namespace system{
  71 + namespace system{
72 72 // Runs the command sending it to the OS and returns a string wit its output
73   -std::string call(const std::string& command)
74   -{
75   - std::string tmp_filename{std::tmpnam(nullptr)};
76   - std::stringstream command_redirect{command+" > "+tmp_filename};
77   - std::system(command_redirect.str().c_str());
78   - std::stringstream command_output;
79   - command_output << std::ifstream(tmp_filename).rdbuf();
80   - std::remove(tmp_filename.c_str());
81   - return command_output.str();
82   -}
83   -} //end ns system
  73 + std::string call(const std::string& command)
  74 + {
  75 + std::string tmp_filename{std::tmpnam(nullptr)};
  76 + std::stringstream command_redirect{command+" > "+tmp_filename};
  77 + std::system(command_redirect.str().c_str());
  78 + std::stringstream command_output;
  79 + command_output << std::ifstream(tmp_filename).rdbuf();
  80 + std::remove(tmp_filename.c_str());
  81 + return command_output.str();
  82 + }
  83 + } //end ns system
84 84 // ----------------- File IO ---------------------------------------------------
85 85 namespace file
86 86 {
... ... @@ -208,5 +208,13 @@ std::string now_str()
208 208 return result.str();
209 209 }
210 210 }// end ns misc
  211 +namespace operators{
  212 +// Print the contents of a vector of printable things
  213 +template<typename T>
  214 +std::ostream& operator <<(std::ostream& stream, const std::vector<T> vec){
  215 + for(const auto& t : vec) stream << t << " ";
  216 + return stream;
  217 +}
  218 +} // end ns operators
211 219 }//end ns cbutils
212 220 #endif
... ...