您的位置:首页 > 其它

关于ostream_iterator<int>(cout," ")的理解

2017-04-30 23:11 459 查看
造函数的参数,语法上相当于有这样的定义:

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  **
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐