关于ostream_iterator<int>(cout," ")的理解
2017-04-30 23:11
567 查看
造函数的参数,语法上相当于有这样的定义:
表达式的结果是一个临时对象
template<class T> struct Foo { Foo(int, double); // 两个,别在意类型 }; /* 你就可以用 Foo<char>(1, 1.5); 调用构造函数特定的构造函数。但这里是cplusplus.com对 ostream_iterator构造函数参数的说明: ostream_iterator (ostream_type& s, const char_type* delimiter); 含义: **s A stream object, which is associated to the iterator. Member type ostream_type is the type of such stream (defined as an alias of basic_ostream<charT,traits>, where charT and trais are the second and third class template parameters). **delimiter C-string with a sequence of character to be inserted after every insertion. Note that the iterator keeps a copy of the pointer passed (not the content of the C-string). */
表达式的结果是一个临时对象
example1:
#include <iostream> #include <iterator> #include <algorithm> int main() { std::ostream_iterator<int> i1(std::cout, ", "); std::fill_n(i1, 5, -1); std::ostream_iterator<double> i2(std::cout); *i2++ = 3.14; } /* 有对象i1,i2,这样好理解,那个直接ostream_iterator<int>(cout," "),中间没加对象的,怎么理解啊? ostream_iterator<int>(cout," ") 就是构造一个迭代器,只不过这个迭代器有些特殊, 1. 单次(single-pass) 2. 输出(output) 3. 可选的分隔符(Optional delimiter) 4. ++为空操作(Incrementing the std::ostream_iterator is a no-op) std::ostream_iterator is a single-pass output iterator that writes successive objects of type T into the std::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. std::ostream_iterator::operator++ Does nothing. These operator overloads are provided to satisfy the requirements of OutputIterator. They make it possible for the expressions *iter++=value and *++iter=value to be used to output (insert) a value into the underlying container. */
example2:
#include <iostream> #include <iterator> #include <vector> #include <deque>//STL之deque双向队列 using namespace std; template<class InputIt, class OutputIt> OutputIt my_copy(InputIt first, InputIt last, OutputIt d_first){ while (first != last) { *d_first++ = *first++; } return d_first; } int main(){ vector<int> v1; vector<int>::const_iterator i1; deque<int> q2;//双向队列q2 front_insert_iterator<deque<int> > i2(q2); ostream_iterator<int> i3(cout," ** ");//输出3次 v1.push_back(0);v1.push_back(1);v1.push_back(2);v1.push_back(3); v1.push_back(4); my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout," $$ "));//输出1次 cout << endl; for (i1 = v1.begin(); i1 != v1.end(); ++i1){ *i2++ = *i1; } for (i1 = v1.begin(); i1 != v1.end(); ++i1){ *i3 = *i1; } cout << endl; for (i1 = v1.begin(); i1 != v1.end(); ++i1){ *i3++ = *i1; } cout << endl; my_copy(q2.begin(), q2.end(), i3); cout << endl; return 0; } /* 输出: 0 $$ 1 $$ 2 $$ 3 $$ 4 $$ 0 ** 1 ** 2 ** 3 ** 4 ** 0 ** 1 ** 2 ** 3 ** 4 ** 4 ** 3 ** 2 ** 1 ** 0 ** */
相关文章推荐
- ostream_iterator<int> output(cout ,"*"); 的理解
- copy(dist.begin(), dist.end(), ostream_iterator<int>(cout, " ")); cout << endl; 这句话是什么意思
- 解释:copy(rs.begin(),rs.end(),ostream_iterator<Rect>(cout,"\n"));
- error C2665: 'ostream_iterator<int,char,struct std::char_traits<char> >::ostream_iterator<int,char,s
- 关于 "Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function" 的解决方案
- spring配置文件中<property name="target">的理解
- 关于<authentication mode="Forms" />
- 关于”System.ServiceModel.Activation.WebServiceHostFactory“与"<webHttp/>"以及RestFul/启用了Ajax的WCF服务
- (转载)vector<int>::iterator mid=(vi.begin()+vi.end())/2
- ostream_iterator(cout," ")的含义
- 关于jsp中<input type="file">获取路径问文件名,获取完全路径问题
- 关于<img>标签的一点理解
- 关于<input type="text">不能显示空格的问题
- 关于出现Don't know how to iterate over supplied "items" in &amp;amp;lt;forEach&amp;amp;gt;异常
- error LNK2001: unresolved external symbol "public: int __thiscall A<int>::get(void)" (?get@?$A@H@@QAEHXZ)
- 关于JSP中单击任意标签弹出文件选择框(<input type="file"/>)的实现方法
- C++中,关于#include<***.h>和#include"***.h"的区别
- 关于spring中<context:component-scan base-package="" />写法
- vector<int>::iterator mid=(vi.begin()+vi.end())/2
- html <"input "/>关于value 空格后的内容消失的问题