Guest User

Untitled

a guest
Jun 3rd, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4. #include <iterator>
  5. #include <algorithm>
  6.  
  7. using Words = std::vector<std::string>;
  8.  
  9. Words getWords(std::istream& in);
  10.  
  11. class SentenceShifter {
  12. private:
  13.   Words words;
  14.  
  15.   void shift(size_t shiftCount) {
  16.     shiftCount %= this->words.size();
  17.     std::rotate(this->words.rbegin(), this->words.rbegin() + shiftCount, this->words.rend());
  18.   }
  19.  
  20. public:
  21.   SentenceShifter(Words words, size_t shiftCount) :
  22.       words(std::move(words)) {
  23.     shift(shiftCount);
  24.   }
  25.  
  26.   Words getShiftedSentence() {
  27.     return words;
  28.   }
  29. };
  30.  
  31. int main() {
  32.   std::istream& in = std::cin;
  33.   std::ostream& out = std::cout;
  34.  
  35.   Words words = getWords(in);
  36.   size_t shiftCount;
  37.   in >> shiftCount;
  38.   SentenceShifter sentenceShifter{ words, shiftCount };
  39.  
  40.   Words shifted = sentenceShifter.getShiftedSentence();
  41.  
  42.   for (auto const& word : shifted) {
  43.     out << word << std::endl;
  44.   }
  45.  
  46.   return 0;
  47. }
  48.  
  49. Words getWords(std::istream& in) {
  50.   std::string sentence;
  51.   std::getline(in, sentence);
  52.   std::istringstream iss{ sentence };
  53.   Words words{ std::istream_iterator<std::string>{ iss },
  54.                std::istream_iterator<std::string>{ }};
  55.   return words;
  56. }
Add Comment
Please, Sign In to add comment