Sorting a vector of custom objects

0 votes
How does one sort a vector that contains custom (i.e. user defined) objects?

Standard STL algorithm sorting should probably be used, along with a predicate (a function or a function object) that operates on one of the fields (as a key for sorting) in the custom object.

Is this the right path for me?
May 31 in C++ by Nicholas
• 2,360 points
24 views

1 answer to this question.

0 votes

A simple example using std::sort

struct MyStruct
{
    int key;
    std::string stringValue;

    MyStruct(int k, const std::string& s) : key(k), stringValue(s) {}
};

struct less_than_key
{
    inline bool operator() (const MyStruct& struct1, const MyStruct& struct2)
    {
        return (struct1.key < struct2.key);
    }
};

std::vector < MyStruct > vec;

vec.push_back(MyStruct(4, "test"));
vec.push_back(MyStruct(3, "a"));
vec.push_back(MyStruct(2, "is"));
vec.push_back(MyStruct(1, "this"));

std::sort(vec.begin(), vec.end(), less_than_key());
answered Jun 1 by Damon
• 3,380 points

Related Questions In C++

0 votes
0 answers

Sorting Characters Of A C++ String

Is there a built-in method for sorting characters in a string, or do I have to construct my own? for instance: string word = "dabc"; I would want to ...READ MORE

1 hour ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
1 answer

Lower and Upper Bound in case of Decreasing/Non-ascending vector

Both std::lower bound and std::upper bound must have an increasing (non-decreasing) order as their objective. By giving a comparator as the 4th parameter of the functions, you may modify the meaning of "growing." To work with descending vectors, use std::greater. #include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; int main() { ...READ MORE

answered Jun 14 in C++ by Damon
• 3,380 points
5 views
0 votes
1 answer

Vector of Vectors to create matrix

Before accessing any elements, you must first set the vector of vectors to the right size.  You may do it this way: // assumes using std::vector for brevity vector<vector<int>> matrix(RR, ...READ MORE

answered Jun 16 in C++ by Damon
• 3,380 points
4 views
0 votes
0 answers

In C++ abs( *a - *b) does not return absolute value of negative number

#include <stdio.h> #include <cmath> void update(int *a,int *b) { ...READ MORE

5 days ago in C++ by Nicholas
• 2,360 points
6 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 3,380 points
11 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
15 views
0 votes
1 answer

lower_bound == upper_bound

Lower bound: the initial greater-or-equal element. Upper bound: ...READ MORE

answered 6 days ago in C++ by Damon
• 3,380 points
20 views
0 votes
1 answer

Comparison of C++ STL collections and C# collections

This is what I know Array - C ...READ MORE

answered Jun 9 in C# by rajiv
• 1,620 points
11 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15 in C++ by Damon
• 3,380 points
14 views
0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

answered Jun 2 in C++ by Damon
• 3,380 points
13 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP