with basic knowledge of multiset and vectors in c++ I solved the following problem. How can I improve my code and also handle any input errors?
Problem statement:
There is a collection of input strings and a collection of query strings. For each query string, print how many times it occurs in the list of input strings.
Input Format The first line contains an integer n, the input strings Each of the next n lines contains a string . The next line contains q, the size of . query strings Each of the q next lines contains a string .
My code:
#include<iostream> #include<vector> #include<algorithm> #include<set> #include<string> int main() { std::multiset<std::string> strings; std::vector<std::string> queries; size_t n,q;//size of the set, no of queries if (std::cin >> n){} else return EXIT_FAILURE; std::cin.ignore(1000, '\n'); for (size_t i = 0; i < n; i++) { std::string input; getline(std::cin, input); strings.insert(input); } if (std::cin >> q) {} else return EXIT_FAILURE; std::cin.ignore(100, '\n'); for (size_t i = 0; i < q; i++) { std::string input; getline(std::cin, input); queries.push_back(input); } //calling the function for (auto temp : queries) { if (strings.find(temp) != strings.end())//if the element exist int the set std::cout << strings.count(temp) << "\n"; else std::cout << "0" << "\n"; } return 0; }