【问题标题】:Partition of Array by Element given X按给定 X 的元素对数组进行分区
【发布时间】:2022-01-26 01:28:46
【问题描述】:

我正在尝试查找 数组的分区,通过检查变量 x 的条件,当小于 x 时,它们将位于一侧或另一侧。 但我的代码需要一些更正。 这里我找不到错误,如果你能帮助我,我会很感激你。 代码是:-

#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
    for(int i=0;i<n;){
        if(arr[i]<x){
            i++;
        }
        else if(arr[i]==x){   
            int temp=arr[i];
            arr[i]=arr[n];
            arr[n]=temp;
            i--;
        }
        else if(arr[i]>x){
            int temp=arr[i];
            for(int j=i;j<n;j++){
                arr[j]=arr[j+1];   
            }
            arr[n]=temp;
           i--;
        }
    } 
    return 0;
}


int main(){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];    
    }
    int x;
    cin>>x;
    
    partition(arr,n,x);
    
     for(int i=0;i<n;i++){
        cout<<arr[i]<<"\t";
     }
    return 0;
}

Input &gt;&gt; array={2,10,15,1,3,15} ,x=10

Expected &lt;&lt; {2,1,3,10,15,15}

Output I get &lt;&lt; nothing .

【问题讨论】:

  • 欢迎来到 Stack Overflow。此代码无法编译;请发布您实际使用的代码。另外,在这种情况下最好的办法是调试程序。这是每个程序员都必须学习的基本技能。
  • ...乍一看,您的函数读取到数组末尾之后,这会导致 未定义的行为。(如果您不知道那是什么,请看起来。)
  • 请问您要的是稳定分区还是普通的不稳定分区。您的预期输出看起来像一个稳定的分区,但在问题中,您只需说“分区”而且您的解决方案看起来更像“不稳定”分区。这些信息非常重要。我假设您的意思是“不稳定”。 . .

标签: c++ arrays sorting compiler-errors partition


【解决方案1】:

代码没有给出任何输出,因为首先,“cin”和“cout”是大写的,这在语法上是不正确的,其次,变量 j 在循环语句中的大小写不同,而在第二个 else 中的主体分区函数中的 -if 子句,main() 函数中第一个 for 循环中的“I”也是如此。解决这个问题,你应该很高兴。

【讨论】:

    【解决方案2】:

    首先在 C++ 中,数组的大小必须是编译时常量。因此,例如,考虑以下示例:

    int n = 10;
    int arr[n]; //INCORRECT
    

    上面的正确写法是:

    const int n = 10;
    int arr[n]; //CORRECT
    

    同样,在您的代码中,

    int n;
    cin>>n;
    int arr[n]; //INCORRECT because n is not a constant expression
    

    第二,在你的代码中,当你写的时候:

    arr[n] = temp; Undefined behavior
    

    你会越界,所以你有未定义的行为

    解决方案

    您可以使用std::stable_partitionstd::vector 来解决您的问题,如下所示:

    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    int main()
    {
        int n;
        std::cout <<"Enter n:"<<std::endl;
        std::cin >> n;
        
        std::vector<int> arr(n); //create a vector of size n instead of an array 
        
        std::cout<<"Enter elements: "<<std::endl;
        //iterate and take input from user
        for(int &elem: arr){
            std::cin >> elem ;     
        }
        int x;
        std::cout << "Enter x: "<<std::endl;
        std::cin>>x;
        
        //use std::partition
        std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
        
        std::cout<<"This is the partitioned vector: "<<std::endl;
        
        for(int i=0;i<n;i++)
        {
            std::cout<<arr[i]<<"\t";
        }
        return 0;
    }
    

    输出

    上述程序的输出如下:

    Enter n:
    6
    Enter elements: 
    2
    10
    15
    1
    3
    15
    Enter x: 
    10
    This is the partitioned vector: 
    2       1       3       10      15      15
    

    可以看到here

    【讨论】:

    • 阅读您的个人资料后非常友好的推荐。停止将std::endl 用于std::cout。第 2 条建议。尝试适应问题和 OP 的编程风格。您使用 std::stable_partition 但 OP 想要手动执行此操作。所以,你的解释很好,我真的很喜欢,不知何故走向了错误的方向。不要误解我。你的回答很好。
    • @ArminMontigny 感谢您的建议。对于您的第二条建议,我正在考虑编写(将其添加到我的答案中)一种手动分区 OP 的方法,但没有足够的时间来处理它。所以我认为最好指出当前程序中的错误,并至少让 OP 知道(如果他/她还不知道)有一种内置的方法可以做到这一点。OP 可能会发生这种情况不知道std::stable_partition,所以对于这种情况,这个答案可能会有所帮助。如果我有时间,也许我会添加手动分区方式。现在我尽我所能。再次感谢。
    猜你喜欢
    • 2020-04-18
    • 2017-10-04
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多