c++中cin.clear()的重要性

2242阅读 0评论2012-08-09 licong0527
分类:

  在学习C++的用法时,有这样一个简单的问题,却让我对cin.clear()重新认识了。
  题目:
         读取一系列int型数据,将其存入vector对象中,然后使用count()统计某个指定值出现的次数。
  下面是我的程序:

点击(此处)折叠或打开

  1. int
  2. main(void)
  3. {
  4.     int num;
  5.     vector< int >ivec;
          
           while( cin >> num )
                ivec.push_back(num);

           cout << "Please input the number you want to count:"
           cin >> num;

           cout << num << " appears " << count(ivec.begin(), ivec.end(), num) << " times!" << endl;

           return 0;
上一篇:libc库和系统调用
下一篇:c++中cin.clear()的重要性