Guest User

Untitled

a guest
Jun 3rd, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <iterator>
  7.  
  8. class WordsShifter {
  9.   const std::vector<std::string> words;
  10. public:
  11.   explicit WordsShifter(std::vector<std::string> words) : words(std::move(words)) { }
  12.  
  13.   std::vector<std::string> getShiftedBy(size_t shiftBy) {
  14.     if (this->words.empty()) {
  15.       return std::vector<std::string>{ };
  16.     }
  17.  
  18.     std::vector<std::string> shiftedWords{ this->words.size() };
  19.  
  20.     shiftBy %= this->words.size();
  21.  
  22.     std::rotate_copy(
  23.         this->words.rbegin(),
  24.         this->words.rbegin() + shiftBy,
  25.         this->words.rend(),
  26.         shiftedWords.rbegin());
  27.  
  28.     return shiftedWords;
  29.   }
  30. };
  31.  
  32. void printVectorToConsole(std::vector<std::string> shiftedWords);
  33.  
  34. std::vector<std::string> parseWordsFromConsole();
  35.  
  36. int main() {
  37.   WordsShifter words(parseWordsFromConsole());
  38.  
  39.   size_t shiftBy;
  40.   std::cin >> shiftBy;
  41.  
  42.   printVectorToConsole(words.getShiftedBy(shiftBy));
  43.  
  44.   return 0;
  45. }
  46.  
  47. void printVectorToConsole(std::vector<std::string> shiftedWords) {
  48.   std::move(shiftedWords.begin(), shiftedWords.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
  49. }
  50.  
  51. std::vector<std::string> parseWordsFromConsole() {
  52.   std::string line;
  53.   std::getline(std::cin, line);
  54.  
  55.   std::istringstream iss(line);
  56.  
  57.   std::vector<std::string> words{
  58.       std::istream_iterator<std::string>{ iss },
  59.       std::istream_iterator<std::string>{ }};
  60.  
  61.   return words;
  62. }
Add Comment
Please, Sign In to add comment