简体   繁体   English

需要一些帮助来颠倒单词顺序

[英]Need some help reversing word orders

Hey guys I've got to take a string of words and re-order them to be backwards in C++.嘿,伙计们,我必须使用一串单词并将它们重新排序为在 C++ 中倒退。 Not spelling them backwards but reverse the sentence essentially.不是倒着拼写,而是本质上颠倒了句子。 My issue is I have to use cin.get() and I keep getting an error that I cant seem to figure out.我的问题是我必须使用 cin.get() 并且我不断收到一个我似乎无法弄清楚的错误。 The error is "no instance of overloaded function matches the argument list" it says this under the "get" after cin.get Here is my code错误是“没有重载 function 的实例与参数列表匹配”它在 cin.get 之后的“get”下说这是我的代码

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str;
  cin.get(str, 1000);

  int n = str.size();
  int i, j = n;

  for (i = n - 1; i >= 0; i--) {
    if (str[i] == ' ') {
      for (int k = i + 1; k < j; k++) {
        cout << str[k];
      }
      cout << " ";
      j = i;
    }
  }

  for (int k = i + 1; k < j; k++) {
    cout << str[k];
  }

  return 0;
}

Because you included <string> and are using std::string s, we'll use std::getline() .因为您包含<string>并且正在使用std::string ,所以我们将使用std::getline()

We are also going to use a std::vector to store the std::string s.我们还将使用std::vector来存储std::string Vectors in C++ are arrays that can grow when needed. C++ 中的载体是 arrays,可以在需要时生长。 They can be used just like arrays [and then some], but their dynamic nature makes them a lot more flexible.它们可以像 arrays [还有一些] 一样使用,但是它们的动态特性使它们更加灵活。 If for whatever (homework) reason you cannot use vectors, this can still be accomplished with a C-array of std::string s.如果出于任何(家庭作业)原因您不能使用向量,这仍然可以通过std::string的 C 数组来完成。 You'll just have to choose a big enough capacity to handle your possible use-cases.您只需要选择足够大的容量来处理您可能的用例。 And keep count of how many words you actually have.并数一数你实际上有多少字。

My code has a few variations that you can change around by commenting/uncommenting.我的代码有一些变体,您可以通过注释/取消注释来更改它们。 There are two methods of acquiring words.有两种获取单词的方法。 One has the words being entered individually, and the other has all the words on one line.一个是单独输入的单词,另一个是一行中的所有单词。 I also have two ways of reversing the word order;我也有两种颠倒词序的方法; so four total different ways for the code to play out.所以总共有四种不同的方式让代码发挥作用。

#include <algorithm>  // std::reverse()
#include <iostream>
#include <sstream>  // std::istringstream
#include <string>
#include <vector>

std::vector<std::string> get_single_words() {
  std::vector<std::string> words;
  for (std::string tmp; std::getline(std::cin, tmp);) {
    if (tmp != "") {
      words.push_back(tmp);
    } else {
      break;  // Get out of loop on empty string (pressing Enter, only)
    }
  }

  return words;
}

std::vector<std::string> get_line_of_words() {
  std::string line;
  std::getline(std::cin, line);
  std::istringstream sin(line);
  std::vector<std::string> words;

  for (std::string tmp; sin >> tmp;) {
    words.push_back(tmp);
  }

  return words;
}

int main() {
  /*
   * Two methods for obtaining words, uncomment only one at a time
   */
  // auto words = get_single_words();
  auto words = get_line_of_words();

  /*
   * Two methods for printing reversed list, uncomment only one at a time
   */
  // Print in reverse by moving backwards through vector
  //
  for (auto riter = words.rbegin(); riter != words.rend(); ++riter) {
    std::cout << *riter << ' ';
  }
  std::cout << '\n';

  // OR by reversing vector and printing normally
  // (first is better for just printing)
  //
  // std::reverse(words.begin(), words.end());
  // for (const auto& word : words) {
  //   std::cout << word << ' ';
  // }
  // std::cout << '\n';

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM