以相反的顺序显示内容

Display the contents in reverse order

本文关键字:显示 顺序      更新时间:2023-10-16

下面的代码是由一位同事向我建议的。所以学分不是我的。我试图坐立不安地绕过这段代码,并尝试以相反的顺序打印出元素。到目前为止,元素是从开头单词 dog 开始打印出来的。但目标是以另一种方式打印它。从猫开始。所以从本质上讲,代码根据血统来回馈单词。例如,在这种情况下,我们从cag那里得到了猫,它是祖先,cag的祖先是cog。依此类推,直到我们从狗开始

#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;
}

这实际上非常简单!与其使用stack来推送然后弹出"找到"的字符串路径,不如使用vectorpush_back字符串;然后,您可以按任一顺序打印值!在此代码中,我已从您拥有的顺序切换到"其他"顺序:

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;
}

你甚至可以在这里选择一个选择!要以原始(="反向"(顺序打印,只需更改for循环:

for (size_t w = wordPath.size() - 1; w <= 0 ; --w) {
cout << wordPath[w] << " ";
}

您可以简单地使用.rbegin(), .rend()和reverse_iterator向后退dictionary,并在到达"cat"时使用标志开始打印。参见 std::vector,例如

#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';
}
}

示例使用/输出

如果我理解并且您只想以相反的顺序以"cat"开头打印,那么以下内容应该与您尝试执行的操作相匹配:

$ ./bin/reverse_cats
cat
fog
cog
bog
dog

输出关联的向量索引

如果要将向量索引与字符串一起输出,可以使用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';
}

示例使用/输出

$ ./bin/reverse_cats
4 cat
3 fog
2 cog
1 bog
0 dog