使用MaxPairwiseProduct程序的分段错误(核心转储)运行时错误

时间:2018-12-16 09:51:40

标签: c++ algorithm pointers memory vector

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int main()
{
    int MaxPairwiseProduct(vector<int>& numbers);
    //declaration
    vector<int> *x;
    x->push_back(1);
    x->push_back(2);

    int answer = MaxPairwiseProduct(*x);
    cout << answer;
}

int MaxPairwiseProduct(vector<int>& numbers) {
    int index1 =  1;
    int index2;

    //vector<int> numbers = number;
    int n = numbers.size();
    for(int i = 2;i < n;++i){
        if(numbers[i]>numbers[index1]){
            index1 = i;
            }
    }
    if(index1 == 1){
        index2 = 2;
    } else {
    index2 = 1;
    }

    for(int i = 1;i < n;++i){
        if(numbers[i] != numbers[index1] && numbers[i]>numbers[index2]){
            index2 = i;
        }
    }
    numbers[index1] * numbers[index2];
    return numbers[index1] * numbers[index2];
}

我正在尝试实施一种看似先进的算法来查找最大对数乘积。我继续遇到分段错误(核心已转储)错误,并且我知道这可能与指针和函数范围有关。有什么建议或提示吗?

2 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int main()
{
    int MaxPairwiseProduct(vector<int>& numbers);
    //declaration
    vector<int> *x = new vector<int>();
    x->push_back(7);
    x->push_back(4);
    x->push_back(5);
    x->push_back(6);


    int answer = MaxPairwiseProduct(*x);

    cout << answer;delete x;
}

int MaxPairwiseProduct(vector<int>& numbers) {
    int index1 =  1;
    int index2;

    //vector<int> numbers = number;
    int n = numbers.size();
    for(int i = 0;i < n;++i){
        if(numbers[i]>numbers[index1]){
            index1 = i;
            }
    }
    if(index1 == 1){
        index2 = 2;
    } else {
    index2 = 1;
    }

    for(int i = 0;i < n;++i){
        if(numbers[i] != numbers[index1] && numbers[i]>numbers[index2]){
            index2 = i;
        }
    }

    return numbers[index1] * numbers[index2];
}

我上班了,有一个奇怪的深夜屁。

答案 1 :(得分:0)

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


int main(){
    long MaxPairwiseProductFast(vector<long>& numbers);

    long N, input;
    vector<long> *V = new vector<long>();
    cin >> N;


    do {
                V->push_back(input);}
    while (V->size() <= N && cin >> input);

    long answer = MaxPairwiseProductFast(*V);

    cout << answer;
    delete V;
}

 long MaxPairwiseProductFast(vector<long>& numbers) {
    int index1 =  1;
    int index2 = 1;

    int n = numbers.size();
    for(int i = 2;i < n;++i){
        if(numbers[i]>numbers[index1]){
            index1 = i;
            }
    }
    if(index1 == 1){
        index2 = 2;
    } else {
    index2 = 1;
    }

    for(int i = 2;i < n;++i){
        if(i != index1 && numbers[i]>numbers[index2]){
            index2 = i;
        }
    }

    return  (long)(numbers[index1] * numbers[index2]);
}

long MaxPairwiseProduct(vector<long>& A) {
int index = 1;
int n = A.size();
for(int i = 2; i < n; ++i){
    if (A[i] > A[index]){
        index = i;
    }
}
swap(A[index], A[n]);
index = 1;
for (int i = 2; i < n-1; ++i){
    if(A[i] > A[index]){
        index = i;
    }
}
swap(A[index], A[n - 1]);
return A[n-1] * A[n];
}

具有用户定义的矢量大小和定义用户输入值的功能的修改版本。