首页 > 解决方案 > 从函数返回向量以打印内容

问题描述

我的程序有一个名为“WordLadder”的函数,它使用 BFS 来创建从字典中的一个单词到另一个单词的路径。我得到了一个打印路径中节点数的起始代码,但我想打印路径本身。目前,当它们进入队列时,我已将单词附加到向量中,但我无法将向量作为我的“WordLadder”函数的一部分返回,以便在主程序中打印它。

我只想让程序根据我选择的两个词打印一条路径,即“TOON - POON - POIN - POIE - PLIE - PLEE - PLEA”,如果字典中的起始词是“TOON”并且目标词是字典中的“PLEA”。

我试图在函数之外声明向量并用这段代码在 main 中打印它,但我没有成功。


void print(std::vector < int >
  const & transformation) {
  std::cout << "The vector elements are : ";

  for (int i = 0; i < transformation.size(); i++)
    std::cout << transformation.at(i) << ' ';
}

我试图在函数内部返回向量,但收到此错误

 error: no viable conversion
      from returned value of type
      'vector<std::__cxx11::string>' (aka
      'vector<basic_string<char> >') to
      function return type 'int'
          return transformation;

这是我的代码。任何帮助将不胜感激,因为我是 C++ 新手。


// To check if strings differ by exactly one character 
bool nextWord(string & a, string & b) {
  int count = 0; // counts how many differences there
  int n = a.length();

  // Iterator that loops through all characters and returns false if there is more than one different letter 
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      count++;
    }

    if (count > 1) {
      return false;
    }
  }
  return count == 1 ? true : false;
}

// A queue item to store the words
struct QItem {
  string word;
};

// Returns length of shortest chain to reach 'target' from 'start' using minimum number of adjacent moves. D is dictionary 
int wordLadder(string & start, string & target, set < string > & ew) {
  //Create vector to store path in a
  vector < string > transformation;

  // Create a queue for BFS and insert 'start' as source vertex 
  queue < QItem > Q;
  QItem item = {
    start
  };
  Q.push(item);

  // While queue is not empty 
  while (!Q.empty()) {

    // Take the front word 
    QItem curr = Q.front();
    transformation.push_back(Q.front().word);
    Q.pop();

    // Go through all words of dictionary 
    for (set < string > ::iterator it = ew.begin(); it != ew.end(); it++) {
      // Proccess the next word according to BFS
      string temp = * it;
      if (nextWord(curr.word, temp)) {

        // Add this word to queue from the dictionary
        item.word = temp;
        Q.push(item);

        // Pop from dictionary so that this word is not repeated
        ew.erase(temp);

        // If we reached target 
        if (temp == target) {
          return trasformation;
        }

      }
    }
  }

  return 0;
}

// Driver program 
int main() {
  string start;
  string target;

  // make dictionary 
  std::ifstream file("english-words.txt");
  set < string > ew;

  copy(istream_iterator < string > (file),
    istream_iterator < string > (),
    inserter(ew, ew.end()));

  cout << endl;
  cout << "Enter Start Word" << endl;
  cin >> start;
  cout << "Enter Target Word" << endl;
  cin >> target;

  cout << wordLadder(start, target, ew);

  return 0;
}

标签: c++stringfunctionvector

解决方案


有多个问题。

当你说“我试图在函数之外声明向量并在 main 中打印它”时,你是在正确的轨道上......

因此,更改wordLadder为通过引用获取向量。

int wordLadder(vector<string> &transformation, string & start, string & target, set < string > & ew)

然后将其声明main传递wordLadder

vector<string> t;
wordLadder(t, start, target, ew);
print(t);

您还必须更改print以采用正确类型的向量,即。string而不是 int

void print(std::vector < string > &transformation)

推荐阅读