简体   繁体   English

如何返回在函数中构造的向量指针值

[英]how to return vector pointer value that is constructed in a function

I have a problem to get a return value of vector* value.我在获取 vector* 值的返回值时遇到问题。 I expect the return of value vector* have a components with size 1000 still after the get_vectorD exits.我希望在 get_vectorD 退出后,返回的值 vector* 仍然有一个大小为 1000 的组件。 Let me know what the problem is in the following my code.让我知道下面我的代码有什么问题。

#include <iostream>
#include <vector>
using namespace std;

void get_vectorD(vector<int>* D) 
{
    vector<int>* d_vec = new vector<int>();
    for (int i = 0; i < 1000; i++) { d_vec->push_back(i); }
    D = d_vec; 
}

void main()
{   
    vector<int>* D = new vector<int>();
    get_vectorD(D);
    cout << D->at(0) << endl;
}

The problem is that you are passing the pointer by value.问题是您正在按值传递指针。 Pass it by a reference and it will work通过引用传递它,它将起作用

void get_vectorD(vector<int>*& D)
{
    vector<int>* d_vec = new vector<int>;
    for (int i = 0; i < 1000; i++) { d_vec->push_back(i); }
    D = d_vec; 
}

and you can make use of function return value:您可以使用函数返回值:

vector<int>* get_vectorD()
{
    vector<int>* d_vec = new vector<int>;
    for (int i = 0; i < 1000; i++) { d_vec->push_back(i); }
    return d_vec;
}

or use pointer of pointer或使用指针的指针

#include <iostream>
#include <vector>

using namespace std;

void get_vectorD(vector<int>** D)
{
    vector<int>* d_vec = new vector<int>;
    for (int i = 0; i < 1000; i++) { d_vec->push_back(i); }
    *D = d_vec;
}

int main()
{
    vector<int>* D;
    get_vectorD(&D);
    cout << D->at(0) << endl;
    delete D;
    return 0;
}

Following would work:以下将起作用:

#include <iostream>
#include <vector>
using namespace std;

void get_vectorD(vector<int>*& D) 
{
    D = new vector<int>();
    for (int i = 0; i < 1000; i++) { D->push_back(i); } 
}

int main()
{   
    vector<int>* D;
    get_vectorD(D);
    cout << D->at(0) << endl;
}

Demo演示

Since you are passing the std::vector 's pointer by value, anything you do in the get_vectorD function won't have any effect on vector<int>* D .由于您按值传递std::vector的指针,因此您在get_vectorD函数中执行的任何操作都不会对vector<int>* D产生任何影响。 That's why you need to pass a reference to the pointer.这就是为什么您需要传递对指针的引用。

Also, there is no need to have a temporary d_vec variable in your get_vectorD function.此外,您的get_vectorD函数中不需要临时d_vec变量。

Better way更好的方法

Furthermore, it would have the same effect and would be much less error prone not using the pointer at all like:此外,它会具有相同的效果,并且根本不使用指针会更不容易出错,例如:

#include <iostream>
#include <vector>

void get_vectorD(std::vector<int>& D) {
    for (int i = 0; i < 1000; i++) {
        D.push_back(i);
    } 
}

int main() {
    std::vector<int> D;
    get_vectorD(D);
    std::cout << D.at(0) << std::endl;
}

Much better way to fill up the std::vector填充std::vector更好方法

Don't forget that you can fill up the std::vector by using std::iota like:不要忘记,您可以使用std::iota来填充std::vector ,例如:

std::vector<int> D(1000);
std::iota(D.begin(), D.end(), 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM