ostream_iterator(cout," ")这句该怎么理解啊

yewennuan 2015-01-25 06:54:09
ostream_iterator<int>是个类型,后面直接加(cout," ")到底什么意思啊
...全文
554 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yewennuan 2015-01-27
  • 打赏
  • 举报
回复
六楼,你有对象i1,i2,这样我理解的,我那个直接ostream_iterator<int>(cout," ") 中间没加对象的,怎么理解啊
mymtom 2015-01-27
  • 打赏
  • 举报
回复
引用 7 楼 yewennuan 的回复:
六楼,你有对象i1,i2,这样我理解的,我那个直接ostream_iterator<int>(cout," ") 中间没加对象的,怎么理解啊
看看下面的代码,也许能帮助你理解

#include <iostream>
#include <iterator>
#include <vector>
#include <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(int argc, char *argv[])
{
    vector<int> v1;
    vector<int>::const_iterator i1;
    deque<int> q2;
    front_insert_iterator<deque<int> > i2(q2);
    ostream_iterator<int> i3(cout," ");

    v1.push_back(0);
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);
    v1.push_back(5);
    v1.push_back(6);
    v1.push_back(7);
    v1.push_back(8);
    v1.push_back(9);

    my_copy(v1.begin(), v1.end(), ostream_iterator<int>(cout," "));
    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;
}

/* output
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 0 
*/
mymtom 2015-01-27
  • 打赏
  • 举报
回复
引用 7 楼 yewennuan 的回复:
六楼,你有对象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.
yewennuan 2015-01-26
  • 打赏
  • 举报
回复
比如前面声明了vector<int>s(5)..........然后transform(s.begin(),s.end(),ostream_iterator<int>(cout," "),negate<int>()),书上说这里的ostream_iterator<int>(cout," ")和s.begin(),s.end()都是迭代器,是ostream_iterator<int>(cout," ")生成的临时对象就是迭代器对吗
mymtom 2015-01-26
  • 打赏
  • 举报
回复
模板啊,构造函数啊

#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;
}

http://en.cppreference.com/w/cpp/iterator/ostream_iterator/ostream_iterator
iyomumx 2015-01-25
  • 打赏
  • 举报
回复
表达式的结果是一个临时对象
yewennuan 2015-01-25
  • 打赏
  • 举报
回复
构造函数没有对象可以直接用吗
码工许师傅 2015-01-25
  • 打赏
  • 举报
回复
实际的用处就是,cout连续几次输出的分隔符; 还依稀记得侯捷老师某本书上的一个例子:
copy(istream_iterator<char>(cin), istream_iterator<char>(), ostream_iterator<char>(cout, ""));   
完全是为了使用迭代器而使用的
码工许师傅 2015-01-25
  • 打赏
  • 举报
回复
构造函数的参数,语法上相当于有这样的定义:
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).

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧