Wednesday 25 July 2012

algorithm Library

<algorithm>



In the following, b and e are input iterators, and d is an output iterator, unless otherwise specified. Parameters eq and lt are optional, and default to functions that take 2 arguments x and y and return x==y and x<y respectively, e.g. bool eq(x,y) {return x==y;}. x and y are objects of the type pointed to by the iterators. p is a pair of iterators. f is a function or function object as noted.
// Operations on ordinary objects
swap(x1, x2); // Swap values of 2 objects of the same type
min(x1, x2); // Smaller of x1 or x2, must be same type
max(x1, x2); // Larger of x1 or x2, must be same type

// Properties of sequences (input iterators)
equal(b, e, b2, eq); // true if [b,e)==[b2,...)
lexicographical_compare(b, e, b2, e2, lt); // true if [b,e)<[b2,e2)
i=min_element(b, e); // Points to smallest in [b,e)
i=max_element(b, e); // Points to largest
n=count(b, e, x); // Number of occurrences of x in [b,e)
n=count_if(b, e, f); // Number of f(x) true in [b,e)

// Searching, i points to found item or end (e) if not found
i=find(b, e, x); // Find first x in [b,e)
i=find_if(b, e, f); // Find first x where f(x) is true
i=search(b, e, b2, e2, eq);// Find first [b2,e2) in [b,e) (forward)
i=find_end(b, e, b2, e2, eq); // Find last [b2,e2) in [b,e) (forward)
i=search_n(b, e, n, x, eq);// Find n copies of x in [b,e) (forward)
p=mismatch(b, e, b2, eq); // Find first *p.first in [b,e) != *p.second in [b2,.) (forward)
i=adjacent_find(b, e, eq); // Find first of 2 equal elements (forward)

// Modifying elements
i=copy(b, e, d); // Copy [b,e) to [d,i)
fill(d, i, x); // Set all in [d,i) to x (forward)
i=fill_n(d, n, x); // Set n elements in [d,i) to x
generate(d, i, f); // Set [d,i) to f() (e.g. rand) (forward)
i=generate_n(d, n, f); // Set n elements in [d,i) to f()
f=for_each(b, e, f); // Call f(x) for each x in [b,e)
i=transform(b, e, d, f); // For x in [b,e), put f(x) in [d,i)
i=transform(b, e, b2, d, f); // For x in [b,e), y in [b2,.), put f(x,y) in [d,i)
replace(b, e, x, y) // Replace x with y in [b,e)
replace_if(b, e, f, y); // Replace with y in [b,e) where f(x) is true
i=replace_copy(b, e, d, x, y); // Copy [b,e) to [d,i) replacing x with y
i=replace_copy_if(b, e, d, f, y); // Copy replacing with y where f(x) is true

// Rearranging sequence elements
sort(b, e, lt); // Sort [b,e) by < (random)
stable_sort(b, e, lt); // Sort slower, maintaining order of equal elements (random)
partial_sort(b, m, e, lt); // Sort faster but leave [m,e) unsorted (random)
nth_element(b, m, e, lt); // Sort fastest but only *m in proper place (random)
iter_swap(b, e); // swap(*b, *e) (forward)
i=swap_ranges(b, e, b2); // swap [b,e) with [b2,i) (forward)
i=partition(b, e, f); // Moves f(x) true to front, [i,e) is f(x) false (bidirectional)
i=stable_partition(b, e, f); // Maintains order within each partition
i=remove(b, e, x); // Move all x to end in [i,e) (forward)
i=remove_if(b, e, f); // Move f(x) true to front in [b,i) (forward)
i=remove_copy(b, e, d, x); // Copy elements matching x to [d,i)
i=remove_copy_if(b, e, d, f); // Copy elements x if f(x) is false to [d,i)
replace(b, e, x1, x2); // Replace x1 with x2 in [b,e)
i=replace_copy(b, e, d, x1, x2); // Copy [b,e) to [d,i) replacing x1 with x2
reverse(b, e); // Reverse element order in [b,e) (bidirectional)
i=reverse_copy(b, e, d); // Copy [b,e) to [d,i) reversing the order (b,e bidirectional)
rotate(b, m, e); // Move [b,m) behind [m,e) (forward)
i=rotate_copy(b, m, e, d); // Rotate into [d,i)
random_shuffle(b, e, f); // Random permutation, f() defaults to rand()
next_permutation(b, e, lt);// Next greater sequence, true if successful (bidirectional)
prev_permutation(b, e, lt);// Previous permutation, true if successful (bidirectional)

// Operations on sorted sequences
i=unique(b, e, eq); // Move unique list to [b,i), extras at end
i=unique_copy(b, e, d, eq); // Copy one of each in [b,d) to [d,i)
i=binary_search(b, e, x, lt); // Find i in [b,e) (forward)
i=lower_bound(b, e, x, lt); // Find first x in [b,e) or where to insert it (forward)
i=upper_bound(b, e, x, lt); // Find 1 past last x in [b,e) or where to insert it (forward)
p=equal_range(b, e, x, lt); // p.first = lower bound, p.second = upper bound (forward)
includes(b, e, b2, e2, lt); // true if [b,e) is a subset of [b2,e2)
i=merge(b, e, b2, e2, d, lt); // Merge [b,e) and [b2,e2) to [d,i)
inplace_merge(b, m, e, lt); // Merge [b,m) and [m,e) to [b,e) (bidirectional)
i=set_union(b, e, b2, e2, d, lt); // [d,i) = unique elements in either [b,e) or [b2,e2)
i=set_intersection(b, e, b2, e2, d, lt); // [d,i) = unique elements in both
i=set_difference(b, e, b2, e2, d, lt); // [d,i) = unique elements in [b,e) but not [b2,e2)
i=set_symmetric_difference(b, e, b2, e2, d, lt); // [d,i) = elements in one but not both
Algorithms never change the size of a container. When copying, the destination must be large enough to hold the result. int a[5]={3,1,4,1,6};
vector b(5);
copy(a, a+5, v.begin()); // Copy a to v
remove(a, a+5, 1); // {3,4,6,1,1}, returns a+3
sort(a, a+4); // {1,3,4,6,1}

No comments:

Post a Comment