Algorithm (C++)

Algorithm (C++)

In the C++ programming language, algorithm is a function template that can work with (theoretically) any container containing values of any type. However, this is not always true because algorithms may have restrictions on the container/iterator category or even have requirements on the value type.

The C++ standard provides some standard algorithms collected in the standard header. All algorithms are in the std namespace.

A few of the most commonly used algorithms are:
* void copy(ForwardIterator source_begin, ForwardIterator source_end, ForwardIterator destination_begin)
* void fill(ForwardIterator destination_begin, ForwardIterator destination_end, T value)
* ForwardIterator find(ForwardIterator begin, ForwardIterator end, T search_object) (returns an iterator the found object or end if the object isn't found)
* T max(T a, T b) returns the greater of the two arguments
* ForwardIterator max_element(ForwardIterator begin, ForwardIterator end) finds the maximum element of a range
* T min(T a, T b) returns the smaller of the two arguments
* ForwardIterator min_element(ForwardIterator begin, ForwardIterator end) finds the minimum element of a range

Usage

#include #include #include using namespace std; int main() { vector v1(3, 42); // Create an array of three integers, all equal to 42. vector v2(5, 0); // Create an array of five integers, all zero. copy(v1.begin(), v1.end(), v2.begin()); // Copy all three elements from v1 to v2. // Now v2 contains { 42, 42, 42, 0, 0 }. fill(v1.begin(), v1.end(), 100); // Replace every element of v1 with "100". // Now v1 is { 100, 100, 100 }. if (find(v1.begin(), v1.end(), 0) = v1.end()) { // find() ends up at the last element of v1 since no results found // thus find() = vi.end() cout << "v1 doesn't contain a 0." << endl; // This will be the case. } else { cout << "v1 contains a 0." << endl; // This won't be the case since v1 is all 100s. } cout << "The bigger of 12.9 and 56.7 is " << max(12.9, 56.7) << "." << endl; // etc. return 0; }

External links

* [http://wwwasd.web.cern.ch/wwwasd/lhc++/RW/stdlibcr/classref.htm Standard C++ 1.2 Class Reference]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • algorithm — UK US /ˈælgərɪðəm/ noun [C] IT ► a set of mathematical instructions that must be followed in a fixed order, and that, especially if given to a computer, will help to calculate an answer to a mathematical problem: a computer/mathematical/search… …   Financial and business terms

  • algorithm — n. a precise rule (or set of rules) specifying how to solve some problem; a set of procedures guaranteed to find the solution to a problem. Syn: algorithmic rule, algorithmic program [WordNet 1.5 +PJC] …   The Collaborative International Dictionary of English

  • algorithm — [al′gə rith΄əm] n. [altered (after ARITHMETIC) < ALGORISM] 1. Math. a) any systematic method of solving a certain kind of problem b) the repetitive calculations used in finding the greatest common divisor of two numbers: called in full… …   English World dictionary

  • Algorithm —   [engl.], Algorithmus.   …   Universal-Lexikon

  • algorithm — (n.) 1690s, from Fr. algorithme, refashioned (under mistaken connection with Gk. arithmos number ) from O.Fr. algorisme the Arabic numeral system (13c.), from M.L. algorismus, a mangled transliteration of Arabic al Khwarizmi native of Khwarazm,… …   Etymology dictionary

  • algorithm — ► NOUN ▪ a process or set of rules used in calculations or other problem solving operations. DERIVATIVES algorithmic adjective. ORIGIN Latin algorismus, from an Arabic word meaning the man of Kw rizm , referring to a 9th century mathematician …   English terms dictionary

  • Algorithm — Flow chart of an algorithm (Euclid s algorithm) for calculating the greatest common divisor (g.c.d.) of two numbers a and b in locations named A and B. The algorithm proceeds by successive subtractions in two loops: IF the test B ≤ A yields yes… …   Wikipedia

  • algorithm — algorithmic, adj. /al geuh ridh euhm/, n. a set of rules for solving a problem in a finite number of steps, as for finding the greatest common divisor. [1890 95; var. of ALGORISM, by assoc. with Gk arithmós number. See ARITHMETIC] * * * Procedure …   Universalium

  • algorithm — A systematic process consisting of an ordered sequence of steps, each step depending on the outcome of the previous one. In clinical medicine, a step by step protocol for management of a health care problem …   Medical dictionary

  • algorithm — n. a sequential set of instructions used in calculations or problem solving. A reconstruction algorithm is a complex mathematical formula used by a computer to construct images from the data acquired by CT, MRI, or other scanners. A diagnostic… …   The new mediacal dictionary

  • algorithm — noun /ˈælɡəɹɪðm/ A precise step by step plan for a computational procedure that begins with an input value and yields an output value in a finite number of steps. Informally, an algorithm is any well defined computational procedure that takes… …   Wiktionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”