sort vectors in map - c++

HOW to sort vectors inside a map based on the size of the 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.

Related

Initilize a vector of Pair<int,int> with emplace_back

I want to initialize a vector of integer pairs while specifying its size (I have to use this structure). I tried:
vector<pair<int, int>> container;
container.emplace_back(size);
And:
container.emplace_back(size, make_pair(0, 0));
But I keep having this error:
error: no matching function for call to 'std::pair<int, int>::pair(long long unsigned int&, std::pair<int, int>)'
Is there any solution or different approach?
Thank you!
emplace_back forwards its parameters to the elements constructor. std::pair<int,int> has no constructor that takes a size and a pair, hence the error. To emplace an element:
std::vector<std::pair<int, int>> container;
container.emplace_back(0,0);
However, if you want to construct a vector of certain size upfront, you need not emplace elements, because they are already there:
std::vector<std::pair<int, int>> container(size);
container[42] = make_pair(1,2); // 42 < size !
I'm guessing what you really want is something like:
vector<pair<int, int>> container(size);
This will initialize the vector constainer with size number of default-constructed elements.

Map C++ Sort and Insert elements by value

How to create a custom comparator to insert and sort elements by value in a map in C++? Generally in a map, elements are sorted by key. I want to soet by value.
This is not possible in C++ to sort map based on its values due to its internal implementation.
Map sorts elements only based on its key.
But there is a way you can achieve what you want.(Both needs additional space though.)
1) If you only want all the values to be sorted & not needing their mapping. You can put all the keys in a vector and then sort the vector.
2) And suppose you want that mapping too. Then you can create a vector of pair<> and then define a comparator to sort based on second value of pair.
bool sortBySecond(const pair<int, int> &a, const pair<int, int> &b){
return (a.second < b.second);
}
Inside main:
vector<pair<int, int> > vect;
sort(vect.begin(), vect.end(), sortBySecond);
The above vector will have pair sorted on the basis of your values of map in ascending order.

C++ Is there a way to initialize unordered_map using contents of vector as keys and custom values?

Let's say I have a vector
vector<int> vect;
vect.push_back(2);
vect.push_back(3);
Now I want an unordered_map with those 2 and 3 as keys and 0 as default values for all those keys.
Is there a way to do it?
I tried doing
unordered_map<int, int> myMap(vect.begin(), vect.end());
hoping it would initialize with what's in the vector as keys and default int value.
However, it couldn't.
I mean, I can always just iterate the vector and manually insert pairs.
I just want to know if I can do it as a one liner during declaration.
Actually a simple one liner is enough, but not on declaration:
vector<int> vect = { 2, 3, 4};
unordered_map<int,int> map;
transform(vect.begin(), vect.end(), inserter(map, map.end()), [] (int v) { return make_pair(v, 0); });

c++, how to have a list of more then 1 element

I want to have a list that holds an integer a string together. I know I need to use "Pair" somewhere but I don't know how?
How I'd "Insert" into the that list that contains pairs?
(i do not need to use maps, I do not want my list contents to be organized alphabetical order.)
std::pair<int, std::string> p1(1, "abc");
std::pair<int, std::string> p2(2, "cba");
std::list<std::pair<int, std::string> > myList;
myList.push_back(p1); // Insert first pair
myList.push_back(p2); // Insert second pair (at the end of the list)
Use push_back, push_front to add elements to the rear, front of the list.
You can also use C++11 features to create new pairs "in-place".
std::list<std::pair<int, std::string>> myList;
myList.push_back(std::make_pair(1, "abc"));
myList.push_back(std::make_pair(2, "def"));
// or
std::list<std::pair<int, std::string>> myList{{1, "abc"}, {2, "cde"}};

Get all stl vector elements greater than a value

I would like to know how can I find the list of a stl vector elements that have value verifying a certain condition. For example if I have a vector of int values
vector<int> V;
and I want to get all the elements that are greater than 5.
Thanks in advance.
You'd std::copy_if() if the values:
std::vector<int> target;
std::copy_if(v.begin(), v.end(), std::back_inserter(target),
std::bind(std::less<int>(), 5, _1));