Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ b从编程珍珠中分离实现_C++_String_Sorting_Alphabetical Sort - Fatal编程技术网

C++ b从编程珍珠中分离实现

C++ b从编程珍珠中分离实现,c++,string,sorting,alphabetical-sort,C++,String,Sorting,Alphabetical Sort,这个问题是我之前在堆栈溢出问题上提出的一个后续问题: 我想向你们展示我的作品,并对它提出一些问题 #include<string.h> #include<iostream> using namespace std; void swap(char *x[],int i,int j) { char *t=x[i]; x[i]=x[j]; x[j]=t; } int get_bit(char *s,int depth) { int requi

这个问题是我之前在堆栈溢出问题上提出的一个后续问题:

我想向你们展示我的作品,并对它提出一些问题

#include<string.h>
#include<iostream>
using namespace std;
void swap(char *x[],int i,int j)
{
    char *t=x[i];
    x[i]=x[j];
    x[j]=t;
}

int get_bit(char *s,int depth)
{
    int required_bit=13;
    int bit=required_bit&0x7;
    int byte=required_bit>>3;
    int val=(s[byte]>>bit)&0x1;
    return val;
}

void bsort(char *x[],int l,int u,int depth)
{
    if(l>=u) return ;
    for(int i=l;i<=u;i++){
        if(strlen(x[i])<depth){
            swap(x,i,l++);
        }
    }
    int m=l;
    for(int i=l;i<u;i++)
    {
        if(get_bit(x[i],depth)==0)
        {
            swap(x,i,m++);
        }
    }

    bsort(x,l,m-1,depth+1);
    bsort(x,m,u,depth+1);
}

int main()
{
    char *x[6]={"car","bus","snow","earth","dog","mouse"};
    bsort(x,0,5,1);
    for(int i=0;i<6;i++)
        cout<<x[i]<<"  "<<endl;

    return 0;
}

第一个
b
c
之前,汽车和公共汽车在正确的位置吗?请告诉我我的代码有什么问题。

它是按长度对字符串进行排序的。输出字符串的长度顺序是递增的,因此对于这个示例,至少它似乎工作正常,是的。
car  
bus  
dog  
snow  
earth  
mouse