最近遇到一个问题,就是要对一个vector的变量设置空的参数默认值,刚开始写null,发现不行,后来再网上查了一下,可以通过在外部设置一个变量,来为它赋值为空
#include <iostream> #include <vector> using namespace std; vector<int> vc; class a{ public: a(vector<int> & vv=vc){ v=vv; s="have value"; } void show(){ cout<<s<<endl; cout<<"v.size(): "<<v.size()<<endl; } private: vector<int> v; string s; }; int main(){ vector<int> s={1,2,3,4}; a a(s); a b; a.show(); b.show(); return 0; }
运行结果:
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/608439.html