Difference between revisions of "Yurttas/OOL/Cplusplus/F/02/index page"
Jump to navigation
Jump to search
| (2 intermediate revisions by the same user not shown) | |||
| Line 10: | Line 10: | ||
bool operator() (int i,int j) { return (i<j);} | bool operator() (int i,int j) { return (i<j);} | ||
} myobject; | } myobject; | ||
| + | </pre> | ||
| + | |||
| + | |||
| + | AÇIKLAMA | ||
| + | <pre> | ||
int main () { | int main () { | ||
int myints[] = {32,71,12,45,26,80,53,33}; | int myints[] = {32,71,12,45,26,80,53,33}; | ||
| − | + | </pre> | |
| + | <pre>std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33</pre> test | ||
| + | |||
| + | <pre> | ||
// using default comparison (operator <): | // using default comparison (operator <): | ||
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 | std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 | ||
| Line 33: | Line 41: | ||
} | } | ||
</pre> | </pre> | ||
| + | == Heading == | ||
| + | |||
[[Yurttas/OOL/Cplusplus/index page | next >>]] | [[Yurttas/OOL/Cplusplus/index page | next >>]] | ||
Latest revision as of 16:59, 16 February 2018
// algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;
AÇIKLAMA
int main () {
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
test
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}