给字符串排序,编译没问题,运行有问题

angelcm51 angelcm51 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:
#include <iostream>
#include <string>
using namespace std;
int main(){
string text;
const string separators=" ,.\"\n";
//从键盘录入字符串
cout<<endl<<"Enter a string terminated by # : "<<endl;
getline(cin,text,'#');

size_t start=text.find_first_not_of(separators);
size_t end=0;
int word_count=0;

while(start!=string::npos){
end=text.find_first_of(separators,start+1);
if(end==string::npos)
end=text.length();
word_count++;
//找到下一个单词里面第一个字符
start=text.find_first_not_of(separators,end+1);
}
//创建指向单词的指针数组
string** pwords=new string*[word_count];

//创建单词对象
start=text.find_first_not_of(separators);
end=0;
int index=0;
while(start!=string::npos){
end=text.find_first_of(separators,start+1);
if(end==string::npos)
end=text.length();
pwords[index++]=new string(text.substr(start,end-start));
//找到下一个单词里面第一个字符
start=text.find_first_not_of(separators,end+1);
}
//排序并输出单词
int lowest=0;
for(int j=0;j<word_count-1;j++){
lowest=j;
//检查光标是否跟随单词
for(int i=j+i;i<word_count;i++)
if(*pwords[i]<*pwords[lowest])
lowest=i;

if(lowest!=j){
string* ptemp=pwords[j];
pwords[j]=pwords[lowest];
pwords[lowest]=ptemp;
}
}
char ch=(*pwords[0])[0];
int words_in_line=0;
for(int i=0;i<word_count;i++){
if(ch!=(*pwords[i])[0]){
cout<<endl;
ch=(*pwords[i])[0];
words_in_line=0;
}

cout<<*pwords[i]<<" ";
if(++words_in_line==6){
cout<<endl;
words_in_line=0;
}
}
//释放堆区内存
for(i=0;i<word_count;i++)
delete pwords[i];

delete [] pwords;
return 0;
}
...全文

相关推荐

MySQL关于字符串中数字排序的问题分析

本文实例讲述了MySQL关于字符串中数字排序的问题。分享给大家供大家参考,具体如下: MySQL字符串相信大家都不陌生,在MySQL字符串排序时经常会遇到一些问题,比如下面要介绍的这个 今天解决了一个关于MySQL字符串排序的很奇怪的问题,在数据里面定义的是varchar类型,实际存放的是Int类型的数据,按一下查询语句进行排序: 将字段*1或者+0可以将MySQL字符串字段按数值排序 如: select * from table where 1 order by id*1 desc; 或者 select * from table where 1 order by id+0 desc;

weixin_38663151

解决Oracle字符串中包含数字、特殊符号的排序问题

最近做项目遇到这样的需求,要求实现某小区需要按照小区、楼栋、单元号、房间号进行排序。看似很简单的一个需求,一条sql语句搞定,其实套路很深,下面小编给大家分享下Oracle字符串中包含数字、特殊符号的排序问题

weixin_38738528

java字符串数组进行大小排序的简单实现

下面小编就为大家带来一篇java字符串数组进行大小排序的简单实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

weixin_38723753

字符串排序方法

字符串排序方法 javaScript中的字符串排序。

cxw3152

python字符串排序方法

本文以实例形式简述了Python实现字符串排序的方法,是Python程序设计中一个非常实用的技巧。分享给大家供大家参考之用。具体方法如下: 一般情况下,python中对一个字符串排序相当麻烦: 一、python中的字符串类型是不允许直接改变元素的。必须先把要排序的字符串放在容器里,如list。 二、python中的list容器的sort()函数没返回值。 所以在python中对字符串排序往往需要好几行代码。 具体实现方法如下: >>> s = "string" >>> l = list(s) >>> l.sort() >>> s = "".join(l) >>> s 'ginrst' 对于

weixin_38627104
评论 1
收藏 已收藏
分享
返回顶部
angelcm51
C++ 语言 65,213 成员 250,513 内容