【问题标题】:I am getting Segmentation fault while reversing vectors without using STL我在不使用 STL 的情况下反转向量时遇到分段错误
【发布时间】:2020-04-21 16:54:33
【问题描述】:

尽管反转数组有效,但我无法使用以下代码反转向量。 请尽早查看,因为我无法正确执行。 在数组中,我在返回并返回指针函数时使用静态,但在这里我无法弄清楚。

#include <bits/stdc++.h>
    using namespace std;


    vector<int> reversearray(vector<int> a,int n)
    {
        int low=0, high=n-1;

        while(low<high)
        {
            swap(a[low],a[high]);
            low++;
            high--;
        }

        return a;


    }
    int main() {

        int t,n;
        vector<int> v,ans;
        cin>>t;
        while(t--)
        {
            cin>>n;

            for(int i=0;i<n;i++){
                cin>>v[i];
            }

            ans=reversearray(v,n);
            for(int i=0;i<n;i++)
                cout<<ans[i]<<" ";
            //Simple solution would be to iterate in reverse order
            /*for(int i=n-1;i>=0;i--){
                cout<<v[i]<<" ";
            }*/
           cout<<"\n";
        }
        return 0;
    }

【问题讨论】:

    标签: c++ c++11 data-structures c++17


    【解决方案1】:

    您遇到分段错误的事实是您正在尝试访问未启动的内存。

    您不能直接扫描v[i] 的输入,因为向量v 的大小最初为0。

    改为使用push_back() 方法或将向量初始化或调整为n 的大小。

    看看下面的实现:

    #include <iostream>
    #include <vector>
    
    std::vector<int> reversearray(std::vector<int> a,int n)
    {
        int low=0, high=n-1;
    
        while(low<high)
        {
            std::swap(a[low],a[high]);
            low++;
            high--;a
        }
    
        return a;
    
    
    }
    
    int main() {
    
        int t,n;
        std::vector<int> v,ans;
        std::cin>>t;
        while(t--)
        {
            std::cin>>n;
    
            for(int i=0;i<n;i++){
                int x;
                std::cin>>x;
                v.push_back(x);
            }
    
            ans=reversearray(v,n);
            for(int i=0;i<n;i++)
                std::cout<<ans[i]<<" ";
    
            //Simple solution would be to iterate in reverse order
            /*for(int i=n-1;i>=0;i--){
                cout<<v[i]<<" ";
            }*/
    
            std::cout<<"\n";
    
            v.clear();
        }
        return 0;
    }
    

    GFG 判决:

    如果你想使用v[i]

    #include <iostream>
    #include <vector>
    
    std::vector<int> reversearray(std::vector<int> a,int n)
    {
        int low=0, high=n-1;
    
        while(low<high)
        {
            std::swap(a[low],a[high]);
            low++;
            high--;
        }
    
        return a;
    
    
    }
    
    int main() {
    
        int t,n;
        std::vector<int> v,ans;
        std::cin>>t;
        while(t--)
        {
            std::cin>>n;
    
            v.resize(n);
    
            for(int i=0;i<n;i++){
                std::cin>>v[i];
            }
    
            ans=reversearray(v,n);
            for(int i=0;i<n;i++)
                std::cout<<ans[i]<<" ";
    
            //Simple solution would be to iterate in reverse order
            /*for(int i=n-1;i>=0;i--){
                cout<<v[i]<<" ";
            }*/
    
            std::cout<<"\n";
        }
        return 0;
    }
    

    PS:我建议不要使用 bits/stdc++.husing namespace std。你可以找出原因。

    【讨论】:

    • 但是为什么我们不能使用 v[i]?请你解释一下它在哪里初始化为0。
    • 但是为什么我们需要调整它的大小,因为在数组中我们从不这样做。你能解释一下它背后的概念吗?
    • 但是它的动态数组,它自己分配内存,也是 Iupvoted 但我需要 15 个信誉。
    • 你能回答这个问题,但它的动态数组,它自己分配内存吗?
    • practice.geeksforgeeks.org/problems/reverse-an-array/0/… 运行此代码,测试用例失败,请根据此更新代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多