C++11中vector的emplace_back用法及输入输出操作符的重载

#include <vector>
#include <iostream>
#include <string>
using namespace std;

struct A{
    int x;
    double y;
    string z;

    A(int _x=0,double _y=0,string _z=""):x(_x),y(_y),z(_z){}
    A(const A &t):x(t.x),y(t.y),z(t.z){}//copy constructor
    friend ostream & operator<<(ostream &out, A &obj){
        out<<"x:"<<obj.x<<", y:"<<obj.y<<", z="<<obj.z<<endl;
        return out;
    }
    friend istream & operator >> (istream &in, A &obj){
        in>>obj.x>>obj.y>>obj.z;
        if(!in)
            obj=A();
        return in;
    }
};

void test()
{
    vector<A> v;
    A a;
    cin>>a;
    v.emplace_back(0,1.0,"2");

    v.emplace_back(a);
    for(auto i=0;i<v.size();++i){
        cout<<v[i];
    }

}

吐槽优快云的标题字符限制,<<和>>不能作为标题内容,不然就提示输入有误。气哭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值