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

angelcm51 2008-07-15 11:00:30
代码如下:
#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;
}
...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyyapple 2008-07-15
  • 打赏
  • 举报
回复
i没有初始化0

改了下:

#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;
int i=0;
for(int j=0;j<word_count-1;j++){
lowest=j;
//检查光标是否跟随单词
i=0;
for(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(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;

getchar();
return 0;
}

64,636

社区成员

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

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