C++ 按相反顺序显示内容
下面的代码是一位飞越者向我建议的。所以信用卡不是我的。我试图绕过这段代码,并试图以相反的顺序打印出元素。到目前为止,这些元素都是从起始词dog开始打印出来的。但目标是以另一种方式打印。从猫开始。因此,基本上,代码可以追溯到基于祖先的单词。例如,在本例中,我们从作为祖先的cag获得cat,而cag的祖先是cog。以此类推,直到我们从狗开始C++ 按相反顺序显示内容,c++,C++,下面的代码是一位飞越者向我建议的。所以信用卡不是我的。我试图绕过这段代码,并试图以相反的顺序打印出元素。到目前为止,这些元素都是从起始词dog开始打印出来的。但目标是以另一种方式打印。从猫开始。因此,基本上,代码可以追溯到基于祖先的单词。例如,在本例中,我们从作为祖先的cag获得cat,而cag的祖先是cog。以此类推,直到我们从狗开始 #include <iostream> #include <string> #include <unordered_set>
#include <iostream>
#include <string>
#include <unordered_set>
#include <stack>
#include <vector>
using namespace std;
int main() {
vector<string> dictionary;
vector<pair<string, int>> words; //stores (word, predecessor)
string startWord = "dog";
string endWord = "cat";
unordered_set<string> seenWords;
dictionary.push_back("dog");
dictionary.push_back("bog");
dictionary.push_back("cog");
dictionary.push_back("fog");
dictionary.push_back("cat");
dictionary.push_back("bag");
dictionary.push_back("beg");
dictionary.push_back("bet");
dictionary.push_back("bat");
words.emplace_back(startWord, -1);
seenWords.insert(startWord);
bool found = false;
//Try all new words as reference words
for(int i = 0; i < words.size() && !found; ++i) {
//we look for words that we can generate from words[i]
cout << i << " " << words[i].first << ": ";
//try all the words from the dictionary
for (int j = 0; j < dictionary.size(); j++) {
string& candidate = dictionary[j];
//check if candidate can be generated from reference
//count the different characters
int differentCharacters = 0;
for (int pos = 0; pos < words[i].first.size(); ++pos)
{
if (candidate[pos] != words[i].first[pos])
++differentCharacters;
}
if (differentCharacters == 1 && seenWords.find(candidate) == seenWords.end()) {
//yes, we can generate this candidate from word[i] and we haven't seen the word before
cout << "(" << words.size() << ")" << candidate << " ";
words.emplace_back(candidate, i);
seenWords.insert(candidate);
if (candidate == endWord) {
found = true;
cout << "Found endword";
break;
}
}
}
cout << endl;
}
if (found) {
//traverse the word path from the end word back to the start word
int i = words.size() - 1;
stack<string> wordPath;
while (i != -1) {
//push the current word onto a stack
wordPath.push(words[i].first);
//go to the previous word
i = words[i].second;
}
//now retrieve the words from the stack and print them in reverse order
cout << "Word path:" << endl;
while (!wordPath.empty()) {
cout << wordPath.top() << " ";
wordPath.pop();
}
cout << endl;
}
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
向量字典;
向量字;//存储(字,前置)
字符串startWord=“dog”;
字符串endWord=“cat”;
无序的集合词;
字典。推回(“狗”);
字典。推回(“bog”);
字典。推回(“cog”);
字典。推回(“雾”);
字典。推回(“猫”);
字典。推回(“袋子”);
字典。推回(“beg”);
字典。推回(“下注”);
字典。推回(“bat”);
单词。向后放置(startWord,-1);
参见单词插入(startWord);
bool-found=false;
//尝试所有新单词作为参考词
对于(int i=0;i cout这实际上非常简单!与其使用堆栈
推送然后弹出“找到的”字符串路径,不如使用向量
和推回
字符串;然后您可以按任意顺序打印值!在这段代码中,我从您拥有的顺序切换到了“其他”顺序:
if (found) {
//traverse the word path from the end word back to the start word
int i = words.size() - 1;
/// stack<string> wordPath;
vector<string> wordPath;
while (i != -1) {
// push the current word into a vector ...
/// wordPath.push(words[i].first);
wordPath.push_back(words[i].first);
//go to the previous word
i = words[i].second;
}
// now retrieve the words from the vector and print them ...
cout << "Word path:" << endl;
/// while (!wordPath.empty()) {
/// cout << wordPath.top() << " ";
/// wordPath.pop();
/// }
///
for (size_t w = 0; w < wordPath.size(); ++w) {
string text = wordPath[w];
size_t index = 0;
for (index = 0; index < dictionary.size(); ++index) {
if (text == dictionary[index]) break;
}
cout << text << "[" << index << "] ";
}
///
cout << endl;
}
您只需使用.rbegin()、.rend()
和反向迭代器,就可以在字典
中后退,并在到达时使用标志开始打印。请参阅,例如
输出相关向量索引
如果要将向量索引与字符串一起输出,可以使用dictionary.rend()-it-1
获取从零开始的索引,例如
/* output in reverse order beginning with cat */
for (auto it = dictionary.rbegin(); it != dictionary.rend(); it++) {
if (*it == "cat")
prn = true;
if (prn)
std::cout << dictionary.rend() - it - 1 << " " << *it << '\n';
}
问题是什么?但目标是以另一种方式打印。再使用一个堆栈来反转wordPath
?我如何以相反的顺序显示当前元素?你能帮忙吗?我可以制作堆栈,但我应该在代码中的何处添加它,并包括元素的索引?@user3365922“问题”h使用堆栈时,您只能访问添加的最后一个元素(顶部);没有可用的[]std::stack
的操作符,设计上。使用限制较少的std::vector
在检索和显示内容时允许更大的灵活性。我们将单词的索引放入wordpath vector中。并将它们与最后一个按相反顺序打印它们的cout语句一起显示。我认为是这样的…可以e试试看?所以我在正确的地方做了修改。唯一的问题是索引是错误的。我得到了一个不同的首字母数字,它应该在索引0处。我还得到了核心dumped@Elchavo18哎呀!忘了我以前的编辑和代码修改吧(事实上,这在很多层面上都是错误的)查看循环的最新更改-在输出字符串时只需确定索引!这样,我就得到了输出的最后一行:cat[4]bat[8]bag[5]bog[1]dog[0]
-我觉得这些索引很正确。(字典中每个字符串的索引
)。我们如何才能添加这些索引?嗯……你失去了我对索引的“附加”功能?如果你向向量添加其他元素,那么同样的方法也会起作用(只要你想从“cat”
)开始)。如果你想要第二个std::vector
,你可以简单地。推回()
使用上面相同的逻辑以相反的顺序。因此,这些元素中的每个元素在向量中都有一个索引?如何在输出中的元素旁边显示它们?我想OP想要列出“找到的”连接,而不是原始的字典。哦,我想我明白了你的要求,索引是什么,是的,我将删除编辑。
#include <iostream>
#include <vector>
#include <string>
int main () {
bool prn = false;
std::vector<std::string> dictionary;
dictionary.push_back("dog");
dictionary.push_back("bog");
dictionary.push_back("cog");
dictionary.push_back("fog");
dictionary.push_back("cat");
dictionary.push_back("bag");
dictionary.push_back("beg");
dictionary.push_back("bet");
dictionary.push_back("bat");
/* output in reverse order beginning with cat */
for (auto it = dictionary.rbegin(); it != dictionary.rend(); it++) {
if (*it == "cat")
prn = true;
if (prn)
std::cout << *it << '\n';
}
}
$ ./bin/reverse_cats
cat
fog
cog
bog
dog
/* output in reverse order beginning with cat */
for (auto it = dictionary.rbegin(); it != dictionary.rend(); it++) {
if (*it == "cat")
prn = true;
if (prn)
std::cout << dictionary.rend() - it - 1 << " " << *it << '\n';
}
$ ./bin/reverse_cats
4 cat
3 fog
2 cog
1 bog
0 dog