×
Namespaces

Variants
Actions

Sorting class and struct types using STL sort in Open C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition, FP2
Article
Keywords: vector, sort()
Created: aknyman (03 Sep 2008)
Last edited: hamishwillee (14 Jun 2012)

Contents

Overview

This code snippet shows how to use C++ Standard Template Library (STL) sort function with user-defined new types using structs or classes. The sort function needs at least two parameters: start and end. These iterator parameters are used to sort the range of elements between them. An optional third parameter has the default less-than operator as a value to compare elements.

None of the comparison operators are defined in the user-defined classes by default, so the sort function is not able to compare two objects until the less-than operator is overloaded. Assignment between classes or structs also needs user-defined implementation if dynamic allocation is used with member variables.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <string>
 
using namespace std;
 
// SCORE STRUCT
struct Score
{
int val1;
int val2;
bool operator==(const Score& score) const;
bool operator<(const Score& score) const;
};
 
bool Score::operator==(const Score& score) const
{
return(score.val1 == val1 &&
score.val2 == val2);
}
 
bool Score::operator<(const Score& score) const
{
if(val2 < score.val2)
return true;
else if (val2 == score.val2 && val1 < score.val1)
return true;
else return false;
}
 
//user defined compare function that compares only val1 variables
bool exampleCompare( Score s1, Score s2 )
{
return s1.val1 < s2.val1;
};
 
 
// SCORE CLASS
class ScoreClass
{
public:
friend ostream& operator<<(ostream& output, const ScoreClass& c);
ScoreClass(string s, int v1, int v2);
bool operator==(const ScoreClass& score) const;
bool operator<(const ScoreClass& score) const;
string getStr() const;
private:
string str;
int val1;
int val2;
};
 
ostream& operator<<(ostream& output, const ScoreClass& c)
{
output << c.str << ' ' << c.val1 << ' ' << c.val2;
return output;
}
ScoreClass::ScoreClass(string s, int v1, int v2)
{
str = s; val1 = v1; val2 = v2;
}
 
bool ScoreClass::operator==(const ScoreClass& score) const
{
return(score.str == str &&
score.val1 == val1 &&
score.val2 == val2);
}
 
bool ScoreClass::operator<(const ScoreClass& score) const
{
if(val2 < score.val2)
return true;
else if (val2 == score.val2 && val1 < score.val1)
return true;
else return false;
}
string ScoreClass::getStr() const
{
return str;
}
 
 
//user defined compare function that compares string values
bool exampleCompare2(ScoreClass s1, ScoreClass s2)
{
return (s1.getStr() < s2.getStr());
}
 
 
int main()
{
int vectorElements = 0;
 
//-- Sorting struct types --
 
vector<Score> structVector;
Score first = {3, 2};
Score second = {1, 5};
Score third = {2, 3};
 
structVector.push_back(first);
structVector.push_back(second);
structVector.push_back(third);
 
vectorElements = structVector.size();
 
sort(structVector.begin(), structVector.end());
 
for (int i=0; i<vectorElements; ++i) //3 2|2 3|1 5|
{
cout << structVector[i].val1 << ' '
<< structVector[i].val2 << '|';
}
 
//use function exampleCompare to sort list again
sort(structVector.begin(), structVector.end(), exampleCompare);
 
for (int i=0; i<vectorElements; ++i) //1 5|2 3|3 2|
{
cout << structVector[i].val1 << ' '
<< structVector[i].val2 << '|';
}
 
//-- Sorting class types --
 
vector<ScoreClass> classVector;
classVector.push_back(ScoreClass((string)"111", 10, 20));
classVector.push_back(ScoreClass((string)"222", 60, 200));
classVector.push_back(ScoreClass((string)"333", 1, 1));
vectorElements = classVector.size();
 
sort(classVector.begin(), classVector.end()); //333 1 1|111 10 20|222 60 200|
 
for (int i=0; i<vectorElements; ++i)
{
cout << classVector[i] << '|';
}
 
//use function exampleCompare2 to sort list again
sort(classVector.begin(), classVector.end(),
exampleCompare2); //111 10 20|222 60 200|333 1 1|
 
for (int i=0; i<vectorElements; ++i)
{
cout << classVector[i] << '|';
}
 
return 0;
}

Postconditions

The STL sort function is used to sort vectors that contain structs and classes and the sorted contents are displayed on the screen.

See also

Sorting predefined types using STL sort in Open C++

This page was last modified on 14 June 2012, at 04:02.
147 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×