| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1152 人关注过本帖
标题:关于qsort字符串数组排序的问题
只看楼主 加入收藏
lu3664198
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:55
专家分:185
注 册:2014-11-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
关于qsort字符串数组排序的问题
编译通过,运行后排序不正确,求解救
代码:
程序代码:
#include <iostream>
#include <string>
int cmp(const void *a, const void *b);
using namespace std;
int main()
{
    int b;
    cin >> b;//输入字符串个数
    string a[1000];
    for (int n = 0; n < b; n++){
        cin >> a[n];
    }
    qsort(a, b, sizeof(a[0]), cmp);
    for (int n = 0; n < b; n++){
        cout << a[n] << endl;
    }
    return 0;
}
int cmp(const void *a, const void *b) {
    return (strcmp((char *)a, (char *)b));
}

运行输入:
6
asdfghjk
aaaaa
zxcvbn
zaaaaa
bczzzz
bbbbbb
结果:
bbbbbb
zxcvbn
aaaaa
zaaaaa
asdfghjk
bczzzz
搜索更多相关主题的帖子: 字符串 
2015-03-14 19:52
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:10 
程序代码:
// g++ -Wall -fomit-frame-pointer -funroll-loops -Ofast -march=corei7-avx -msse4.2 -mavx -std=c++11 sort.cpp -lm -o sort
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::sort;

int main(void)
{
    int num = 0;
    cout << "Number: ";
    cin >> num;
    cout << "Please input " << num << " strings:" << endl;
    vector<string> vec_str(num, "");
    for (auto &i : vec_str)
        cin >> i;
    sort(vec_str.begin(), vec_str.end());
    cout << "Result:"<< endl;
    for (auto i : vec_str)
        cout << i << endl;
   
    return 0;
}


C++11大法好
2015-03-14 20:35
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:10 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 99999
#define LEN 99999

#define MALLOC(p, n, t)\
    if(!(p = (t*)malloc((n) * sizeof(t)))) {\
        fprintf(stderr, "malloc error...");\
        exit(EXIT_FAILURE);\
    }

#define FREE(p) if(p) free(p), p = NULL

void sort(char *p[], int n) {
    char *pt;
    int i, j;

    for(i = 0; i < n - 1; i++) {
        for(j = i + 1; j < n; j++) {
            if(strcmp(p[i], p[j]) > 0) {
                pt = p[i];
                p[i] = p[j];
                p[j] = pt;
            }
        }
    }
}

int main(void) {
    char *p[N];
    int i = 0, n = 0;
    puts("输入多行字符串 (空行退出)");

    do {
        MALLOC(p[i], LEN, char);
        fgets(p[i], LEN, stdin);
        p[i][strlen(p[i]) - 1] = '\0';
    } while(*p[i++] != '\0' && ++n < N);

    if(n > 0) {
        sort(p, n);
        puts("排序如下:");

        for(i = 0; i < n; i++) {
            puts(p[i]);
            FREE(p[i]);
        }
    }

    return 0;
}

Only the Code Tells the Truth             K.I.S.S
2015-03-14 20:38
lu3664198
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:55
专家分:185
注 册:2014-11-16
收藏
得分:0 
回复 2楼 zklhp
谢谢,我对c++还是有点不熟悉....其实之所以用c++是因为刚好开始学而已( ̄▽ ̄"),那为啥我的代码无法正确排序呢?哪里错了呀?

wwwwwww...~~;
本人为大一新生,说的不好不要拍我 ~ ( ̄~ ̄) ;
2015-03-14 23:30
lu3664198
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:55
专家分:185
注 册:2014-11-16
收藏
得分:0 
回复 3楼 longwu9t
用预处理器分配,判断,回收内存,然后用冒泡法排序?我好想有点明白(思考

wwwwwww...~~;
本人为大一新生,说的不好不要拍我 ~ ( ̄~ ̄) ;
2015-03-14 23:43
快速回复:关于qsort字符串数组排序的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016295 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved