c++ - standard and efficient map between objects -
i working on clustering problem have called distance matrix. distance matrix like:
- the number of nodes(g) n (dynamic)
- this matrix symmetric (dist[i,j]==dist[j,i])
- g1,g2,.... object (they contain strings , integers , may more..)
- i want able reach value simple way dist[4][3] or more clear way dist(g1,g5) (here g1 , g5 may kind of pointer or reference)
- many std algorithm applied on distance matrix min, max, accumulate ..etc
- preferably not mandatory, not use boost or other 3rd party libraries
what best standard way declare matrix.
you can create 2 dimensional vector so
std::vector<std::vector<float> > table(n, std::vector<float>(n));
don`t forget initialize this, reserves memory n members, not need reallocate members adding more. , not fragment memory. can access members so
table[1][2] = 2.01;
it not uses copy constructors time because vector index operator returns reference member; pretty efficient if n not need change.
Comments
Post a Comment