免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4807 | 回复: 5
打印 上一主题 下一主题

[C++] 这个WHILE循环怎么退出啊?求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-04 19:23 |只看该作者 |倒序浏览
//calculate the time of the given words
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>


using std::cout;     using std::string;
using std::cin;      using std::vector;
using std::endl;     using std::sort;

int main()
{
   //ask for input the words
   cout << "input a word: ";
   vector<string> st;
   string s;
   while(cin >> s )                           
      st.push_back(s);
  
   //sort the string st
   sort(st.begin(),st.end());

   //define two vectors vectors<string> st1 and vector<int> v
   //to save the word and the time
   vector<string> st1;
   vector<int> v;

   //loop variant i and count
   int i = -1, count = 1;

   //find the word
   for(vector<string>::size_type iv = 0; iv != st.size(); ++iv){
        i += 1;
        while (st == st[i+1]){
            ++count;
            ++i;
        }
       st1.push_back(st);
       v.push_back(count);
    }

   //output the word and the appear time
   cout << "the word is: " << endl;
   for(vector<string>::size_type j = 0; j != st1.size(); ++j)
         cout << st[j] << "\t";
   cout << "the time is: " <<endl;
   for(vector<int>::size_type t = 0; t != v.size(); ++t)
      cout << v[t] << "\t";
     
   system("pause");   
   return 0;
                                 
}

这个WHILE循环怎么退出啊?退出后还要进行统计出现的字符数.比如:aaa bbb ccc aaa bbb ccc aa bb ,    出现次数:aa  1,bb 1,aaa 2,bbb 2, ccc 2

[ 本帖最后由 bayernrimnm 于 2008-6-4 20:19 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-06-04 19:31 |只看该作者
在网上找了很久,没找到,都是些退出了循环,程序也结束了,有没有知道的,谢了

论坛徽章:
0
3 [报告]
发表于 2008-06-04 20:01 |只看该作者
是直接粘贴的代码吗
怎么乱七八糟的啊

[ 本帖最后由 zhuhefang2006 于 2008-6-4 20:03 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-06-04 20:05 |只看该作者
是的  不好意思 把它敲出来

论坛徽章:
0
5 [报告]
发表于 2008-06-04 22:10 |只看该作者
问题已解决,在调试的时候才看得到结果!!

论坛徽章:
0
6 [报告]
发表于 2008-06-05 09:00 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP