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,
  • 相关阅读:
    php wamp 配置虚拟主机
    php xml 操作。
    第一个输入框中 输入 test,第二个输入框中自动出现 test.baibu.com
    正则表达式 手机号码的验证
    JQery 设置 退格键 不可用 jQery 中的keydown 事件
    YII 片段缓存如何实现。
    php set_time_limit() 函数
    VLAN
    进程
    802.11无线网络权威指南
  • 原文地址:https://www.cnblogs.com/scw2901/p/4228909.html
Copyright © 2011-2022 走看看