Created
February 9, 2023 19:25
-
-
Save JamesBremner/7ca9b6c721dc1a36c7c9275fd18436ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <fstream> | |
#include <sstream> | |
#include <iostream> | |
#include <vector> | |
/// @brief A graph edge | |
class edge | |
{ | |
public: | |
std::string v1; | |
std::string v2; | |
std::string attr; | |
edge( | |
const std::string &s1, | |
const std::string &s2, | |
const std::string &sattr) | |
: v1(s1), | |
v2(s2), | |
attr(sattr) | |
{ | |
} | |
void display() | |
{ | |
std::cout << v1 << " | "<< v2 <<" | "<< attr << "\n"; | |
} | |
}; | |
/// @brief define graph type | |
typedef std::vector<edge> graph_t; | |
/// @brief split line into space delimited tokens | |
/// @param line | |
/// @return vector of tokens | |
std::vector<std::string> tokenize(const std::string &line) | |
{ | |
std::vector<std::string> ret; | |
std::stringstream sst(line); | |
std::string a; | |
while (getline(sst, a, ' ')) | |
ret.push_back(a); | |
return ret; | |
} | |
/// @brief Output text listing graph edges | |
/// @param[in] g | |
void display(graph_t& g) | |
{ | |
std::cout << "v1 | v2 | atribute\n|---|---|---|\n"; | |
for( edge& e : g ) | |
{ | |
e.display(); | |
} | |
} | |
/// @brief read graph from input file | |
/// @param[in/out] g graph to add edges to | |
/// @param fname input file path | |
void read( | |
graph_t& g, | |
const std::string &fname) | |
{ | |
std::ifstream ifs(fname); | |
if (!ifs.is_open()) | |
throw std::runtime_error("no input file"); | |
std::string line; | |
while (getline(ifs, line)) | |
{ | |
auto vtoken = tokenize(line); | |
g.push_back( edge( vtoken[0],vtoken[1],"bf")); | |
for( int k = 2; k<vtoken.size(); k++ ) | |
g.push_back( edge( vtoken[0],vtoken[k],"ff")); | |
} | |
} | |
main() | |
{ | |
// construct graph | |
graph_t g; | |
// read edges from file | |
read(g,"input.txt"); | |
// display edges | |
display(g); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment