Friday, January 24, 2014

stl container swap

stl container swap : Standard Template Library Tutorial,

Common Container Abilities and Operations

Common Container Abilities

This section covers the common abilities of STL container classes. Most of them are requirements that, in general, every STL container should meet. The three core abilities are as follows:
  1. All containers provide value rather than reference semantics. Containers copy elements internally when they are inserted rather than managing references to it. Thus, each element of an STL container must be able to be copied. If objects you want to store don't have a public copy constructor, or copying is not useful (for example, because it takes time or elements must be part of multiple containers), the container elements must be pointers or pointer objects that refer to these objects. 
  2. In general, all elements have an order. Thus, you can iterate one or many times over all elements in the same order. Each container type provides operations that return iterators to iterate over the elements. This is the key interface of the STL algorithms.
  3. In general, operations are not safe. The caller must ensure that the parameters of the operations meet the requirements. Violating these requirements (such as using an invalid index) results in undefined behavior. Usually the STL does not throw exceptions by itself. If user-defined operations called by the STL containers do throw, the behavior is different.

Common Container Operations

The following subsections explore some of these common operations.


Every container class provides a default constructor, a copy constructor, and a destructor. You can also initialize a container with elements of a given range. This constructor is provided to initialize the container with elements of another container, with an array, or from standard input. These constructors are member templates, so not only the container but also the type of the elements may differ, provided there is an automatic conversion from the source element type to the destination element type. The following examples expand on this:

Table: Common Operations of Container Classes
ContType c
Creates an empty container without any element
ContType c1(c2)
Copies a container of the same type
ContType c(beg,end)
Creates a container and initializes it with copies of all elements of [beg,end)
Deletes all elements and frees the memory
Returns the actual number of elements
Returns whether the container is empty (equivalent to size()==0, but might be faster)
Returns the maximum number of elements possible
c1 == 2
Returns whether c1 is equal to c2
c1 != c2
Returns whether c1 is not equal to c2 (equivalent to !(c1==c2))
c1 < c2
Returns whether c1 is less than c2
c1 > c2
Returns whether c1 is greater than c2 (equivalent to c2<c1
c1 <= c2
Returns whether c1 is less than or equal to c2 (equivalent to !(c2<c1))
c1 >= c2
Returns whether c1is greater than or equal to c2 (equivalent to !(c1<c2))
c1 = c2
Assigns all elements of c1 to c2
Swaps the data of c1and c2
Same (as global function)
Returns an iterator for the first element
Returns an iterator for the position after the last element
Returns a reverse iterator for the first element of a reverse iteration
Returns a reverse iterator for the position after the last element of a reverse iteration
Inserts a copy of elem (return value and the meaning of pos differ)
Removes all elements of the range [beg,end) (some containers return next element not removed)
Removes all elements (makes the container empty)
Returns the memory model of the container

  • Initialize with the elements of another container:

  • Initialize with the elements of an array:

  • Initialize by using standard input:

    Don't forget the extra parentheses around the initializer arguments here. Otherwise, this expression does something very different and you probably will get some strange warnings or errors in following statements. Consider writing the statement without extra parentheses:

    In this case, c declares a function with a return type that is deque<int>. Its first parameter is of type istream_iterator<int> with the name cin, and its second unnamed parameter is of type "function taking no arguments returning istream_iterator<int>." This construct is valid syntactically as either a declaration or an expression. So, according to language rules, it is treated as a declaration. The extra parentheses force the initializer not to match the syntax of a declaration.
In principle, these techniques are also provided to assign or to insert elements from another range. However, for those operations the exact interfaces either differ due to additional arguments or are not provided for all container classes.

Size Operations

For all container classes, three size operations are provided:
  1. size()
    Returns the actual number of elements of the container.
  2. empty()
    Is a shortcut for checking whether the number of elements is zero (size()==0). However, empty() might be implemented more efficiently, so you should use it if possible.
  3. max_size()
    Returns the maximum number of elements a container might contain. This value is implementation defined. For example, a vector typically contains all elements in a single block of memory, so there might be relevant restrictions on PCs. Otherwise, max_size() is usually the maximum value of the type of the index.


The usual comparison operators ==, ! =, <, <=, >, and >= are defined according to the following three rules:
  1. Both containers must have the same type.
  2. Two containers are equal if their elements are equal and have the same order. To check equality of elements, use operator ==.
  3. To check whether a container is less than another container, a lexicographical comparison is done.

Assignments and swap ()

If you assign containers, you copy all elements of the source container and remove all old elements in the destination container. Thus, assignment of containers is relatively expensive.

If the containers have the same type and the source is no longer used, there is an easy optimization: Use swap(). swap() offers much better efficiency because it swaps only the internal data of the containers. In fact, it swaps only some internal pointers that refer to the data (elements, allocator, sorting criterion, if any). So, swap() is guaranteed to have only constant complexity, instead of the linear complexity of an assignment.

See Also:


No comments:

Post a Comment