SGI

Table of Contents: the Standard Template Library

  1. Introduction to the STL
  2. How to use the documentation
  3. Containers
    1. Concepts
      1. General concepts
        1. Container
        2. Forward Container
        3. Reversible Container
        4. Random Access Container
      2. Sequences
        1. Sequence
        2. Front Insertion Sequence
        3. Back Insertion Sequence
      3. Associative Containers
        1. Associative Container
        2. Simple Associative Container
        3. Pair Associative Container
        4. Sorted Associative Container
        5. Hashed Associative Container
        6. HashFunction
        7. Unique Associative Container
        8. Multiple Associative Container
        9. Unique Sorted Associative Container
        10. Multiple Sorted Associative Container
        11. Unique Hashed Associative Container
        12. Multiple Hashed Associative Container
    2. Container classes
      1. Sequences
        1. vector
        2. deque
        3. list
        4. slist
        5. bit_vector
      2. Associative Containers
        1. set
        2. map
        3. multiset
        4. multimap
        5. hash_set
        6. hash_map
        7. hash_multiset
        8. hash_multimap
        9. hash
      3. String package
        1. Character Traits
        2. char_traits
        3. basic_string
      4. rope
      5. Container adaptors
        1. stack
        2. queue
        3. priority_queue
      6. bitset
  4. Iterators
    1. Introduction
    2. Concepts
      1. Trivial Iterator
      2. Input Iterator
      3. Output Iterator
      4. Forward Iterator
      5. Bidirectional Iterator
      6. Random Access Iterator
    3. Iterator Tags
      1. Introduction
      2. iterator_traits
      3. iterator_category
      4. distance_type
      5. value_type
      6. Iterator tag classes
        1. input_iterator_tag
        2. output_iterator_tag
        3. forward_iterator_tag
        4. bidirectional_iterator_tag
        5. random_access_iterator_tag
      7. Iterator base classes
        1. input_iterator
        2. output_iterator
        3. forward_iterator
        4. bidirectional_iterator
        5. random_access_iterator
    4. Iterator functions
      1. distance
      2. advance
    5. Iterator classes
      1. istream_iteratoroot swap SGI

        swap

        Category: algorithms Component type: function

        Prototype

        template <class Assignable> 
        void swap(Assignable& a, Assignable& b);
        

        Description

        Assigns the contents of a to b and the contents of b to a. This is used as a primitive operation by many other algorithms.

        Definition

        Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

        Requirements on types

        Preconditions

        None.

        Complexity

        Amortized constant time. [1] [2]

        Example

        int x = 1;
        int y = 2;
        assert(x == 1 && y == 2);
        swap(x, y);
        assert(x == 2 && y == 1);
        

        Notes

        [1] The time required to swap two objects of type T will obviously depend on the type; "constant time" does not mean that performance will be the same for an 8-bit char as for a 128-bit complex<double>.

        [2] This implementation of swap makes one call to a copy constructor and two calls to an assignment operator; roughly, then, it should be expected to take about the same amount of time as three assignments. In many cases, however, it is possible to write a specialized version of swap that is far more efficient. Consider, for example, swapping two vector<double>s each of which has N elements. The unspecialized version requires 3*N assignments of double, but a specialized version requires only nine pointer assignments. This is important because swap is used as a primitive operation in many other STL algorithms, and because containers of containers (list<vector<char> >, for example) are very common. The STL includes specialized versions of swap for all container classes. User-defined types should also provide specialized versions of swap whenever it is possible to write one that is more efficient than the general version.

        See also

        iter_swap, swap_ranges
        [Silicon Surf] [STL Home]
        Copyright © 1999 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation

        ./usr/share/doc/stl-manual/html/swap_ranges.html0000644000000000000000000001040310447635250020615 0ustar rootroot swap_ranges SGI

        swap_ranges

        Category: algorithms Component type: function

        Prototype

        template <class ForwardIterator1, class ForwardIterator2>
        ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
                                     ForwardIterator2 first2);
        

        Description

        Swap_ranges swaps each of the elements in the range [first1, last1) with the corresponding element in the range [first2, first2 + (last1 - first1)). That is, for each integer n such that 0 <= n < (last1 - first1), it swaps *(first1 + n) and *(first2 + n). The return value is first2 + (last1 - first1).

        Definition

        Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

        Requirements on types

        ForwardIterator1 and ForwardIterator2 must both be models of Forward Iterator. The value types of ForwardIterator1 and ForwardIterator2 must be convertible to each other.

        Preconditions

        • [first1, last1) is a valid range.
        • [first2, first2 + (last1 - first1)) is a valid range.
        • The two ranges [first1, last1) and [first2, first2 + (last1 - first1)) do not overlap.

        Complexity

        Linear. Exactly last1 - first1 swaps are performed.

        Example

        vector<int> V1, V2;
        V1.push_back(1);
        V1.push_back(2);
        V2.push_back(3);
        V2.push_back(4);
        
        assert(V1[0] == 1 && V1[1] == 2 && V2[0] == 3 && V2[1] == 4);
        swap_ranges(V1.begin(), V1.end(), V2.begin());
        assert(V1[0] == 3 && V1[1] == 4 && V2[0] == 1 && V2[1] == 2);
        

        Notes

        See also

        swap, iter_swap.
        [Silicon Surf] [STL Home]
        Copyright © 1999 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation