[BGL, Graph] add_vertex with specific ctor

Hello, I like the named parameter method to define graph properties, so all our properties are concentrated in a struct. But, I'd like add a vertex with specific properties are already defined: //======================= #include <iostream> #include <string> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> using namespace std; using namespace boost; struct People { People () { } People (string const& n, int a) : name(n), age(a) { } string name; int age; }; int main(int, char**) { typedef adjacency_list< vecS, vecS, bidirectionalS, People > Graph; typedef graph_traits< Graph >::vertex_descriptor vertex_descriptor; Graph g; vertex_descriptor v; People n ("toto", 26); v = add_vertex (g); // this use the default ctor of People, a add_vertex(g, n) g[v] = n; // will be more efficiently (avoid a People assignment) cout << g[v].name << " is " << g[v].age << " old." << endl; } //======================= -- Johan

On Nov 21, 2005, at 8:31 AM, Johan Oudinet wrote:
Hello,
I like the named parameter method to define graph properties, so all our properties are concentrated in a struct. But, I'd like add a vertex with specific properties are already defined: [snip] People n ("toto", 26); v = add_vertex (g); // this use the default ctor of People, a add_vertex(g, n) g[v] = n; // will be more efficiently (avoid a People assignment)
You should be able to write: v = add_vertex(n, g); Doug

On 11/21/05, Doug Gregor <dgregor@cs.indiana.edu> wrote:
On Nov 21, 2005, at 8:31 AM, Johan Oudinet wrote:
Hello,
I like the named parameter method to define graph properties, so all our properties are concentrated in a struct. But, I'd like add a vertex with specific properties are already defined: [snip] People n ("toto", 26); v = add_vertex (g); // this use the default ctor of People, a add_vertex(g, n) g[v] = n; // will be more efficiently (avoid a People assignment)
You should be able to write:
v = add_vertex(n, g);
Yes, thanks (I got it just after posting my question... sorry). Just if someone else find a way to use struct for properties without default ctor, there is add_vertex (np, g) and add_edge(u, v, ep, g). I found it in the BGL book. Regards, -- Johan
participants (2)
-
Doug Gregor
-
Johan Oudinet