From: Dariusz Murakowski Date: Fri, 5 Apr 2013 03:53:59 +0000 (-0400) Subject: Print mutset_t (to stream using operator<<) as CSV-like string. X-Git-Url: http://src.murakowski.org/?a=commitdiff_plain;h=c7b5374e9079509fc0be3ad0a422902e375c7903;p=VirEvoDyn.git Print mutset_t (to stream using operator<<) as CSV-like string. --- diff --git a/wf.cpp b/wf.cpp index 75caa50..d0bbe04 100644 --- a/wf.cpp +++ b/wf.cpp @@ -4,10 +4,25 @@ #include #include +#include // std::copy +#include // std::ostream_iterator + typedef unsigned int spin_t; typedef std::set mutset_t; // virus is defined by set of mutations typedef std::map 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 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; }