Không cần phải nói nhiều hơn bất kỳ đoạn mã sau:Tại sao ostream_iterator không hoạt động như mong đợi?
#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));
}
Bạn có thể giải thích cách hoạt động của giải pháp sau này không? –
@ IvanZ.Siu: Xem cập nhật của tôi. – wilx