1

I have a sorted(according to first.second element)vector pair of following type : vector<pair<pair<int,int>,int>>p;

My Comparator function to sort vector pair

bool sortbysec(pair<pair<int,int>,int>&a,pair<pair<int,int>,int>&b) { if(a.first.second==b.first.second)return a.first.first<b.first.first; else return a.first.second<b.first.second; } 

I want upper bound of p.first.secondelement on the list of p.first.firstelements.

For example -

 2 2 7 1 3 4 1 4 3 4 5 8 5 7 5 4 8 6 5 8 1 6 8 9 8 8 0 8 9 2 

In simple words, I want an upper bound of middle element of the vector pair.Searching the upper bound of that particular element should be done in list of first element of the vector pair.

For Example, upper bound of 3 in second row is 4 in the 4th row.

My question is : I dont know how to code this thing. I have tried this syntax(incorrect):

auto it=lower_bound(v.begin(),v.end(),make_pair(make_pair(2,2),7)); 

This is showing error. I hope you understood the question. Please anyone tell me a way how to code it properly so i get the desired results in c++ only.

18
  • I think you cant use all the search function because all of them are searching by key, and you need to search by value Commented May 27, 2020 at 16:49
  • Can you elaborate please @yaodav Commented May 27, 2020 at 16:57
  • 1
    How about std::tuple<int, int, int> (or std::array<int, 3>) instead of pair<pair<int,int>,int>? Commented May 27, 2020 at 16:59
  • 1
    Let me try to explain. I dont know c++ from memory. I know how to rtfm, I know how to write code, usually it has lots of errors, then I read the error message and fix the errors. To help you I would need to repeat this whole process that you already did. And to be plain honest I am too lazy to do that. On the other hand if you would include a minimal reproducible example and the error message the question would be much easier to answer. The only thing I know for sure is that you need to use the same comparater for lower_bound as the one you used for sorting, but atm I have honestly no clue what your error is Commented May 27, 2020 at 20:28
  • 2
    you should include the code and the error in the question! The problem is not the line you posted. There is no syntax error. The only problem in that line is that you should use the same comparator as for sorting. The actual problem is #define int long long int. This is undefined behavior. You are lucky that you got a compiler error and not worse. Don't ever do that. If you want to declare a variable of type long long int then write long long int x; because when you write int x; you declare a variable of type int not of type long long int (unless you break the language) Commented May 27, 2020 at 21:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.