从函数返回向量以打印内容

Returning a vector from a function in order to print the contents

本文关键字:打印 向量 函数 返回      更新时间:2023-10-16

我的程序有一个名为"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;
}

有多个问题。

当你说"我试图在函数之外声明向量并在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)