c++ - Moving from C array to std::map <int, val> operator `-` gotcha -
i had c style array of values. needed map memory economy (not allocate @ once , keep allocate needed)... can made set or in futher optimization vector. got on 1 painfull gotcha: val * v; auto val_index = v - val_collection used give item id... such code not compile. in std::vector case?
std::distance can give distance beginning of container (or other sequence):
std::vector<val>::iterator v = whatever(); size_t val_index = std::distance(val_collection.begin(), v); for random-access containers (including vector, not map), use - if like:
size_t val_index = v - val_collection.begin();
Comments
Post a Comment