当前位置: 代码迷 >> 综合 >> istream/ostream
  详细解决方案

istream/ostream

热度:3   发布时间:2023-12-22 06:06:04.0

#include<iostream>
#include<algorithm>
#include<iterator>
#include<vector>
using namespace std;
class Node  /*自定义的类,只有两个整型成员*/
{
public:
 int x,y;

 Node():x(0),y(0) {}
 Node(int xx,int yy):x(xx),y(yy) {}
 /*输入输出*/
 friend std::ostream& operator<<(std::ostream&,Node&);
 friend std::istream& operator>>(std::istream&,Node&);
};
std::ostream& operator<<(std::ostream& out,Node& n)
{
 out<<n.x<<','<<n.y;
 return out;
}
std::istream& operator>>(std::istream& in,Node& n)
{
    in>>n.x>>n.y;
 return in;
}

int main()
{
 std::vector<Node> vn;
 /*用istream_iterator输入一批Node 有问题*/
 std::copy(std::istream_iterator<Node>(std::cin),std::istream_iterator<Node>(),std::back_inserter(vn));
 /*用ostream_iterator输出一批Node*/
 std::copy(vn.begin(),vn.end(),std::ostream_iterator<Node>(std::cout));

 return 0;
}

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main() {
    istream_iterator<int> in_it(cin), eof;
    vector<int> vec(in_it, eof);
    sort(vec.begin(), vec.end());
    ostream_iterator<int> out_it(cout, " ");
    unique_copy(vec.begin(), vec.end(), out_it);
    
    return 0;
}