0% found this document useful (0 votes)
40 views3 pages

Bool Isodd (Int I) (Return ( (I%2) 1) )

This document summarizes several common algorithms from the C++ Standard Template Library (STL) including sort, count, search, binary_search, rotate, reverse, erase_if, and unique. It provides examples of how to use each algorithm and describes what each one does at a high level.

Uploaded by

luiguimoya123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Bool Isodd (Int I) (Return ( (I%2) 1) )

This document summarizes several common algorithms from the C++ Standard Template Library (STL) including sort, count, search, binary_search, rotate, reverse, erase_if, and unique. It provides examples of how to use each algorithm and describes what each one does at a high level.

Uploaded by

luiguimoya123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Librería Algorithm

Librería Algorithm

SORT
sort(primero,ultimo)
sort(primero,ultimo,funcion)

bool myfunction (int i,int j) { return (i>j); }


v.push_back(123);
v.push_back(7);
v.push_back(843);
v.push_back(32);
v.push_back(40);

sort(v.begin(),v.end()); 7-32-40-12- 843


sort(vi.begin(),vi.end(),myfunction); 843-123-40-32-7

COUNT/ COUNT_IF

bool IsOdd (int i) { return ((i%2)==1); }


count(primero,ultimo,elemento)
int n = count (myvector.begin(), myvector.end(), 20);
cout << "20 aparece " << n << " veces.\n";
count_if(primero,ultimo,funcion)

SEARCH
6 // search algorithm example
7 #include <iostream> // std::cout
8 #include <algorithm> // std::search
9 #include <vector> // std::vector
10
11 bool mypredicate (int i, int j) {
12 return (i==j);
13 }
14
15 int main () {
16 std::vector<int> haystack;
17
18 // set some values: haystack: 10 20 30 40 50 60 70 80 90
19 for (int i=1; i<10; i++) haystack.push_back(i*10);
20
21 // using default comparison:
22 int needle1[] = {40,50,60,70};
23 std::vector<int>::iterator it;
24 it = std::search (haystack.begin(), haystack.end(), needle1,
25 needle1+4);
26
27 if (it!=haystack.end())
28 std::cout << "needle1 found at position " << (it-haystack.begin())
29 << '\n';
30 else
31 std::cout << "needle1 not found\n";
32
33 // using predicate comparison:
34 int needle2[] = {20,30,50};
35 it = std::search (haystack.begin(), haystack.end(), needle2,
36 needle2+3, mypredicate);

1
Librería Algorithm

if (it!=haystack.end())
std::cout << "needle2 found at position " << (it-haystack.begin())
<< '\n';
else
std::cout << "needle2 not found\n";

return 0;
}

BINARY_SEARCH
binary_search (v.begin(), v.end(), 3)
binary_search (v.begin(), v.end(), 6, myfunction)

DEVUELVE UN BOOL

ROTATE
// rotate algorithm example
1
#include <iostream> // std::cout
2
#include <algorithm> // std::rotate
3
#include <vector> // std::vector
4
5
int main () {
6
std::vector<int> myvector;
7
8
// set some values:
9
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
10
11
std::rotate(myvector.begin(),myvector.begin()+3,myvector.end());
12
// 4 5 6 7 8 9 1 2 3
13
// print out content:
14
std::cout << "myvector contains:";
15
for (std::vector<int>::iterator it=myvector.begin();
16
it!=myvector.end(); ++it)
17
std::cout << ' ' << *it;
18
std::cout << '\n';
19
20
return 0;
21
}

REVERSE
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1

ERASE_IF

vec.erase(remove_if (vec.begin(),vec.end(),IsOdd),vec.end());
1 2 3 4 5 6 7 8 9
2 4 6 8 ? ? ? ? ?
2 4 6 8 5 6 7 8 9

2
Librería Algorithm

UNIQUE
Borra a todos los elementos repetidos que estan lado a lado, pore so es necesario ordenar primero

vec.sort(vec.begin(),vec.end())
vec.erase(unique(vec.begin(),vec.end(),IsOdd),vec.end());

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy