mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-23 09:42:28 +08:00
feat: Add C++ code for the graph bfs and dfs (#401)
* Add C++ code for the graph bfs and dfs * Add C++ code for the graph bfs and dfs
This commit is contained in:
34
codes/cpp/include/Vertex.hpp
Normal file
34
codes/cpp/include/Vertex.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2023-03-02
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/* 顶点类 */
|
||||
struct Vertex {
|
||||
int val;
|
||||
Vertex(int x) : val(x) {}
|
||||
};
|
||||
|
||||
/* 输入值列表 vals ,返回顶点列表 vets */
|
||||
vector<Vertex*> valsToVets(vector<int> vals) {
|
||||
vector<Vertex*> vets;
|
||||
for (int val : vals) {
|
||||
vets.push_back(new Vertex(val));
|
||||
}
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* 输入顶点列表 vets ,返回值列表 vals */
|
||||
vector<int> vetsToVals(vector<Vertex*> vets) {
|
||||
vector<int> vals;
|
||||
for (Vertex *vet : vets) {
|
||||
vals.push_back(vet->val);
|
||||
}
|
||||
return vals;
|
||||
}
|
Reference in New Issue
Block a user