从函数返回向量以打印内容
Returning a vector from a function in order to print the contents
我的程序有一个名为"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)
相关文章:
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 如何在 c++ 中打印存档中的对象向量
- 如何在C++中打印对象向量的内容
- 从函数返回向量以打印内容
- 为什么 c++ 不能打印向量 B[0]?
- 打印出对象向量中的每个元素C++
- 有没有办法搜索向量的元素,<String>然后检查它是否包含特定的字符,如果它确实打印了它
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员
- 如何在C 中打印字符串向量的向量
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- 无法打印字符向量C++向量
- Windbg C 如何打印向量内容
- circle类和班级成员的打印向量发行
- 如何在递归函数中打印向量
- 重载<<运算符打印向量的前n个值(不是整个向量)
- 打印向量 - 空输出
- 打印向量元素错误消息
- 在C 中打印向量阵列
- 如何填充和打印向量集的向量
- 我如何访问和打印向量的向量中的单个元素