c++ string 中文怎么排序

ghy_cool03 2008-09-29 08:08:52
如果我排例如: 电炉 电路 这种词的话,谁在前在后由什么决定?
...全文
1117 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
binjuny 2008-10-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tjianli 的回复:]
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1("电器");
    string str2("电炉");
    string str3("电话");

    cout < < (str1 < str2) < < endl;//0
    cout < < (str1 > str3) < < endl;//1
    cout < < (str2 > str3) < < endl;//1

wstring str4(L"电脑");
wstring str5(L"电话");
cout < < (str4 < str5) < < endl;//1

    return 0;
}
宽字符的好像…
[/Quote]
宽字符的好像用的就是wstring


typedef basic_string<char, char_traits<char>, allocator<char> > string;

typedef basic_string<wchar_t, char_traits<wchar_t>,allocator<wchar_t> > wstring;

elegant87 2008-10-02
  • 打赏
  • 举报
回复
按字典顺序排序的吧!
lzr4304061988012 2008-10-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tjianli 的回复:]
#include <iostream>
#include <string>

using namespace std;

int main()
{
string str1("电器");
string str2("电炉");
string str3("电话");

cout < < (str1 < str2) < < endl;//0
cout < < (str1 > str3) < < endl;//1
cout < < (str2 > str3) < < endl;//1

wstring str4(L"电脑");
wstring str5(L"电话");
cout < < (str4 < str5) < < endl;//1

return …
[/Quote]
正确
gaa1ugy 2008-10-01
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

using namespace std;

int main()
{
string str1("电器");
string str2("电炉");
string str3("电话");

cout < < (str1 < str2) < < endl;//0
cout < < (str1 > str3) < < endl;//1
cout < < (str2 > str3) < < endl;//1

wstring str4(L"电脑");
wstring str5(L"电话");
cout < < (str4 < str5) < < endl;//1

return …


此程序并非完全正确,因为字符串比较跟编码方式有关。在C++中,英文默认都是用ASCII码编码,而汉字则不一样,有可能是gb18030或者unicode等等。C++中并没有解决字符编码的功能,编译器一般是根据操作系统的默认编码方式来处理的
lizhaohu 2008-10-01
  • 打赏
  • 举报
回复
我觉得应该用拼音的循序进行比较
lann64 2008-10-01
  • 打赏
  • 举报
回复
这个的确是由编码方式决定的。英文字符也不仅仅是ASCII码,只不过我们常用ascii码,忽略了其他编码方式。
对于汉字,一定要有序的话,现在解决的最好途径是自己定义次序。生成自己的class。
guizi110 2008-10-01
  • 打赏
  • 举报
回复
确实是个问题,应该找个统一标准,不然可能会影响到汉字未来的发扬光大问题
study_live 2008-10-01
  • 打赏
  • 举报
回复
汉字比较应该是按拼音比较的
迷途的书童 2008-10-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tjianli 的回复:]
#include <iostream>
#include <string>

using namespace std;

int main()
{
string str1("电器");
string str2("电炉");
string str3("电话");

cout < < (str1 < str2) < < endl;//0
cout < < (str1 > str3) < < endl;//1
cout < < (str2 > str3) < < endl;//1

wstring str4(L"电脑");
wstring str5(L"电话");
cout < < (str4 < str5) < < endl;//1

return …
[/Quote]

不错不错,受教啦
mabo321 2008-10-01
  • 打赏
  • 举报
回复
由你自己决定。。。

别指望 string 给你排序~~~

chlaws 2008-10-01
  • 打赏
  • 举报
回复
自己写个比较的就成了
jay的Fans 2008-09-29
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

using namespace std;

int main()
{
string str1("电器");
string str2("电炉");
string str3("电话");

cout << (str1 < str2) << endl;//0
cout << (str1 > str3) << endl;//1
cout << (str2 > str3) << endl;//1

wstring str4(L"电脑");
wstring str5(L"电话");
cout << (str4 < str5) << endl;//1

return 0;
}
宽字符的好像不是按照拼音来的
runtospring 2008-09-29
  • 打赏
  • 举报
回复
这怎么排序?按拼音?按比划?
chgq456 2008-09-29
  • 打赏
  • 举报
回复
TimidSparrow 2008-09-29
  • 打赏
  • 举报
回复
估计按照Unicode编码排序
hityct1 2008-09-29
  • 打赏
  • 举报
回复
直接按汉字的编码不行吗?
帅得不敢出门 2008-09-29
  • 打赏
  • 举报
回复
由自己决定吧.就当比较二进制似乎无顺序可言.

这篇文章可以参考下
http://www.diybl.com/course/3_program/c++/cppjs/2008324/106096.html

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧