关于while(cin>>value)怎么停

一开始搞来搞去发现总是没办法显示最后一个数的time,然后现在的代码是输入数字回车,显示除了最后一个其余数字的出现次数,之后按Ctrl+Z,再按回车(跳出while循环),就会显示按任意键继续,就可以执行下面的代码了。


#include<iostream>
using namespace std;
int main(){
	int currVal = 0, val = 0;
	int cnt = 0;
	if (cin >> currVal){
		cnt = 1;
		cout << "开始" << endl;
		while (cin >> val){
			if (val == currVal)
			{
				++cnt;
				cout << "计数" << endl;
			}
			else
			{
				cout << currVal << " occurs " << cnt << " times " << endl;
				currVal = val;
				cnt = 1;
				cout << "换数"<< endl;
			}
		}
		
		cout << currVal << " occurs " << cnt << " times " << endl;
	}
	system("PAUSE");
	cout << "end" << endl;
	system("PAUSE");
return 0;

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的实现,包括了Table类以及其三个模板类DoubleTable、IntTable和CharTable。其中,每个模板类都继承自Table类,并重载了min函数以满足题目要求。 ```c++ #include <iostream> #include <map> #include <algorithm> using namespace std; template<class T> class Table { protected: map<int, T> data; const int capacity; public: Table(int capacity) : capacity(capacity) {} T& operator[](int key) { return data[key]; } virtual void min() const = 0; }; template<class T> class DoubleTable : public Table<T> { public: DoubleTable(int capacity) : Table<T>(capacity) {} void min() const override { if (this->data.empty()) { cout << "no result" << endl; return; } auto it = min_element(this->data.begin(), this->data.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); cout << it->first << " " << it->second << endl; } }; template<class T> class IntTable : public Table<T> { public: IntTable(int capacity) : Table<T>(capacity) {} void min() const override { if (this->data.empty()) { cout << "no result" << endl; return; } auto it = min_element(this->data.begin(), this->data.end(), [](const auto& a, const auto& b) { return a.second < b.second || (a.second == b.second && a.first < b.first); }); cout << it->first << " " << it->second << endl; } }; template<class T> class CharTable : public Table<T> { public: CharTable(int capacity) : Table<T>(capacity) {} void min() const override { if (this->data.empty()) { cout << "no result" << endl; return; } auto it = min_element(this->data.begin(), this->data.end(), [](const auto& a, const auto& b) { return a.second < b.second || (a.second == b.second && a.first < b.first); }); cout << it->first << " " << it->second << endl; } }; int main() { const int SIZE = 10; int key, ty; string temp; DoubleTable<double> dT(SIZE); IntTable<int> iT(SIZE); CharTable<char> cT(SIZE); iT[2016] = 19; iT[999] = 27; while (cin >> ty) { switch (ty) { case 1: cin >> key; cin >> dT[key]; dT.min(); break; case 2: cin >> key; cin >> iT[key]; iT.min(); break; case 3: cin >> key; cin >> cT[key]; cT.min(); break; default: getline(cin, temp); } } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值