Getting Started with Standard Template Library (STL) (ON)

Source: Internet
Author: User

Perhaps you have used C + + as the main programming language to solve TopCoder problems. This means that you have simply used the STL because both the array and the string are passed as STL objects to the function. You may have noticed that many programmers write code much faster and more succinctly than you.

Maybe you're not yet. But want to be a C + + program APE, because this programming language is powerful and rich in libraries (perhaps because there are a lot of very streamlined solutions in TopCoder's practice room and contests).

No matter how it used to be, this article will help. Here, we'll review some of the powerful features of the standard Template Library (Standard Template Library-stl, a very useful tool that sometimes saves you a lot of time in an algorithmic race).

The easiest way to familiarize yourself with the STL is to start with the container. Container

Whenever you need to manipulate a large number of elements, you use some kind of container. The C language has only one built-in container: an array.

The problem is not that the array has limitations (for example, it is not possible to determine the size of the array at run time). Rather, the problem is that many tasks require more powerful containers.

For example, we might need one or more of the following actions: Add a string to the container remove a string from the container determine if a string exists in the container to return some distinct elements from the container loop through the container, obtaining an additional list of strings in some order.

Of course, we can implement these functions on an ordinary array. However, these trivial implementations can be very inefficient. You can create a tree structure or hash structure to solve the problem quickly, but think about this: the implementation of this container depends on the type of element that is about to be stored. For example, if we want to store points on a plane rather than strings, we have to rewrite this module to implement functionality.

If not, then we can develop an interface for this container once and for all, and then we can use it for any data type. In short, this is the idea of STL containers. Preface

When a program uses STL, it should include (#include) the appropriate standard header file. For most containers, the name of the standard header file is the same as the container name and does not require an extension. For example, if you want to use stacks, just add the following line of code at the beginning of the program:

1 #include <stack>

Container types (as well as algorithms, operators, and all STL) are not defined in the global namespace, but are defined in a special namespace called Std. After you include all the header files, add the following line before writing the code:

1 using namespace Std;

There's another important thing to remember: The container type is also a template parameter. The template parameters are indicated in the code with "angle brackets" (' < '/' > '). Like what:

1 vector< int > N;

If you want to make nested constructs, make sure that the brackets are not next to each other--leave a space in place. (Translator: c++11 new features support two angle brackets next to each other, no need to add spaces)

1 2 3 vector< vector< int

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.