+ 1

How to filter data in vector of structure

Hi I have a structure which has 7 integers and 2 strings as data fields. I have few objects of this structure stored into vector. How to sort the data? Sorting should be done on 1 value of integer and need to discard other elements. For example, A,B,C,D,E,F,G are headers of data and 1,2,3,4,5,5-1 1,2,3,4,6,5,2 1,2,3,6,4,5,3 If G is my sorting header and filter criteria is A,B,C then output should have only one row 1,2,3,6,4,5,3 Why ? Because A,B,C is same for all three rows and out of these rows, row having 3 is max If G is my sorting header and criteria is A,B,C,D then output should have two rows 1,2,3,4,6,5,2 (max from first two rows as both these have same A,B,C,D) 1,2,3,6,4,5,3 (as this is unique and not same values of A,B,C,D with previous rows) Any suggestion will be helpful.

12th Oct 2022, 6:38 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
16 odpowiedzi
+ 2
Why should the output be only one row? is that filtering rather than sorting?
12th Oct 2022, 6:43 AM
Ipang
+ 2
Yea, You can consider it as filtering...
12th Oct 2022, 6:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Ketan, I didn't get the second case where G is the sorting header and output was 1, 2, 3 ,4, 6, 5, 2 Tell me why that is ...
12th Oct 2022, 6:48 AM
Ipang
+ 2
Oh my bad.... Output for second case should be two rows 1,2,3,4,6,5,2 (max from first two rows as both these have same A,B,C,D) 1,2,3,6,4,5,3 (as this is unique and not same values of A,B,C,D with previous rows) I have updated the question also.... Thanks for catching this
12th Oct 2022, 6:52 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Hi Bob_Li , thanks but my req is to filter data as correctly pointed by Ipang My sample code is also doing filter..let me modify question title as well to avoid confusion
15th Oct 2022, 4:26 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Yup... i was just focusing on remove algo but it works on single object means one row If it is possible to have comparision between more rows, it would have been better
15th Oct 2022, 5:17 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
ok, I was just thinking of more generalized solutions for similar sorting situations. That's why I did not use map, just to keep it as simple as possible. You're right, there is also filtering required. i don't even know how to start with that...😅 Learned a lot about sorting, though. 😁 c++ doesn't really have convenient solutions for working on tabular data like Python's Pandas. Even simple sorting is very complicated.
15th Oct 2022, 5:11 PM
Bob_Li
Bob_Li - avatar
+ 1
It would be like coding an entire dataframe functionality. 😁 Filter, sort, add, delete, compute..
15th Oct 2022, 5:29 PM
Bob_Li
Bob_Li - avatar
+ 1
Ketan Lalcheta I added a template function in my code for filtering based on lambda expression. also a template for bool contains. These functionalities are useful for data manipulation. Familiarity with lambda expressions are required, though.. (lambda functions are pretty awesome.) I am learning a lot from you thread.
16th Oct 2022, 12:01 AM
Bob_Li
Bob_Li - avatar
0
what is your struct definition? are the numbers discrete or in a collection? It is hard to suggest a strategy without more information.
13th Oct 2022, 11:57 PM
Bob_Li
Bob_Li - avatar
0
All fields of struct can have any numbers and they are not constrained with each other It can be duplicate or unique for all indvidual fields But entire row would never be same i.e. alll values of a to g will not be totally same for any two rows
14th Oct 2022, 4:25 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Below is my trial on same: https://www.sololearn.com/compiler-playground/cVU9tJYvYljO/#cpp Can I have some idea about this implementation in terms of performance? Feel free to share your views and any performance improvement is most welcome.
14th Oct 2022, 11:32 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
your link is broken in the app.
14th Oct 2022, 12:12 PM
Bob_Li
Bob_Li - avatar
14th Oct 2022, 1:12 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
If you overload the bool operators, std::sort could be made to work on custom struct. https://code.sololearn.com/cRh04y6j9WiT/?ref=app
15th Oct 2022, 2:10 PM
Bob_Li
Bob_Li - avatar
0
It is shorter and simpler In Python Pandas, for comparison. While Python runs slower than c++, writing it is way easier and faster. https://code.sololearn.com/ccDhkJUJOHr1/?ref=app
16th Oct 2022, 8:17 AM
Bob_Li
Bob_Li - avatar