本文整理汇总了C++中graph::traverse方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::traverse方法的具体用法?C++ graph::traverse怎么用?C++ graph::traverse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph
的用法示例。
在下文中一共展示了graph::traverse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: prompt
void prompt(void)
{
while(true)
{
std::string city_name, hop;
std::cout << "City name: "; std::getline(std::cin, city_name);
std::cout << "Hop: "; std::getline(std::cin, hop);
bstnode *n = bst->get_node_by_key(city_name);
if(n == NULL)
std::cout << "Error: City name '" << city_name << "' not found." << std::endl;
else
{
int city_code = n->element->value;
g.traverse(city_code, atoi(hop.c_str()));
}
}
}
示例2: load
int load(const int argc, const char *argv[])
{
/***************************
* CHECK IF IS FILE EXISTS *
***************************/
if(argc < 2)
{
fprintf(stdout, "Warning: Resource file not loaded.\nUsage: %s [path_of_file]\n", argv[0]);
return 1;
}
parser fp;
if(fp.open(argv[1]) != true)
{
fprintf(stdout, "Error: File '%s' not found.\n", argv[1]);
return 2;
}
clock_t begin, end;
double elapsed_secs = 0;
/***************************
* READ FILE *
***************************/
std::cout << "Step 1 of 2: Read a file, add to the vertex of the graph, add to the BST (together) . . . ";
begin = clock();
fp.parse(
[&] (const std::string &line)
{
std::string arr[5];
fp.divide_by_delimiter(arr, line, 5, '\t');
/*
arr[0] = StateAbb
arr[1] = PlaceName
arr[2] = longitude
arr[3] = latitude
arr[4] = DistancetoCapitalCity_km
*/
std::string city_name = arr[1];
double longitude, latitude;
try
{
longitude = stod(arr[2]);
latitude = stod(arr[3]);
}
catch(const std::invalid_argument &e)
{
return; /* Invalid line, so we should not add this line */
}
/* Add to graph */
int key = g.add_vertex(graphelement(arr[1]));
if(key == -1) /* Duplicate city name */
{
char *temp = new char[5]();
for(int i = 2; ; ++i) /* suffix start with #2 */
{
std::snprintf(temp, 5, "%d", i);
if((key = g.add_vertex(graphelement(std::string(city_name + " #" + temp)))) != -1)
{
city_name = city_name + " #" + temp;
break;
}
}
delete temp;
}
/* Add to BST */
bstelement *element = new bstelement(city_name, key, longitude, latitude);
bst->add(element);
}
);
end = clock();
std::cout << "Finished (" << elapsed_secs(begin, end) << "sec)" << std::endl;
//bst->printall();
std::cout << "Step 2 of 2: Add the edges to each vertex . . . ";
begin = clock();
double lat1, lon1, lat2, lon2;
bst->traverse_level_order(
[&] (bstnode *node)
{
bst->binary_search_tree::traverse_level_order(
[&] (bstnode *n)
{
if(10000 >= calc_distance(node->element->latitude,
node->element->longitude,
n->element->latitude,
n->element->longitude))
{
if(node->element->latitude < n->element->latitude
&& node->element->longitude < n->element->longitude)
{
bool is_connected = false;
g.traverse(n->element->value,
[&] (graph<graphelement>::node* edgn)
//.........这里部分代码省略.........