| ID | CS001099 | Creation date | September 16, 2008 |
| Platform | S60 3rd Edition, FP2 | Tested on devices | Nokia 6220 Classic |
| Category | Open C/C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): vector, sort() |
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.
The following libraries are required:
LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib
#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;
}
The STL sort function is used to sort vectors that contain structs and classes and the sorted contents are displayed on the screen.
No related wiki articles found