示例#1
0
 inline 
 std::basic_ostream<CharT, TraitsT>&
 operator<<(std::basic_ostream<CharT, TraitsT>& os, 
            const boost::posix_time::time_period& p) {
   boost::io::ios_flags_saver iflags(os);
   typedef boost::date_time::time_facet<ptime, CharT> custom_ptime_facet;
   typedef std::time_put<CharT>                  std_time_facet;
   std::ostreambuf_iterator<CharT> oitr(os);
   if (std::has_facet<custom_ptime_facet>(os.getloc())) {
     std::use_facet<custom_ptime_facet>(os.getloc()).put(oitr, os, os.fill(), p);
   }
   else {
     //instantiate a custom facet for dealing with periods since the user
     //has not put one in the stream so far.  This is for efficiency 
     //since we would always need to reconstruct for every time period
     //if the local did not already exist.  Of course this will be overridden
     //if the user imbues as some later point.
     std::ostreambuf_iterator<CharT> oitr(os);
     custom_ptime_facet* f = new custom_ptime_facet();
     std::locale l = std::locale(os.getloc(), f);
     os.imbue(l);
     f->put(oitr, os, os.fill(), p);
   }
   return os;
 }
    //! Formats a month as as string into an output iterator
    static void format_month(const month_type& month,
                             ostream_type& os,
                             const facet_type& f)
    {

      switch (f.month_format())
      {
        case month_as_short_string:
        {
          std::ostreambuf_iterator<charT> oitr(os);
          f.put_month_short(oitr, month.as_enum());
          break;
        }
        case month_as_long_string:
        {
          std::ostreambuf_iterator<charT> oitr(os);
          f.put_month_long(oitr, month.as_enum());
          break;
        }
        case month_as_integer:
        {
          charT fill_char = '0';
          os << std::setw(2) << std::setfill(fill_char) << month.as_number();
          break;
        }

      }
    } // format_month
示例#3
0
std::vector<std::basic_string<charT> >
gather_month_strings(const std::locale& locale, bool short_strings=true)
{
  typedef std::basic_string<charT> string_type;
  typedef std::vector<string_type> collection_type;
  typedef std::basic_ostringstream<charT> ostream_type;
  typedef std::ostreambuf_iterator<charT> ostream_iter_type;
  typedef std::basic_ostringstream<charT> stringstream_type;
  typedef std::time_put<charT>           time_put_facet_type;
  charT short_fmt[3] = { '%', 'b' };
  charT long_fmt[3]  = { '%', 'B' };
  collection_type months;
  string_type outfmt(short_fmt);
  if (!short_strings) {
    outfmt = long_fmt;
  }
  {
    //grab the needed strings by using the locale to
    //output each month
    for (int m=0; m < 12; m++) {
      tm tm_value;
      tm_value.tm_mon = m;
      stringstream_type ss;
      ostream_iter_type oitr(ss);
      std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
                                                      &tm_value,
                                                      &*outfmt.begin(),
                                                      &*outfmt.begin()+outfmt.size());
      months.push_back(ss.str());
    }
  }
  return months;
}
示例#4
0
  inline
  std::basic_ostream<CharT, TraitsT>&
  operator<<(std::basic_ostream<CharT, TraitsT>& os, const local_date_time& ldt)
  {
    boost::io::ios_flags_saver iflags(os);
    typedef local_date_time time_type;//::utc_time_type typename 
    typedef date_time::time_facet<time_type, CharT> custom_time_facet;
    typedef std::time_put<CharT> std_time_facet;
    std::ostreambuf_iterator<CharT> oitr(os);

    if(std::has_facet<custom_time_facet>(os.getloc())) {
      std::use_facet<custom_time_facet>(os.getloc()).put(oitr, 
                                                         os, 
                                                         os.fill(), 
                                                         ldt);
    }
    else {
      custom_time_facet* f = new custom_time_facet();
      std::locale l = std::locale(os.getloc(), f);
      os.imbue(l);
      f->put(oitr, os, os.fill(), ldt);
    }

    return os;
  }
示例#5
0
void CSVIO::write(const std::vector<std::vector<std::string> >& data, const std::vector<std::string>& header, const std::string file){
	ofstream ofile (file, std::ios_base::app);

	try{
		if(!ofile.is_open()){
			ofile.open(file, std::ios_base::app);
		}
		std::ostream_iterator<std::string> oitr (ofile,", ");
		std::copy(header.begin(), header.end(), oitr );
		ofile << "\n";

		std::vector<std::vector<std::string> >::const_iterator rowItr;

		for(rowItr = data.begin(); rowItr != data.end(); ++rowItr){
			std::copy((*rowItr).begin(), (*rowItr).end(), oitr);
			ofile << "\n";
		}
		ofile.close();
	}catch(std::exception e){
		if(ofile.is_open()){
			ofile.close();
		}
		std::cout << e.what() <<  "\n";
	}
}
    //! Formats a month as as string into an output iterator
    static void format_weekday(const weekday_type& wd,
                               ostream_type& os,
                               const facet_type& f,
                               bool  as_long_string)
    {

      std::ostreambuf_iterator<charT> oitr(os);
      if (as_long_string) {
        f.put_weekday_long(oitr, wd.as_enum());
      }
      else {
        f.put_weekday_short(oitr, wd.as_enum());
      }

    } // format_weekday
 // Put ymd to ostream -- part of ostream refactor
 static void ymd_put(ymd_type ymd,
                     ostream_type& os,
                     const facet_type& f)
 {
   std::ostreambuf_iterator<charT> oitr(os);
   charT fill_char = '0';
   switch (f.date_order()) {
     case ymd_order_iso: {
       os << ymd.year;
       if (f.has_date_sep_chars()) {
         f.month_sep_char(oitr);
       }
       month_formatter_type::format_month(ymd.month, os, f);
       if (f.has_date_sep_chars()) {
         f.day_sep_char(oitr);
       }
       os  << std::setw(2) << std::setfill(fill_char)
           << ymd.day;
       break;
     }
     case ymd_order_us: {
       month_formatter_type::format_month(ymd.month, os, f);
       if (f.has_date_sep_chars()) {
       f.day_sep_char(oitr);
       }
       os  << std::setw(2) << std::setfill(fill_char)
         << ymd.day;
       if (f.has_date_sep_chars()) {
         f.month_sep_char(oitr);
       }
       os << ymd.year;
       break;
     }
     case ymd_order_dmy: {
       os  << std::setw(2) << std::setfill(fill_char)
           << ymd.day;
       if (f.has_date_sep_chars()) {
         f.day_sep_char(oitr);
       }
       month_formatter_type::format_month(ymd.month, os, f);
       if (f.has_date_sep_chars()) {
         f.month_sep_char(oitr);
       }
       os << ymd.year;
       break;
     }
   }
 }
std::vector<std::basic_string<charT> >
gather_weekday_strings(const std::locale& locale, bool short_strings=true)
{
  typedef std::basic_string<charT> string_type;
  typedef std::vector<string_type> collection_type;
  typedef std::basic_ostringstream<charT> ostream_type;
  typedef std::ostreambuf_iterator<charT> ostream_iter_type;
  typedef std::basic_ostringstream<charT> stringstream_type;
  typedef std::time_put<charT>           time_put_facet_type;
  charT short_fmt[3] = { '%', 'a' };
  charT long_fmt[3]  = { '%', 'A' };

  collection_type weekdays;


  string_type outfmt(short_fmt);
  if (!short_strings) {
    outfmt = long_fmt;
  }
  {
    //grab the needed strings by using the locale to
    //output each month / weekday
    const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
    tm tm_value;
    memset(&tm_value, 0, sizeof(tm_value));
    for (int i=0; i < 7; i++) {
      tm_value.tm_wday = i;
      stringstream_type ss;
      ostream_iter_type oitr(ss);
      std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
                                                      &tm_value,
                                                      p_outfmt,
                                                      p_outfmt_end);

      weekdays.push_back(ss.str());
    }
  }
  return weekdays;
}
示例#9
0
文件: main.cpp 项目: januswel/cpplib
int main(void) {
    const int upper = 10;
    std::vector<int> ds;
    ds.reserve(upper);
    for (int i = 0; i < upper; ++i) {
        ds.push_back(i);
    }

    std::ostream_iterator<double> oitr(std::cout, "\n");

    std::cout << "source\n";
    std::copy(ds.begin(), ds.end(), oitr);
    std::cout << std::endl;

    const double m = 3.3f;
    std::cout << "mul by " << m << "\n";
    std::transform(
            ds.begin(), ds.end(), oitr,
            std::bind2nd(std::ptr_fun(mul), m));
    std::cout << std::endl;

    return 0;
}