Online Book Reader

Home Category

Professional C__ - Marc Gregoire [241]

By Root 1338 0
of strings:

void removeEmptyStrings(vector& strings)

{

auto it = remove_if(strings.begin(), strings.end(),

[](const string& str){return str.empty();});

// Erase the removed elements.

strings.erase(it, strings.end());

}

int main()

{

vector myVector = {"", "one", "", "two", "three", "four"};

for (auto& str : myVector) cout << "\"" << str << "\" ";

cout << endl;

removeEmptyStrings(myVector);

for (auto& str : myVector) cout << "\"" << str << "\" ";

cout << endl;

return 0;

}

Code snippet from ModifyingAlgorithms\Remove.cpp

The output is as follows:

"" "one" "" "two" "three" "four"

"one" "two" "three" "four"

The remove_copy() and remove_copy_if() variations of remove() do not change the source range. Instead they copy all unremoved/retained elements to a different destination range. They are similar to copy(), in that the destination range must already be large enough to hold the new elements.

The remove() family of functions is stable in that it maintains the order of elements remaining in the container even while moving the retained elements toward the beginning.

unique

The unique() algorithm is a special case of remove() that removes all duplicate contiguous elements. The list container provides its own unique() method that implements the same semantics. You should generally use unique() on sorted sequences, but nothing prevents you from running it on unsorted sequences.

The basic form of unique() runs in place, but there is also a version of the algorithm called unique_copy() that copies its results to a new destination range.

Chapter 12 showed an example of the list::unique() algorithm, so we omit an example of the general form here.

reverse

The reverse() algorithm reverses the order of the elements in a range. The first element in the range is swapped with the last, the second with the second-to-last, and so on.

The basic form of reverse() runs in place and requires two arguments: a start and end iterator for the range. There is also a version of the algorithm called reverse_copy() that copies its results to a new destination range and requires three arguments: a start and end iterator for the source range and a start iterator for the destination range.

Other Modifying Algorithms

The STL includes several other modifying algorithms, including iter_swap(), swap_ranges(), fill(), fill_n(), generate_n(), rotate(), rotate_copy(), next_permutation(), and prev_permutation(). They are used less frequently, so no examples are given for them. However, if you understand how to use the algorithms explained in this section, you should have no problem using these other algorithms. Consult the section “Modifying Algorithms” in Chapter 11 to get a list of all available modifying algorithms with a brief description.

Sorting Algorithms

The STL provides several variations of sorting algorithms. A “sorting algorithm” will reorder the contents of a container such that an ordering is maintained between sequential elements of the collection. Thus, it applies only to sequential collections. Sorting is not relevant to associative containers because they already maintain elements in a sorted order. Sorting is not relevant to the unordered associative containers either because they have no concept of ordering. Some containers, such as list and forward_list provide their own sorting methods because these can be implemented more efficiently internally than a general sort mechanism. Consequently, the general sorting algorithms are most useful for vectors, deques, and arrays.

Basic Sorting and Merging

The sort() function sorts a range of elements in O(N log N) time in the general case. Following the application of sort() to a range, the elements in the range are in nondecreasing order (lowest to highest), according to operator<. If you don’t like that order, you can specify a different comparison callback such as greater.

A variant of sort(), called stable_sort(), maintains the relative order of equal elements in the range. However, because it needs to maintain relative order of equal elements

Return Main Page Previous Page Next Page

®Online Book Reader