return a std::vector 在 c++ 中
return a std::vector in c++
编辑
*请为这个话题点赞,因为我不能再在这个论坛上提问了。
编程是我的生命,我只是因为自动禁止而陷入困境。谢谢(不然我需要版主帮忙解决这个问题 *
我是 C++ 的初级程序员,我基本上想 return std::vector
当我调试我的代码时,我得到 function call missing argument list 。这是我的简单代码
感谢您的帮助
#include "stdafx.h"
#include <vector>
#include <iostream>
static std::vector<int> returnStaticVector();
static std::vector<int> returnStaticVector(){
std::vector<int> vectorInt = std::vector<int>();
vectorInt.push_back(0);
return vectorInt;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> a = std::vector<int>();
a = returnStaticVector(); // Compile , but error when I try to access to the size of the std::vector
//int size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
//int size = &a.size; // & Illegal operation
//int& size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
int* size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
return 0;
}
在std::vector
中size是成员函数,不是成员变量。你这样使用它:
int size = a.size();
如果没有括号,则为语法错误。
顺便说一句,您可以做的另一件事来简化您的代码是像这样声明向量:
std::vector<int> a;
或者在 C++11 中
std::vector<int> a{};
这两个都将 default-construct 向量——这适用于任何 class 类型。
这样做,
std::vector<int> a = std::vector<int>();
没那么好,因为它更长,会让你输入两次,而且它复制初始化它而不是默认构造它,这略有不同并且可能效率较低。
第一件事——你的代码中真正的编译问题是因为你使用了 a.size 而不是 a.size( )。尝试更改它,代码应该可以成功编译。
除此之外,我认为像您所做的那样 return 向量不是一个好主意。尝试使用来自调用函数的引用传递向量。那是更好的设计。
如果您仍在按值考虑 return,请考虑复制省略,这是编译器实施的一种优化,可在许多情况下防止不必要的复制。它使 return 按值或 pass-by-value 在许多情况下可行。
在此处阅读有关复制省略的更多信息:-
http://en.cppreference.com/w/cpp/language/copy_elision
What are copy elision and return value optimization?
编辑
*请为这个话题点赞,因为我不能再在这个论坛上提问了。 编程是我的生命,我只是因为自动禁止而陷入困境。谢谢(不然我需要版主帮忙解决这个问题 *
我是 C++ 的初级程序员,我基本上想 return std::vector
当我调试我的代码时,我得到 function call missing argument list 。这是我的简单代码
感谢您的帮助
#include "stdafx.h"
#include <vector>
#include <iostream>
static std::vector<int> returnStaticVector();
static std::vector<int> returnStaticVector(){
std::vector<int> vectorInt = std::vector<int>();
vectorInt.push_back(0);
return vectorInt;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> a = std::vector<int>();
a = returnStaticVector(); // Compile , but error when I try to access to the size of the std::vector
//int size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
//int size = &a.size; // & Illegal operation
//int& size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
int* size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member
return 0;
}
在std::vector
中size是成员函数,不是成员变量。你这样使用它:
int size = a.size();
如果没有括号,则为语法错误。
顺便说一句,您可以做的另一件事来简化您的代码是像这样声明向量:
std::vector<int> a;
或者在 C++11 中
std::vector<int> a{};
这两个都将 default-construct 向量——这适用于任何 class 类型。
这样做,
std::vector<int> a = std::vector<int>();
没那么好,因为它更长,会让你输入两次,而且它复制初始化它而不是默认构造它,这略有不同并且可能效率较低。
第一件事——你的代码中真正的编译问题是因为你使用了 a.size 而不是 a.size( )。尝试更改它,代码应该可以成功编译。
除此之外,我认为像您所做的那样 return 向量不是一个好主意。尝试使用来自调用函数的引用传递向量。那是更好的设计。
如果您仍在按值考虑 return,请考虑复制省略,这是编译器实施的一种优化,可在许多情况下防止不必要的复制。它使 return 按值或 pass-by-value 在许多情况下可行。
在此处阅读有关复制省略的更多信息:-
http://en.cppreference.com/w/cpp/language/copy_elision
What are copy elision and return value optimization?