A
alan
Hello world,
I currently have implemented a sparse array needed by a class as a
map<int, boost::shared_ptr<my_type> >.
However, one of the derived classes needs to periodically "tighten"
the sparse array (i.e. make it non-sparse). For example:
a[0] = 1
a[4] = 2
a[42] = 54
=>
a[0] = 1
a[1] = 2
a[2] = 54
I'm currently somewhat puzzled at how I would rearrange the keys. One
possible solution I thought would be:
/*where inv is the member containing the map*/
int lim = inv.size();
map<int, boost::shared_ptr<my_type> >::iterator it = inv.begin();
for( int i = 0; i < lim; ++i, ++it){
inv = it->second;
}
/*delete everything after the last*/
inv.erase(it, inv.end());
However, this does create some new objects each time I need to
tighten; granted that my current application uses only smart pointers,
it's not a big deal, but suppose that a future map with similar
requirements is needed, where the value is no longer a pointer but a
reasonably costly object? Is there a better way of implementing this?
I currently have implemented a sparse array needed by a class as a
map<int, boost::shared_ptr<my_type> >.
However, one of the derived classes needs to periodically "tighten"
the sparse array (i.e. make it non-sparse). For example:
a[0] = 1
a[4] = 2
a[42] = 54
=>
a[0] = 1
a[1] = 2
a[2] = 54
I'm currently somewhat puzzled at how I would rearrange the keys. One
possible solution I thought would be:
/*where inv is the member containing the map*/
int lim = inv.size();
map<int, boost::shared_ptr<my_type> >::iterator it = inv.begin();
for( int i = 0; i < lim; ++i, ++it){
inv = it->second;
}
/*delete everything after the last*/
inv.erase(it, inv.end());
However, this does create some new objects each time I need to
tighten; granted that my current application uses only smart pointers,
it's not a big deal, but suppose that a future map with similar
requirements is needed, where the value is no longer a pointer but a
reasonably costly object? Is there a better way of implementing this?