Print mutset_t (to stream using operator<<) as CSV-like string.
authorDariusz Murakowski <murakdar@mit.edu>
Fri, 5 Apr 2013 03:53:59 +0000 (23:53 -0400)
committerDariusz Murakowski <murakdar@mit.edu>
Fri, 5 Apr 2013 04:03:56 +0000 (00:03 -0400)
wf.cpp

diff --git a/wf.cpp b/wf.cpp
index 75caa50..d0bbe04 100644 (file)
--- a/wf.cpp
+++ b/wf.cpp
@@ -4,10 +4,25 @@
 #include <set>
 #include <map>
 
+#include <algorithm>    // std::copy
+#include <iterator>     // std::ostream_iterator
+
 typedef unsigned int spin_t;
 typedef std::set<spin_t> mutset_t;  // virus is defined by set of mutations
 typedef std::map<mutset_t,int> pop_t;   // number of each virus species
 
+/*
+ * print a set of mutations to stream as a CSV-like string
+ */
+std::ostream& operator<<(std::ostream& os, mutset_t v)
+{
+    mutset_t::iterator i = v.begin(), end = v.end();
+    os << *i;  // print first element
+    for (++i; i != end; ++i)  // then with comma as prefix
+        os << ',' << *i;
+    return os;
+}
+
 
 int main(int argc, char **argv)
 {
@@ -26,7 +41,35 @@ int main(int argc, char **argv)
     Pop[s] = 100;
     Pop[s] += 50;
 
-    std::cout << *Pop.begin()->first.begin() << ' ' << Pop.begin()->second << std::endl;
+    for(pop_t::iterator vi = Pop.begin(); vi != Pop.end(); ++vi) {
+        mutset_t v = vi->first;
+
+        /*
+        // appends "," to each element
+        std::ostream_iterator<int> output(std::cout, ",");
+        std::copy(v.begin(),v.end(), output);
+        */
+        /*
+        // appends "," to each element
+        for(mutset_t::iterator i = v.begin(); i != v.end(); ++i) {
+            std::cout << (*i) << ",";
+        }
+        */
+        /*
+        // prefix every element (except the first) with ","
+        mutset_t::iterator i = v.begin(), end = v.end();
+        std::cout << *i;
+        for (++i; i != end; ++i)
+            std::cout << ',' << *i;
+        std::cout << ' ';
+        */
+        ///*
+        // proper printing function
+        std::cout << v << ' ';
+        //*/
+
+        std::cout << vi->second << std::endl;
+    }
 
     return 0;
 }