练习10.1:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {vector<int>vi;int val;vi.push_back(45);vi.push_back(45);vi.push_back(45);vi.push_back(45);for (int i = 1; i <= 45; i++) {vi.push_back(i);}val = 45;cout << count(vi.begin(), vi.end(), val) << endl;return 0;
}
测试结果:
文件输入的代码如下:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>using namespace std;int main(int argc, char **argv) {ifstream in(argv[1]);if (!in) {cout << "打开输入文件失败!" << endl;exit(1);}int val;vector<int>vi;while (in >> val)vi.push_back(val);cin >> val;cout << count(vi.begin(), vi.end(), val) << endl;system("pause");return 0;
}
练习10.2:
#include <iostream>
#include <list>
#include <string>
#include <algorithm>using namespace std;int main() {list<string>ls;ls.push_back("ftghbjk");ls.push_back("fgvbhjnk");ls.push_back("asd");ls.push_back("asd");ls.push_back("asd");ls.push_back("asd");cout << count(ls.begin(), ls.end(), "asd");return 0;
}
文件输入的代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>using namespace std;int main(int argc, char **argv) {ifstream in(argv[1]);if (!in) {cout << "打开输入文件失败!" << endl;exit(1);}list<string>ls;string word;while (in >> word)ls.push_back(word);cin >> word;cout << count(ls.begin(), ls.end(), word) << endl;system("pause");return 0;
}