从最后一个往回走一步是谁
运行下面这段程序:
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
struct DNode {
string name;
DNode* next = nullptr;
DNode* prev = nullptr;
};
pair<DNode*, DNode*> dbuild(const vector<string>& names) {
DNode* head = nullptr;
DNode* tail = nullptr;
for (const string& x : names) {
DNode* nd = new DNode{x};
if (head == nullptr) {
head = nd;
} else {
tail->next = nd;
nd->prev = tail;
}
tail = nd;
}
return {head, tail};
}
int main() {
auto [head, tail] = dbuild({"阿岚", "小满", "阿泰", "南风"});
cout << tail->prev->name << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论