以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  [讨论]关于对字符串中各单词按字母顺序排序的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=63092)


--  作者:tianzhaoling
--  发布时间:5/27/2008 10:29:00 AM

--  [讨论]关于对字符串中各单词按字母顺序排序的问题
在网上看到一段代码,其作用是把一个字符串中各单词按字母表顺序进行排序,并在排序后输出,这段代码比自己写的要简短得多,有朋友还有更简洁的实现方法么?

void main()
{
    vector<char *> words;

 char source[] = "Most websites and web applications run smoothly and correctly as long as only one user e.g."
              "the original developer or just a few users are visiting at a given time. But what happens if"
     "thousands of users access the website or web application at the same time?"
              "Using Webserver Stress Tool you can simulate various load patterns for your"
     "webserver which will help you to find problems in your webserver set up With"
     "steadily increasing loads you are able to find out how much load you server can"
     "handle before serious problems arise";

 char *t = strtok(source," ");

 while(t) {
  t[0] = isupper(t[0]) ? tolower(t[0]) : t[0];
  words.push_back(t);
  t = strtok(NULL," ");
 }

 for(int i=0,p=words.size()-1,sub=words.size()-1; i<sub; ++i,--p)
 {
  for(int x=0; x<p; ++x)
  {
   if ( strcmp(words[x],words[x+1]) == 1 )
   {
    char *temp = words[x];
    words[x] = words[x+1];
    words[x+1] = temp;

   }
  }
 }

 copy(words.begin(),words.end(),ostream_iterator<char *>(cout,"\n"));
}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms