На самом деле я не понимаю, в чем ваша проблема. Я запускаю ваш код и получаю вершины по индексу как обычно. Вот код:
#include <iostream>
#include <list>
namespace ED
{
typedef int NodeId;
}
struct vertex
{
ED::NodeId v;
ED::NodeId m;
ED::NodeId f;
ED::NodeId r;
int scanned;
void setM(vertex neigh);
void setF(vertex neigh);
void setR(vertex neigh);
void setScanned(int scan);
} vrtex;
vertex getVertex(ED::NodeId x, std::list<vertex> vertecies)
{
for (std::list<vertex>::iterator it = vertecies.begin(); it!=vertecies.end(); ++it)
{
if(it->v == x)
{
printf("Element found!\n");
return (vertex) * it;
}
}
printf("No element with id %i found.", x);
return vertex(); // If there is no suche node id in vertecies
}
int main()
{
std::list<vertex> vertices =
{
{0,0,0,0,0},
{1,0,0,0,0},
{2,0,0,0,228},
{3,0,0,0,0}
};
printf("%d\n", vertices.size());
vertex e = getVertex(2, vertices);
printf("Element.scanned = %d\n", e.scanned);
vertex vert = {0,2,0,0,0};
e = getVertex(vert.m, vertices);
printf("Element.scanned = %d\n", e.scanned);
}
И вывод:
[DDRDmakar@localhost New Folder]$ g++ test.cpp && ./a.out
4
Element found!
Element.scanned = 228
Element found!
Element.scanned = 228
Возможно, я использую неправильные типы или аргументы, но это работает ...
Но это будет лучшеиспользовать для каждого цикла. Он работает так же, но гораздо безопаснее:
vertex getVertex(ED::NodeId x, std::list<vertex> vertecies)
{
for (const auto &e : vertecies)
{
if(e.v == x)
{
printf("Element found!\n");
return e;
}
}
printf("No element with id %i found.", x);
return vertex(); // If there is no suche node id in vertecies
}