Changing Items

The reverse function acts on a range in a container and reverses the order of the items; this means that the iterators must be writeable. The copy and copy_n functions copy every item from one range to another in a forward direction; for copy, the input range is given by two input iterators, and for copy_n, the range is an input iterator and a count of items. The copy_backward function will copy the items, starting at the end of the range, so that the output range will have the items in the same order as the original. This means that the output iterator will indicate the end of the range to copy to. You can also copy items only if they satisfy some condition specified by a predicate.

  • The reverse_copy function will create a copy in the reverse order to the input range; in effect, the function iterates backward through the original and copies items to the output range forward.
  • In spite of the name, the move and move_backward functions are semantically equivalent to the copy and copy_backward functions. Thus, in the following, the original container will have the same values after the operation:
        vector<int> planck{ 6,6,2,6,0,7,0,0,4,0 }; 
vector<int> result(4); // we want 4 items
auto it1 = planck.begin(); // get the first position
it1 += 2; // move forward 2 places
auto it2 = it1 + 4; // move 4 items
move(it1, it2, result.begin()); // {2,6,0,7}
  • This code will copy four items from the first container to the second container, starting at the item in the third position.
  • The remove_copy and remove_copy_if functions iterate through the source range and copy items other than those with the specified value.
        vector<int> planck{ 6,6,2,6,0,7,0,0,4,0 }; 
vector<int> result;
remove_copy(planck.begin(), planck.end(),
back_inserter(result), 6);
  • Here, the planck object is left the same as before and the result object will contain {2,0,7,0,0,4,0}. The remove_copy_if function behaves similarly, but is given a predicate rather than an actual value.
  • The remove and remove_if functions don't quite do what their names suggest. These functions act on a single range and iterate looking for a specific value (remove), or pass each item to a predicate that will indicate if the item should be removed (remove_if). When an item is removed, the items later in the container are shifted forward, but the container remains the same size, which means that the items at the end remain as they were. The reason the remove functions behave like this is because they only know about reading and writing items through iterators (which is generic for all containers). To erase an item, the function will need to have access to the erase method of the container, and the remove functions only have access to iterators.
  • If you want to remove the items at the end, then you must resize the container accordingly. Typically, this means calling a suitable erase method on the container, and this is made possible because the remove method returns an iterator to the new end position:
        vector<int> planck { 6,6,2,6,0,7,0,0,4,0 }; 
auto new_end = remove(planck.begin(), planck.end(), 6);
// {2,0,7,0,0,4,0,0,4,0}
planck.erase(new_end, planck.end()); // {2,0,7,0,0,4,0}
  • The replace and replace_if functions iterate through a single range, and if the value is a specified value (replace) or returns true from a predicate (replace_if), then the item is replaced with a specified new value. There are also two functions, replace_copy and replace_copy_if, that leave the original alone and make the change to another range (similar to remove_copy and remove_copy_if functions).
  • The rotate functions treat the range as if the end is joined to the beginning, and so you can shift items forward so that when an item falls off the end it gets put in the first position. If you want to move every item forward four places, you can do this:
        vector<int> planck{ 6,6,2,6,0,7,0,0,4,0 }; 
auto it = planck.begin();
it += 4;
rotate(planck.begin(), it, planck.end());
  • The result of this rotation is {0,7,0,0,4,0,6,6,2,6}. The rotate_copy function does the same thing, but, rather than affecting the original container, it copies the items into another container.
  • The unique function acts on a range and "removes" (in the manner explained previously) the items that are duplicates of adjacent items, and you can provide a predicate for the function to call to test if two items are the same. This function only checks adjacent items, so a duplicate later in the container will remain. If you want to remove all duplicates, then you should sort the container first, so that similar items are adjacent.
  • The unique_copy function will copy items from one range to another only if they are unique, so one way to remove duplicates is to use this function on a temporary container and then assign the original to the temporary:
        vector<int> planck{ 6,6,2,6,0,7,0,0,4,0 }; 
vector<int> temp;
unique_copy(planck.begin(), planck.end(), back_inserter(temp));
planck.assign(temp.begin(), temp.end());
  • After this code, the planck container will have {6,2,6,0,7,0,4,0}.
  • Finally, the iter_swap will swap the items indicated by two iterators, and the swap_ranges function swaps the items in one range to the other range (the second range is indicated by one iterator and it is assumed to refer to a range of the same size as the first).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset