Elementary Introduction to C + + STL

Source: Internet
Author: User

The C + + STL (Standard Template Library) is a powerful set of C + + template classes that provide generic template classes and functions that can implement a variety of popular and commonly used algorithms and data structures such as vectors, lists, queues, stacks.

The core of the C + + Standard Template Library consists of the following three components:

Component

Describe

Container (Containers)

A container is a collection that is used to manage a class of objects. C + + provides a variety of different types of containers,

such as deque, list, vector, map and so on.

Algorithm (algorithms)

The algorithm acts on the container. They provide a way to perform a variety of operations, including the content of the container

Operations such as row initialization, sorting, searching, and conversion.

Iterators (iterators)

Iterators are used to iterate over the elements of an object collection. These collections may be containers, or they may be a subset of containers.

These three components come with a rich set of predefined functions that help us handle complex tasks in a simple way.

The following program shows a vector container (a C + + Standard template), which is very similar to an array, except that the vector automatically handles its own storage requirements when it needs to be scaled in size:

1#include <iostream>2#include <vector>3 using namespacestd;4  intMain ()5 {6    //Create a vector store int7vector<int>Vec;8    inti;9    //show the original size of the VECTencout <<"Vector size ="<< vec.size () <<Endl; One    //pushes 5 values into a vector A     for(i =0; I <5; i++){ - Vec.push_back (i); -    } the    //show the size of the VEC extension -cout <<"extended vector size ="<< vec.size () <<Endl; -    //5 values in an access vector -     for(i =0; I <5; i++){ +cout <<"value of Vec ["<< I <<"] = "<< Vec[i] <<Endl; -    } +    //accessing values using Iterators iterator Avector<int>::iterator v =Vec.begin (); at     while(V! =Vec.end ()) { -cout <<"value of V ="<< *v <<Endl; -v++; -    } -    return 0; -}

When the above code is compiled and executed, it produces the following results:

Vector size = 0extended vector size = 5value of Vec [0] = 0value of VEC [1] = 1value of VEC [2] = 2value of VEC [3] = 3val UE of VEC [4] = 4value of v = 0value of v = 1value of v = 2value of v = 3value of V = 4

There are a few things to note about the various functions used in the above example:

    • The push_back () member function inserts a value at the end of the vector, extending the size of the vector if necessary.
    • The size () function displays the dimensions of the vector.
    • The Begin () function returns an iterator that points to the beginning of the vector.
    • The end () function returns an iterator that points to the end of the vector.

  

Elementary Introduction to C + + STL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.