zoukankan      html  css  js  c++  java
  • std::ostream_iterator用法

    Defined in header <iterator>
        template< class T, class CharT = char, class Traits = std::char_traits<CharT>>
              class ostream_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void>

    std::ostream_iterator is a single-pass OutputIterator that writes successive objects of type T into thestd::basic_ostream object for which it was constructed, using operator<<. Optional delimiter string is written to the output stream after every write operation. The write operation is performed when the iterator (whether dereferenced or not) is assigned to. Incrementing the std::ostream_iterator is a no-op.

    In a typical implementation, the only data members of std::ostream_iterator are a pointer to the associatedstd::basic_ostream and a pointer to the first character in the delimiter string.

    When writing characters, std::ostreambuf_iterator is more efficient, since it avoids the overhead of constructing and destructing the sentry object once per character.

     1 #include <iostream>
     2 #include <sstream>
     3 #include <iterator>
     4 #include <numeric>
     5  
     6 int main()
     7 {
     8     std::istringstream str("0.1 0.2 0.3 0.4");
     9     std::partial_sum(std::istream_iterator<double>(str),
    10                       std::istream_iterator<double>(),
    11                       std::ostream_iterator<double>(std::cout, " "));
    12 }

    Output:

    0.1 0.3 0.6 1
     1 // ostream_iterator example
     2 #include <iostream>     // std::cout
     3 #include <iterator>     // std::ostream_iterator
     4 #include <vector>       // std::vector
     5 #include <algorithm>    // std::copy
     6 
     7 int main () {
     8   std::vector<int> myvector;
     9   for (int i=1; i<10; ++i) myvector.push_back(i*10);
    10 
    11   std::ostream_iterator<int> out_it (std::cout,", ");
    12   std::copy ( myvector.begin(), myvector.end(), out_it );
    13   return 0;
    14 }

    Output:
       10, 20, 30, 40, 50, 60, 70, 80, 90,
  • 相关阅读:
    查找第一个不重复的字符问题
    gops
    关于 Go 中 Map 类型和 Slice 类型的传递
    Go 程序的性能优化及 pprof 的使用
    Go语言标准库_输入/输出
    Linux 终端 Bash 常用快捷键介绍及经验
    蓄水池采样算法(Reservoir Sampling)
    Guice 依赖绑定
    基本动态规划之硬币问题
    程序打包成jar 获取不到工程目录下文件的问题
  • 原文地址:https://www.cnblogs.com/scw2901/p/4228909.html
Copyright © 2011-2022 走看看