随机产生的单词文档排序

#include#include#include#include#include#include#include #include #include#include#include#includeusing namespace std;void makefile()//写文件{ofstream outfile("data.db", ios::out);if(outfile.fail()){cerr<<"file open error!"<<endl;}srand(time(NULL));unsigned int count = 100000;char buffer[20] = {0};while(count--){int length = rand()%10+1;for(char *p=buffer; p<buffer+length; ++p){int index = rand()%26;*p = 'a'+index;}outfile<<buffer<<" ";}outfile.close();}bool comparer(string a,string b){if((strcmp(a.c_str(),b.c_str()))<0) { return 1; } else { return 0; }}int main(){makefile();string str,temp;vector inputvec;ifstream str1("data.db");while(getline(str1,str,' ')){ inputvec.push_back(str);//将文件单词全写入容器}str1.close();ofstream outfile;outfile.open("dict.db");sort(inputvec.begin(),inputvec.end(),comparer);for(vector::iterator i=inputvec.begin(); i!=inputvec.end(); i++){outfile<<*i<<' ';} outfile.close();cout<<"Go! And check the result!";return 0;}
阅读更多
文章标签: 排序 文件流
个人分类: C++
下一篇由浅入深理解数据库中索引
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭