You Are Here:

Community: Wiki

This page was last modified on 14 October 2008, at 12:12.

CS001098 - Sorting predefined types using STL sort

From Forum Nokia Wiki



ID CS001098 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, list, sort(), list.sort()

Overview

This code snippet shows how to use the C++ Standard Template Library (STL) sort function with predefined data types. 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.

Some STL containers cannot use regular the sort function and they provide specialized versions of sort as a member function. The list container is one of them and it also has an optional default value to compare elements.

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;
 
 
bool exampleCompare(string& s1, string& s2)
{
// compare string lengths
int len1 = s1.length();
int len2 = s2.length();
 
return len1 < len2;
 
}
 
int main()
{
//-- Sorting predefined data types --
 
// 1) int array
int intArray[] = { 3, 1, 2 };
int arrayElements = sizeof(intArray) / sizeof(intArray[0]);
 
sort(intArray, intArray + arrayElements);
for (int i=0; i<arrayElements; ++i)
cout << intArray[i] << ' '; //1 2 3
 
// 2) int vector
vector<int> intVector;
intVector.push_back(3);
intVector.push_back(1);
intVector.push_back(2);
int vectorElements = intVector.size();
 
sort(intVector.begin(), intVector.end());
for (int i=0; i<vectorElements; ++i)
cout << intVector[i] << ' '; //1 2 3
 
 
// 3) string list
list<string> stringList;
list<string>::iterator stringIterator;
 
stringList.push_back ("second");
stringList.push_back ("first");
stringList.push_back ("third");
 
stringList.sort();
for (stringIterator = stringList.begin();
stringIterator != stringList.end();
++stringIterator)
cout << *stringIterator << ' '; //first second third
 
//use function exampleCompare to sort list again
stringList.sort(exampleCompare);
 
for (stringIterator = stringList.begin();
stringIterator != stringList.end();
++stringIterator)
cout << *stringIterator << ' '; //first third second
 
return 0;
}

Postconditions

The STL sort function is used to sort arrays, vectors, and lists and the sorted values are displayed on the screen.

See also

CS001099 - Sorting class and struct types using STL sort

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fGuaranaUIE2dE44ocumentationX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ