C++中如何对一个string型数组进行排序?

cpccai 2005-10-08 12:22:08
如题
如果用STL中的sort函数,应如何调用呢?
...全文
1920 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gamelearner 2005-10-08
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

void main()
{
string strArray[2] = { "hello", "abc" };
sort(strArray, strArray+2); // 排序的起始地址和结束地址后一位

for (int i = 0; i < 2; i++)
{
cout << strArray[i].c_str() << endl;
}
}
希望之晨 2005-10-08
  • 打赏
  • 举报
回复
回复人: xlsue(爱的下半场) ( ) 信誉:100 2005-10-08 07:44:00 得分: 0
同意楼上的

楼上写的并不是很好,可用虚无的方法

补充下: sort() 可以自己定义比较函数.
如 sort(strArray.begin(), strArray.end() ,sortfunction);
你可以自己定义sortfunction
xlsue 2005-10-08
  • 打赏
  • 举报
回复
是啊,同意楼上的,有点多此一举。我只注意他的排序那里:)
zhouhuahai 2005-10-08
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int main()
{
vector<string> strArray ;
string str[4] = {"hello, world!", "welcome to cpp.", "effective c++", "exceptional c++"};

for(int i = 0; i<4; i++)
strArray.push_back(str[i]);


sort(strArray.begin(), strArray.end());

vector<string>::iterator st;
for(st = strArray.begin(); st != strArray.end(); st++)
cout<<*st<<endl;

system("pause");
return 0;
}
二楼好像对string还不熟, 对string来说,可以直接用cout<<.
xlsue 2005-10-08
  • 打赏
  • 举报
回复
同意楼上的

64,649

社区成员

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

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