|
发表于 2021-3-20 20:16:20
|
显示全部楼层
本楼为最佳答案
作业还是建议自己写
- #include <iostream>
- #include <vector>
- #include <string>
- #include <utility>
- #include <algorithm>
- const size_t nul = -1;
- size_t locate(const std::vector<std::pair<std::string, size_t>> &v, const std::string &s) {
- for(size_t i = 0; i < v.size(); ++i) {
- if(v[i].first == s) return i;
- }
- return nul;
- }
- const std::vector<std::pair<std::string, size_t>> get(std::istream &is) {
- std::vector<std::pair<std::string, size_t>> result;
- std::string s;
- while(1) {
- is >> s;
- if(s == "0") break;
- size_t i = locate(result, s);
- if(i == nul) result.push_back(std::pair<std::string, size_t>(s, 1));
- else ++result[i].second;
- }
- return result;
- }
- std::ostream &operator<<(std::ostream &os, const std::vector<std::pair<std::string, size_t>> &rhs) {
- for(const auto &i: rhs) {
- os << i.first << ": " << i.second << std::endl;
- }
- return os;
- }
- int main() {
- std::vector<std::pair<std::string, size_t>> result = get(std::cin);
- std::sort(result.begin(), result.end());
- std::cout << result << std::endl;
- return 0;
- }
复制代码 |
|