Sunday, May 6, 2012

stl container operator

The operations common to all containers meet the core abilities that were mentioned in the previous subsection. Table below lists these 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.

Note: If a system does not provide member templates, it will typically allow only the same types. In this case, you can use the copy() algorithm instead.

Initialize with the elements of another container:

std::list<int> l; //l is a linked list of ints
//copy all elements of the list as floats into a vector0
std::vector<float> c(l.begin(),l.end());

Initialize with the elements of an array:

int array[] = { 2, 3, 17, 33, 45, 77 };
//copy all elements of the array into a set
std::set<int> c(array,array+sizeof(array)/sizeof(array[0]));

Initialize by using standard input:

//read all integer elements of the deque from standard input
std::deque<int> c((std::istream_iterator<int>(std::cin)),

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:

std::deque<int> c(std::istream_iterator<int>(std::cin),

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