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博客 ” ,链接:http://blog.itpub.net/30221425/viewspace-2142671/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30221425/viewspace-2142671/

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐