fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <fstream>
  6. #include <iterator>
  7.  
  8. using namespace std;
  9.  
  10. vector<string> readStringsFromFile(const string& fileName);
  11. vector<string> readStrings(istream& file);
  12. vector<string> filterValues(const vector<string>& words);
  13. ostream &operator<<(ostream &out, const vector<string>& words);
  14.  
  15. int main()
  16. {
  17. vector<string> finishWords = readStrings(cin);
  18. cout << "to sa moje slowa:\n" << finishWords << endl;
  19. cout << "to sa moje odfiltorwane slowa:\n" << filterValues(finishWords) << endl;
  20. readStringsFromFile("niedziała.txt");
  21. return 0;
  22. }
  23.  
  24. //==========================================================================================
  25.  
  26. vector<string> readStringsFromFile(const string& fileName)
  27. {
  28. ifstream file(fileName, ios::in );
  29. if (file.good())
  30. {
  31. return readStrings(file);
  32. }
  33. cerr << "Plik nie istnieje" << endl;
  34. return vector<string>();
  35. }
  36. //==========================================================================================
  37. vector<string> readStrings(istream& file)
  38. {
  39. string line;
  40. vector<string> words;
  41. while(getline(file , line))
  42. {
  43. words.push_back(line);
  44. }
  45. return words;
  46. }
  47. //==========================================================================================
  48. bool isValidString(const string& s)
  49. {
  50. return !std::any_of(s.begin(), s.end(), [](auto ch) { return std::ispunct(ch)!=0; });
  51. }
  52. //==========================================================================================
  53. vector<string> filterValues(const vector<string>& words)
  54. {
  55. vector<string> result;
  56. result.reserve(words.size());
  57. std::copy_if(words.begin(), words.end(), std::back_inserter(result), isValidString);
  58. return result;
  59. }
  60. //==========================================================================================
  61. ostream &operator<<(ostream &out, const vector<string>& words)
  62. {
  63. std::ostream_iterator<string> out_it (out, "\n");
  64. std::copy(words.begin(), words.end(), out_it );
  65. return out;
  66. }
  67.  
  68. //==========================================================================================
  69.  
Success #stdin #stdout #stderr 0s 3468KB
stdin
tralalala
chlupie fala
#statek płynie
po głębinie!
stdout
to sa moje slowa:
tralalala
chlupie fala
#statek płynie
po głębinie!

to sa moje odfiltorwane slowa:
tralalala
chlupie fala

stderr
Plik nie istnieje