开发者

sort vectors in map

开发者 https://www.devze.com 2023-03-10 13:21 出处:网络
HOW to sort vectors inside a map based on the size of th开发者_Go百科e vectors? example: map<int, vector<int> >

HOW to sort vectors inside a map based on the size of th开发者_Go百科e vectors?

example:

map<int, vector<int> > 

sort based on the size of the vector in order to remove some elements later within the less size.

1,2,3,4

2,5

6,7,8

after sort and delete ...

1,2,3,4

6,7,8

5

I hope this clarify the intended need.

Thanks


A map is an ordered container on which the order predicate applies to the key.

For example you can have a std::map<int, std::vector<int>, std::less<int> >

Here your key is not the vector, hence you cannot do what you are looking for with your map.

Here maybe you want a std::map<std::vector<int>, int, some_struct> where some_struct is a functor that defines a strict order relationship on your vectors.


You can do it provided the size of the vector doesn't change:

map <int, vector<int> > amap;
vector <int> v;
v.push_back( 42 );
amap.insert( make_pair( v.size(), v ));

If the size of the vector does change, you would have to remove the old entry and re-insert.

0

精彩评论

暂无评论...
验证码 换一张
取 消