Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何返回指向向量开头的int指针<;int>;_C++_Pointers_Vector - Fatal编程技术网

C++ 如何返回指向向量开头的int指针<;int>;

C++ 如何返回指向向量开头的int指针<;int>;,c++,pointers,vector,C++,Pointers,Vector,我想我应该详细描述一下这个问题 class solution{ public: int* function(int para){ //clear ret ret.clear(); //some lines to manipulate the ret; //........... return ret.data(); } private: std::vector<int> ret; } 类解决方案{ 公众: int*函数(int段){ //透明

我想我应该详细描述一下这个问题

class solution{
public:
  int* function(int para){
   //clear ret
   ret.clear();
   //some lines to manipulate the ret;
   //...........
   return ret.data();
  }
private:
 std::vector<int> ret;
}
类解决方案{
公众:
int*函数(int段){
//透明网
ret.clear();
//一些用于操纵ret的线;
//...........
返回返回返回数据();
}
私人:
std::载体ret;
}

我也不喜欢这样使用指针。但是设计要求指定使用int*作为返回。那么,这种方法能保证函数返回指针可用吗?

这里有一个普遍的问题:

int* function(std::vector<int> vec){

}

但是请注意,这个函数是毫无意义的,因为您可以直接调用向量上的方法(
data()
),而不是将其传递到函数中。

//NOT:int*函数(std::vector vec){
// NOT: int* function(std::vector<int> vec){
int* function(std::vector<int>& vec){ // see & here
    if(vec.empty()){
        return nullptr; // nullptr is more meaningful for pointers
    }
    return vec.data();
    // OR: return &vec.front();
}
// On each update of the vector&, update the pointer also
// as the memory location MAY change.
// And obviously, the vector& must outlive the pointer.
int*函数(std::vector和vec){//请参见此处(&H) if(vec.empty()){ return nullptr;//nullptr对于指针更有意义 } 返回向量数据(); //或:返回&vec.front(); } //每次更新向量时,也要更新指针 //因为内存位置可能会改变。 //显然,向量&必须比指针长。

您需要一个引用&。您不能像您的参数那样返回指向临时局部变量的有效指针。

在这样的函数中
int*函数(std::vector vec)
您通过值传递参数,因此在调用
函数(my_vector)时
编译器将通过调用复制构造函数创建
my_vector
的副本,该副本仅在程序控制流进入函数和控制流退出函数之间有效

因此,在该上下文中,您不能返回指向向量开头的指针,因为当您退出该函数时,它将不再存在。这是“返回临时”调用,这非常糟糕,因为您的程序在访问该指针时可能会崩溃

要执行所需操作,可以通过引用传递向量:
int*函数(std::vector&vec)
,然后调用
vec.data()

int*函数(std::vector&vec)
{
返回向量数据();
}

但是,如果可以,请使用
std::vector
,不要弄乱指针。

@Borgleader未定义的返回值在这种情况下,如果您返回指向
vec
中第一个元素的指针,您将得到未定义的行为,因为
vec
是按值传递的,并将在函数末尾被销毁。@diterLücking是的,但不管他现在如何设置它(解释为什么需要完整的答案)。我只是指出他可以调用vector的数据成员函数,而不是编写自己的函数。当你使用指向数据的指针时,你应该小心,因为当向量被操纵时(从中推送或删除元素)然后存储数据的地址可能会更改。如果它是我定义的类,我可以将输入向量保存到我自己的私有向量中,然后使用return vec.data()吗?@user2840454向量变量必须在使用指针时一直存在。如果您调整了它的大小,请在指针移动时更新它。--这是规则,请在您的设计中使用它。OP:将最后一句话作为好的建议。如果您问这个问题,您还没有准备好使用原始指针。
return vec.data();
// NOT: int* function(std::vector<int> vec){
int* function(std::vector<int>& vec){ // see & here
    if(vec.empty()){
        return nullptr; // nullptr is more meaningful for pointers
    }
    return vec.data();
    // OR: return &vec.front();
}
// On each update of the vector&, update the pointer also
// as the memory location MAY change.
// And obviously, the vector& must outlive the pointer.
int* function(std::vector<int>& vec)
{
   return vec.data();
}