fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <unordered_set>
  4. #include <vector>
  5. #include <string>
  6. #include <iterator>
  7.  
  8. std::istringstream tokens_in(
  9. R"(00sdfdsf
  10. ahdadsg
  11. angel
  12. ksjflsjdf
  13. green
  14. green000
  15. carrot
  16. )"
  17. );
  18.  
  19. std::istringstream dict_in(
  20. R"(angel
  21. carrot
  22. green
  23. kitten
  24. zoo
  25. )"
  26. );
  27.  
  28. using dictionary_type = std::unordered_set<std::string>;
  29. dictionary_type read_dictionary(std::istream& is)
  30. {
  31. using iter_type = std::istream_iterator<std::string>;
  32. return dictionary_type(iter_type(is), iter_type());
  33. }
  34.  
  35. std::vector<std::string> filter(std::istream& token_stream, const dictionary_type& dictionary)
  36. {
  37. std::vector<std::string> filtered_tokens;
  38.  
  39. std::string token;
  40. while (token_stream >> token)
  41. if (dictionary.count(token))
  42. filtered_tokens.push_back(token);
  43.  
  44. return filtered_tokens;
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50. auto filtered = filter(tokens_in, read_dictionary(dict_in));
  51.  
  52. for (auto& token : filtered)
  53. std::cout << token << '\n';
  54. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
angel
green
carrot