vector作为返回值的问题

小陆zi 2017-10-08 03:53:51
大家好:

遇到一个问题:
void init(void) {
float *a = new float[10];
floar *b = new float[5];
}
vector<float> func(void)
{
return vector<float>(a,b);
}

vector <float> out = func();

我的问题是这个func返回的是什么?是一个什么类型的返回值最后给了out这个vector?

其实是在看caffe的代码的时候关于std::vector<float> Classifier::Predict(const cv::Mat& img)函数这个函数的返回值,不能理解

望高手解答
谢谢
...全文
791 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 5 楼 edwardlulinux 的回复:
谢谢 两位 热心 帮助; 源码是在 ubuntu机器里,所以没有贴出来; https://github.com/BVLC/caffe/blob/master/examples/cpp_classification/classification.cpp 在classification.cpp文件中:106行:std::vector<float> output = Predict(img); std::vector<float> Predict(img) { const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); return std::vector<float>(begin, end); } 上面的代码是 我自己模仿caffe的代码的写的,不明白表达式的意图。 希望大家帮助我一下。 谢谢
看一下文档里面的构造函数 template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); range constructor Constructs a container with as many elements as the range [first,last), with each element constructed from its corresponding element in that range, in the same order.
小陆zi 2017-10-08
  • 打赏
  • 举报
回复
谢谢 两位 热心 帮助; 源码是在 ubuntu机器里,所以没有贴出来; https://github.com/BVLC/caffe/blob/master/examples/cpp_classification/classification.cpp 在classification.cpp文件中:106行:std::vector<float> output = Predict(img); std::vector<float> Predict(img) { const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); return std::vector<float>(begin, end); } 上面的代码是 我自己模仿caffe的代码的写的,不明白表达式的意图。 希望大家帮助我一下。 谢谢
小陆zi 2017-10-08
  • 打赏
  • 举报
回复
你好 hdt 我的意思是说这个里有两个 指针组成的vector(pointer1,pointer2),这是什么表达?是二维数组还是什么?我自己 也写过一个程序 但是cout的out.size() =0
真相重于对错 2017-10-08
  • 打赏
  • 举报
回复
引用 2 楼 edwardlulinux 的回复:
你好 hdt 我的意思是说这个里有两个 指针组成的vector(pointer1,pointer2),这是什么表达?是二维数组还是什么?我自己 也写过一个程序 但是cout的out.size() =0
错误的问题,很难得到正确的答案 你的代码有很多错误,所以我无法确定你的问题,如果要得到答案,请贴出正确的代码,至少是能通过编译的。
srhouyu 2017-10-08
  • 打赏
  • 举报
回复
引用 2 楼 edwardlulinux 的回复:
你好 hdt 我的意思是说这个里有两个 指针组成的vector(pointer1,pointer2),这是什么表达?是二维数组还是什么?我自己 也写过一个程序 但是cout的out.size() =0
你查一下vector的各种构造函数就行了。 vector<elementType> vecList(n,elem); 创建一个大小为n的向量vecList,该向量中所有的n个元素都初始化为elem func函数里面并没有a和b的定义,init函数里面的a和b也不会被func看到,所以你不能说a和b是指针。 我不知道程序的意图是什么,但是这样的写法就是代表调用vector两个整数参数的构造函数。
真相重于对错 2017-10-08
  • 打赏
  • 举报
回复
返回一个vector对象,虽然返回局部变量,但是vector有拷贝构造函数之类的机制,out根据那个临时变量重新构造。

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧