I was trying to solve problem B1 of Helvetic Coding Contest 2019 online mirror (teams allowed, unrated). I got WA and TLE for a several time.Then I went to standings and found a line from a submission: int f=lower_bound(dg,dg+b,make_pair(a[i],10000000))-dg;
But,I couldn't understand it,,why I have to subtract dg at last??? Now, I am badly in need of help.Someone please explain the above statement.
lower bound returns first iterator of the value greater then or equal you are searching for. And by seeing your code it can be assumed it is used for finding the position of the value of make_pair(a[i], 10000000) in a container named dg. If you subtract dg from the returned iterator of lower_bound function then you will get the position. because dg is similar to dg.begin() here. both of them points first value of the container. [ extra tip: subtracting two iterator or pointer gives how many chunks of memory address are there between(not depends on your datatype) ]. you can know more about lower bound in here
btw, next time please attach the code where you have taken a part from it. Cause now I don't know whether the dg container is sorted or not. or which container is it. Thanks, have a good day. :D
dg is an array(not
std::array
butstd::pair<int,int> dg[10]
) but not a container. array shall not support begin() method, anyway it's not an obeject at all. and a container's name is also not equal to its begin() method. But since c++11 you can usestd::begin()
andstd::end()
whether arr is an array or a container.coyorkdow i didn't know without a container we can store pairs. It would be nice of you if you give some reference about this. Thanks. :)
In c++ containers library is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like vector, queues, lists and stacks. reference from https://en.cppreference.com/w/cpp/container. So
std::pair<int,int>
is not a container butstd::vector<std::pair<int,int>>
is a container that storesstd::pair<int,int>
. Meanwhilestd::pair<int,int>[100]
andnew std::pair<int,int>[100]
can also stores pairs but neither are container. When memory are allocated, each object will call its default constructor.lower_bound returns an iterator(or an pointer if parameters are not iterators) pointing to the first element in the range [first, last) that is not less than value you gived. So if you want to get the index of an element then you should transform it into an integer. Both random iterator and or pointer support subtract operation and will return the numbers of differences of elements between two interators or pointers(i.e. an iterator pointing to a[5] subtract to an interator pointing to a[2] will returns 3). Since dg is an array, array name points to the first element of the array. So after you do the subtract you get the index of the first element in the range from dg[0] to dg[b] which is not less then make_pair(a[i],10000000).
Pointer is a kind of iterator
make_pair(a[i],10000000)--->What kind of argument is it???Why I have to write it in the third slot of the funtion???
It is the "value you gived".
std::make_pair()
will return a pair which contains two parameters. If a[i] is an int value then you will get astd::pair<int,int>
object. You find the first element that is not less than this pair.What if you learn your programming language a bit before solving problems?
More like "learn to look up how unfamiliar features work" instead of spamming questions.
Maybe google can help?