请问想使用sort对一个string型的数组进行排序这么做为什么会出错?

战国9s 2014-02-10 09:32:23

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
using namespace std;
int comp(const string &s1,const string &s2){

string temp1;
string temp2;
transform(s1.begin(),s1.end(),temp1.begin(),::tolower);
transform(s1.begin(),s1.end(),temp2.begin(),::tolower);
return strcmp(temp1.c_str(),temp2.c_str());
}
int main(){

int n;
string s[201];
while(cin>>n)
{
int i;
for(i=0;i<n;i++)
{
cin>>s[i];
}
sort(s,s+n,comp);
for(i=0;i<n;i++)
{
cout<<s[i];
}
cout<<endl;
}
return 0;
}

功能是想对string数组里面的内容进行不区分大小写的排序,请问这样做为什么SORT函数会出现错误,十分感谢!
...全文
147 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2014-02-10
  • 打赏
  • 举报
回复
楼主,你需要找本<effective stl>认真啃一下了。 stl如果瞎用,会死很惨的。
turing-complete 2014-02-10
  • 打赏
  • 举报
回复
发现一个逻辑错误,请参考 http://en.cppreference.com/w/cpp/algorithm/sort 注意sort()的谓词的函数签名
战国9s 2014-02-10
  • 打赏
  • 举报
回复
程序执行到sort函数那里会弹出DEBUG的对话框,DEBUG也调试过了,应该是空指针的问题,但是想不出哪里会出现这种错误。
derekrose 2014-02-10
  • 打赏
  • 举报
回复
什么叫出错 你必须要说清楚

65,186

社区成员

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

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