排序使用C中的sort()一个二维字符数组++ [英] Sort a 2D character Array using sort() in C++

查看:280
本文介绍了排序使用C中的sort()一个二维字符数组++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维字符数组(我不想使用std ::字符串数组)。我怎么能以升序字符串(字符*)根据字符串的长度使用std ::排序()进行排序?

我曾尝试以下。但是,这是行不通的。

 字符名称[100] [30];布尔补偿(为const char *一,为const char * B){
    返回strlen的(a)及LT; strlen的(B);
}诠释主(){
    ...
    //我想前n个字符串进行排序
    排序(地名,人名+ N,COMP); // N'LT = 100
    ...
}

我发现这些错误:

  1> E:\\ Program Files文件(x86)的在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3128):错误C2075:'_Val':数组初始化需要花背带
1> E:在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3150)\\程序文件(x86):见参考函数模板实例无效的std :: _ Insertion_sort1< _BidIt,布尔(__的cdecl *)(为const char *,为const char *),CHAR [30]>(_ BidIt,_BidIt,_Pr,_Ty(*))'被编译
1>同
1> [
1> _BidIt = CHAR(*)[30],
1> _Pr =布尔(__cdecl *)(为const char *,为const char *),
1> _Ty =的char [30]
1> ]
1> E:在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3270)\\程序文件(x86):见参考函数模板实例无效的std :: _ Insertion_sort< _RanIt,布尔(__的cdecl *)(为const char *,为const char *)>(_ BidIt,_BidIt,_Pr)正在编制
1>同
1> [
1> _RanIt = CHAR(*)[30],
1> _BidIt = CHAR(*)[30],
1> _Pr =布尔(__cdecl *)(为const char *,为const char *)
1> ]
1> E:在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3279)\\程序文件(x86):见参考函数模板实例无效的std :: _排序和LT; CHAR(*)[30],INT,布尔( __cdecl *)(为const char *,为const char *)>(_ RanIt,_RanIt,_Diff,_Pr)正在编制
1>同
1> [
1> _RanIt = CHAR(*)[30],
1> _Diff = INT,
1> _Pr =布尔(__cdecl *)(为const char *,为const char *)
1> ]
1> E:\\项目的视觉studio2008 \\样本\\样本\\ sorting.cpp(51):见参考函数模板实例无效的std ::排序< CHAR(*)[30],布尔(__的cdecl *)(为const char *,常量的char *)>(_ RanIt,_RanIt,_Pr)正在编制
1>同
1> [
1> _RanIt = CHAR(*)[30],
1> _Pr =布尔(__cdecl *)(为const char *,为const char *)
1> ]
1> E:\\ Program Files文件(x86)的在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3133):错误C2106:'=':左操作数必须是左值
1> E:\\ Program Files文件(x86)的在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3140):错误C2106:'=':左操作数必须是左值
1> E:\\ Program Files文件(x86)的在电子\\微软的Visual Studio 9.0 \\ VC \\包括\\算法(3141):错误C2106:'=':左操作数必须是左值
1>生成日志保存在file:// E:\\项目的视觉studio2008 \\样本\\样本\\调试\\ BuildLog.htm
1>样品 - 4错误(S),3个警告
==========生成:0成功,1失败,0了最新,0已跳过==========


解决方案

如果你的数据是在

 字符名称[100] [30];

那么你可以不指针,因为该数据结构不具有指针都只有100 * 30 = 3000个字符排序......此起彼伏。
因此做排序,你会需要真正围绕100行此举将他们的所有内容。

的std ::排序不能直接使用,因为数据结构是一个数组的数组,数组在C ++二等公民(例如,你不能分配阵列到另一个)。

I have a 2D character array (I don't want to use array of std::string). How can I sort the strings (char*) in ascending order according to the length of the string using std::sort()?

I have tried the following. But it doesn't work.

char names[100][30];

bool comp(const char* a, const char* b){
    return strlen(a)<strlen(b);
}

int main(){
    ...
    //I want to sort the first n strings 
    sort(names,names+n,comp); //n<=100
    ...
}

I have found these errors:

1>e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3128) : error C2075: '_Val' : array initialization needs curly braces
1>        e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3150) : see reference to function template instantiation 'void std::_Insertion_sort1<_BidIt,bool(__cdecl *)(const char *,const char *),char[30]>(_BidIt,_BidIt,_Pr,_Ty (*))' being compiled
1>        with
1>        [
1>            _BidIt=char (*)[30],
1>            _Pr=bool (__cdecl *)(const char *,const char *),
1>            _Ty=char [30]
1>        ]
1>        e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3270) : see reference to function template instantiation 'void std::_Insertion_sort<_RanIt,bool(__cdecl *)(const char *,const char *)>(_BidIt,_BidIt,_Pr)' being compiled
1>        with
1>        [
1>            _RanIt=char (*)[30],
1>            _BidIt=char (*)[30],
1>            _Pr=bool (__cdecl *)(const char *,const char *)
1>        ]
1>        e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3279) : see reference to function template instantiation 'void std::_Sort<char(*)[30],int,bool(__cdecl *)(const char *,const char *)>(_RanIt,_RanIt,_Diff,_Pr)' being compiled
1>        with
1>        [
1>            _RanIt=char (*)[30],
1>            _Diff=int,
1>            _Pr=bool (__cdecl *)(const char *,const char *)
1>        ]
1>        e:\projects visual studio2008\sample\sample\sorting.cpp(51) : see reference to function template instantiation 'void std::sort<char(*)[30],bool(__cdecl *)(const char *,const char *)>(_RanIt,_RanIt,_Pr)' being compiled
1>        with
1>        [
1>            _RanIt=char (*)[30],
1>            _Pr=bool (__cdecl *)(const char *,const char *)
1>        ]
1>e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3133) : error C2106: '=' : left operand must be l-value
1>e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3140) : error C2106: '=' : left operand must be l-value
1>e:\program files (x86) in e\microsoft visual studio 9.0\vc\include\algorithm(3141) : error C2106: '=' : left operand must be l-value
1>Build log was saved at "file://e:\projects visual studio2008\sample\sample\Debug\BuildLog.htm"
1>sample - 4 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

解决方案

If your data is in

char names[100][30];

then you cannot sort "the pointers" because that data structure has no pointers at all... just 100*30 = 3000 characters one after another. Therefore to do the sorting you will need to actually move around the 100 rows will all their content.

std::sort cannot be used directly because the data structure is an array of arrays, and arrays are in C++ second-class citizens (for example you cannot assign an array to another).

这篇关于排序使用C中的sort()一个二维字符数组++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆