从函数C ++返回向量

时间:2021-02-18 02:02:44

I am trying to return a vector with the values in the reverse order (e.g, I enter a vector with 0,1,2,3,4 and the function return a vector with 4,3,2,1,0).

我试图以相反的顺序返回一个带有值的向量(例如,我输入一个带有0,1,2,3,4的向量,该函数返回一个带有4,3,2,1,0的向量)。

The compiler says: Segmentation fault.

编译器说:分段错误。

In my tests I observed that probably my problem is in the assignment new2 = ret_vec(numbers);, but I don't know what is going on.

在我的测试中,我发现可能我的问题在于赋值new2 = ret_vec(数字);但我不知道发生了什么。

#include <string>
#include <iostream>
#include <vector>

using namespace std;

vector<int> ret_vec(vector<int>n){
    vector <int> n2;

    for (int i = 0; i< n.size(); ++i){
        n2[i] = n[i];
    }

    return n2;
}

void initializer(int s, vector<int>& n){

    for (int i = 0; i< s; ++i){
        n.push_back(i);
    }
}

void print_vector(vector<int> n){

    for (int i = 0; i<n.size(); ++i){
        cout << n[i] << " ";
    }
}

int main () {
    vector <int> numbers;
    int size;

    cin >> size;
    initializer(size,numbers);

    vector <int> new2(numbers.size());

    cout << "Old ";
    print_vector(numbers);

    new2 =  ret_vec(numbers);

    cout << "New ";
    print_vector(new2);

    return 0;

}

2 个解决方案

#1


In the following function

在以下功能中

vector<int> ret_vec(vector<int>n){
    vector <int> n2;

    for (int i = 0; i< n.size(); ++i){
        n2[i] = n[i];
    }

    return n2;
}

You are just copy the content of the parameter vector. (I thing you forgot a space between the parameter and the typ of it)

您只需复制参数向量的内容即可。 (我忘了参数和它的典型之间的空格)

You can revers the order like this way, too (its reverse it "by hand"):

您也可以像这样颠倒顺序(它反过来“手动”):

vector<int> ret_vec(vector<int> n){
    vector <int> n2;

    for(int i=n1.size()-1; i<=0; --i) 
    { 
        n2.push_back(n1[i]);
    }

    return n2;
}

#2


I am trying to return a vector with the values in the reverse order

我试图以相反的顺序返回值为向量的向量

The simplest approach would be to use C++:

最简单的方法是使用C ++:

vector<int> reverse_vec(const vector<int>& n)
{
  vector<int> ret(n.rbegin(), n.rend());
  return ret;
}

Here, reverse iterators are used to construct a vector with the contents of the input vector reversed. Alternatively, if you want to reverse a vector in place, use std::reverse:

这里,反向迭代器用于构造一个向量,其中输入向量的内容被反转。或者,如果要在适当的位置反转向量,请使用std :: reverse:

vector<int> v = ....
std::reverse(v.begin(), v.end()); // v is now reversed

As for problems with your code, an obvious one is that you are accessing a vector out of bounds here:

至于你的代码问题,一个显而易见的问题是你在这里访问一个超出范围的向量:

vector <int> n2; // empty vector

for (int i = 0; i< n.size(); ++i){
    n2[i] = n[i]; // oops! n2 accessed out of bounds!
}

The fix is not to use this code at all and go for the simple option.

修复程序根本不使用此代码,而是选择简单的选项。

#1


In the following function

在以下功能中

vector<int> ret_vec(vector<int>n){
    vector <int> n2;

    for (int i = 0; i< n.size(); ++i){
        n2[i] = n[i];
    }

    return n2;
}

You are just copy the content of the parameter vector. (I thing you forgot a space between the parameter and the typ of it)

您只需复制参数向量的内容即可。 (我忘了参数和它的典型之间的空格)

You can revers the order like this way, too (its reverse it "by hand"):

您也可以像这样颠倒顺序(它反过来“手动”):

vector<int> ret_vec(vector<int> n){
    vector <int> n2;

    for(int i=n1.size()-1; i<=0; --i) 
    { 
        n2.push_back(n1[i]);
    }

    return n2;
}

#2


I am trying to return a vector with the values in the reverse order

我试图以相反的顺序返回值为向量的向量

The simplest approach would be to use C++:

最简单的方法是使用C ++:

vector<int> reverse_vec(const vector<int>& n)
{
  vector<int> ret(n.rbegin(), n.rend());
  return ret;
}

Here, reverse iterators are used to construct a vector with the contents of the input vector reversed. Alternatively, if you want to reverse a vector in place, use std::reverse:

这里,反向迭代器用于构造一个向量,其中输入向量的内容被反转。或者,如果要在适当的位置反转向量,请使用std :: reverse:

vector<int> v = ....
std::reverse(v.begin(), v.end()); // v is now reversed

As for problems with your code, an obvious one is that you are accessing a vector out of bounds here:

至于你的代码问题,一个显而易见的问题是你在这里访问一个超出范围的向量:

vector <int> n2; // empty vector

for (int i = 0; i< n.size(); ++i){
    n2[i] = n[i]; // oops! n2 accessed out of bounds!
}

The fix is not to use this code at all and go for the simple option.

修复程序根本不使用此代码,而是选择简单的选项。