相关疑难解决方法(0)

为什么ostream_iterator不能按预期工作?

不用说以下代码:

#include <utility>
#include <vector>
#include <iostream>
#include <iterator>

using namespace std;

typedef pair<char, char> PAIR;

ostream& operator <<(ostream& os, const PAIR& r)
{
    return os << r.first;
}

int main() 
{
    vector<PAIR> coll; 

    cout << coll[0]; // OK. 

    // The following line will cause a compilation error! Why???
    copy(coll.begin(), coll.end(), ostream_iterator<PAIR>(cout)); 
}
Run Code Online (Sandbox Code Playgroud)

c++ stream

10
推荐指数
2
解决办法
1937
查看次数

标签 统计

c++ ×1

stream ×1