ITPub博客

首页 > 应用开发 > IT综合 > 【C++】C++函数可以返回列表或者容器

【C++】C++函数可以返回列表或者容器

原创 IT综合 作者:风尘_NULL 时间:2017-07-27 20:31:43 0 删除 编辑
C++是不能返回数组的(可以返回数组指针或者引用),但是C++11新标准可以返回容器:
std::vector <int> product_list(const int a){
 if(a>0){
 //列表
 return {1,3,5};
 }else if(a==0){
 //int容器
 std::vector <int> ivec={2,4,7};
 return ivec;
 }else{
 return {3,6,9};
 }

} int main()
{
 std::vector <int> ivec=product_list(0);
 for(auto c:ivec){
 std::cout<<c<<std::endl;
 }
}
运行结果如下:


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30221425/viewspace-2142671/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
腾讯科技(深圳)有限公司 DBA

注册时间:2015-04-24

  • 博文量
    76
  • 访问量
    238220