Boost Library and STL introduction

Source: Internet
Author: User

Turn from: http://blog.csdn.net/feitianxuxue/article/details/7237749

Boost Library:
The boost library is one of C + + third party libraries. As a backup of the standard library, some of these content is expected to become the next generation of C + + standard library content, is the "quasi" standard library, most of the use of Boost library features only need to include the corresponding header file, a few need to link the library. But there are also a lot of things that are experimental in boost, and they need to be used carefully in actual development.

1. STL Introduction

Standard Template Library STL is the technology that every person in C + + programming needs to Master today, it is necessary to sum up

This article will introduce the STL and discuss its three main concepts: container, iterator, algorithm.

the biggest feature of STL is:

The separation of data structure and algorithm, not object oriented nature. The Access object is implemented through an iterator like a pointer;

A container is a data structure such as a chain list, a vector, and is provided in a template manner;

The algorithm is a function template that is used to manipulate the data in the container. Because STL is based on a template, it can be used for any data type and structure.

containers can be grouped into three main types: sequence containers, associated containers, and container adapters.

Each STL container has associated member functions, and a subset of these member functions is defined in all STL containers.

The properties and pointers of the STL iterator are similar, and the program can manipulate the elements in the STL container using an iterator

STL algorithms are functions used to perform common data operations, including searching, sorting, and comparing elements, and STL provides approximately 70 algorithms, most of which use iterators to access container elements.

1.1 Container Introduction

Containers can be divided into three types: sequence containers, associated containers, and container adapters.

sequence container : vector deque List

Vector: Quick inserts and deletes from the back end, direct access to any element

Deque: Performs quick inserts and deletes from the front or back, accessing any element directly

List: Double linked lists that can perform fast inserts and deletions anywhere

Associative containers:set Multiset map Multimap

Set: Perform a quick search with elements that do not allow duplicates

Multiset: Perform a quick search with elements allowing duplicates

Map: One-to-one mapping, elements not allowed to repeat, fast search based on keys

Multimap: One-to-many mappings, elements allow repetition, fast search based on keys

container adapters:stack queue priority_queue

Stack: Backward First Out

Queue: Advanced First Out

Priority_queue: The highest-priority element is always the first team

A sequence container represents a linear data structure, such as a vector and a linked list;

The associative container is a non-linear container that can be used to quickly find the elements stored in the container. Such containers can hold a set of values or key/value pairs, and sequence containers and associated containers are collectively referred to as first class containers.

STL implements stacks and queues as container adapters, enabling programs to view sequence containers in a constrained manner.

STL typically implements generic programming through templates to avoid inheritance and virtual functions, and to achieve better performance at execution time.

1.2 Introduction to Iterators

Iterators are in many ways the same as pointers, and are used to point to the elements of the first class container.

An iterator provides access to objects in a container and defines the scope of objects in the container. An iterator is like a pointer. In fact, a pointer to C + + is also an iterator. However, iterators are not just pointers, so you can't assume they have address values. For example, an array index can also be considered an iterator.

There are various ways to create iterators. The program might create an iterator as a variable. An STL container class might create an iterator to use a particular type of data. As a pointer, you must be able to get the data using the * operator class. You can also use other mathematical operators such as + +. Typically, the + + operator is used to increment the iterator to access the next object in the container. If the iterator arrives after the last element in the container, the iterator becomes the Past-the-end value. Using a past-the-end worthy pointer to access an object is illegal, as if using null or a pointer to initialization.

Don't say much nonsense, directly on demo

The following program demonstrates the use of istream_iterator input from standard input and the use of Ostream_iterator output to standard output

#include "stdafx.h"
#include <iostream>
using namespace std;

#include <iterator>

int _tmain (int argc, _tchar* argv[])
{
 cout<< " Enter 2 integers: "<<endl;
 The downlink creates a istream_iterator that can extract the int value std::istream_iterator<int> inputint in a type-safe manner from the standard input object cin
 ( CIN);

 The downlink inputint the iterator and reads an integer from the CIN and assigns it to number1
 int number1=*inputint;
 ++inputint;                    Position the iterator Inputint to the next value int number2=*inputint in the output stream
 ;         Enter the next integer from the Inputint and assign it to number2

 //downlink creates a ostream_iterator that can access the int value in the standard output object cout
 STD::OSTREAM_ Iterator<int> Outputint (cout);
 cout<< " And yes: "<<endl;
 *outputint=number1+number2; will be number1 and number2 and endowed with *outputint
 cout<<endl;
 System ("pause");
 return 0;
}


1.3 Introduction to the algorithm

STL algorithm can be used in various containers. Many of the algorithms provided by STL, which are used for manipulating containers, inserting, deleting, searching, sorting, and other algorithms, are suitable for some or all of the STL containers.

The implementation of STL is very simple. So far, the designer of the class has used the algorithm as a member function of the container to associate the algorithm with the container. STL uses the different method, the STL algorithm and the container is separate, it only indirectly through the iterator operation container element.

STL algorithm can be used in STL container and pointer based, C-style array.

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.