Vector initialization and sharing

Source: Internet
Author: User

Guoxinmiao8.blog.sohu.com/103762653.html

STL is part of the ANSI/ISO standard for C ++ and can be used in all c ++ language compilers and all platforms. The same version of STL is available in any hardware configuration. STL provides a large number of reusable software organizations. For example, programmers no longer need to design their own sorting and search algorithms. These are already part of STL. The code written using STL is easier to modify and read. Because the code is shorter, many basic work codes have been componentized.

STL Composition
STL has three core components: container, algorithm, iterator, container adapter, function object ), in addition, there are other standard STL components.

Container ):

Containers are the methods for organizing data in memory, such as arrays, stacks, queues, linked lists, or binary trees (but these are not STL standard containers ). Containers in STL are a data structure that stores a finite set of T (Template) values. The internal implementation of containers is generally a class. These values can be objects, if the data type T represents class.

Algorithm (algorithm ):

An algorithm is a behavior or function that is used to process its content in a container in various ways. For example, there are algorithms for sorting, copying, searching, and merging container content. In STL, algorithms are represented by template functions. These functions are not a member function of the container class. Instead, they are independent functions. One of the surprising features is that its algorithms are so common. It can be used not only for STL containers, but also for common C ++ arrays or containers specified by any other application.

Iterator ):

Once a container type and data behavior (algorithm) are selected, the only thing that you need to do is to use the iterator to interact with each other. The dashboard can be seen as a normal pointer to elements in the container. You can increment the iterator like a pointer to point it to each subsequent element in the container in turn. An iterator is a key part of STL because it connects algorithms with containers.

Next I will introduce the three main components of STL in sequence.

Container
Containers in STL include queue containers and associated containers, container adapters (congtainer adapters: Stack, queue, priority queue), bit_set, and string_package.
In this article, I will introduce the list, vector, deque and other queue containers, and association containers such as set, multisets, map, and multimaps. There are seven basic container classes.
Queue container (ordered container): the queue container stores the T-type value set in a linear arrangement. Each member of the queue has its own unique position. Ordered containers include vector type, double-end queue type, and list type.
Basic container-ordered container
Vector (vector container class): # include <vector>. vector is a dynamic array and a class template for basic arrays. Many basic operations are defined internally. Since this is a class, it will have its own constructor. Four constructor types are defined in the vector class:

The default constructor constructs an empty vector with an initial length of 0,
For example, vector <int> V1;
Constructor with a single integer parameter, which describes the initial size of a vector. This constructor also has an optional parameter, which is an instance of the T type and describes the initial values of each member of each vector;
For example: vector <int> V2 (init_size, 0); if it is pre-defined: int init_size; its member values are initialized to 0;
Copy the constructor to construct a new vector. As a complete copy of an existing vector,
For example, vector <int> V3 (V2 );
A constructor with two constant parameters generates a vector with an initial value as an interval. The interval is specified by a semi-open interval [first, last] (the display of MS word may be problematic, first is a left square bracket, last is a right circle bracket.
For example, vector <int> V4 (first, last)
The following example uses the fourth constructor. You can try other methods on your own.

// Stl_cpp_7.cpp
// Program: initialization demo
# Include <cstring>
# Include <vector>
# Include <iostream>
Using namespace STD;
Int ar [10] = {12, 45,234, 64, 12, 35, 63, 23, 12, 55 };
Char * STR = "Hello World ";
Int main (void)
{
Vector <int> vec1 (AR, Ar + 10); // first = AR, last = Ar + 10, excluding Ar + 10
Vector <char> vec2 (STR, STR + strlen (STR); // first = STR, last = STR + strlen (STR), excluding the last
Cout <"vec1:" <Endl;
// Print vec1 and vec2. const_iterator is an iterator.
// Of course, you can also use for (INT I = 0; I <vec1.size (); I ++) cout <VEC [I]; Output
// Size () is a member function of the vector.
For (vector <int >:: const_iterator P = vec1.begin (); P! = Vec1.end (); ++ P)
Cout <* P;
Cout <''\ n' <" vec2: "<Endl;
For (vector <char >:: const_iterator p1 = vec2.begin (); P1! = Vec2.end (); ++ P1)
Cout <* P1;
Getchar ();
Return 0;
}

To help understand the concept of vectors, we have written a small example, where the member functions of vector are used: Begin (), end (), push_back (), assign (), front (), back (), erase (), empty (), at (), size ().

// Stl_cpp_8.cpp
# Include <iostream>
# Include <vector>
Using namespace STD;
Typedef vector <int> intvector; // custom type intvector
// Test the function of the vector container.
Void main (void)
{
// The vec1 object is initially empty.
Intvector vec1;
// The vec2 object initially has 10 elements with a value of 6.
Intvector vec2 (10, 6 );
// The vec3 object initially has 3 elements with a value of 6, copying the structure
Intvector vec3 (vec2.begin (), vec2.begin () + 3 );
// Declare a bidirectional iterator named I
Intvector: iterator I;
// Display data in vec1 from front to back
Cout <"vec1.begin () -- vec1.end ():" <Endl;
For (I = vec1.begin (); I! = Vec1.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Display data in vec2 from the beginning to the back
Cout <"vec2.begin () -- vec2.end ():" <Endl;
For (I = vec2.begin (); I! = Vec2.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Display data in vec3 from front to back
Cout <"vec3.begin () -- vec3.end ():" <Endl;
For (I = vec3.begin (); I! = Vec3.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Test the function for adding and inserting members. The vector cannot be inserted before.
Vec1.push _ back (2); // Add a member from the end
Vec1.push _ back (4 );
Vec1.insert (vec1.begin () +); // insert member 5 at the first position of vec1
// Insert all members of vec3 from the first position of vec1
Vec1.insert (vec1.begin () + 1, vec3.begin (), vec3.end ());
Cout <"after push () and insert () now the vec1 is:" <Endl;
For (I = vec1.begin (); I! = Vec1.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Test the value assignment function
Vec2.assign (8, 1); // assign a value to vec2 again. The initial values of the eight members are 1.
Cout <"vec2.assign (8, 1):" <Endl;
For (I = vec2.begin (); I! = Vec2.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Test the reference class function
Cout <"vec1.front () =" <vec1.front () <Endl; // The 0th member of vec1
Cout <"vec1.back () =" <vec1.back () <Endl; // The last member of vec1
Cout <"vec1.at (4) =" <vec1.at (4) <Endl; // the fifth member of vec1
Cout <"vec1 [4] =" <vec1 [4] <Endl;
// Test removal and Deletion
Vec1.pop _ back (); // remove the last Member from vec1
Vec1.erase (vec1.begin () + 1, vec1.end ()-2); // delete a member
Cout <"vec1.pop _ back () and vec1.erase ():" <Endl;
For (I = vec1.begin (); I! = Vec1.end (); ++ I)
Cout <* I <"";
Cout <Endl;
// Display the sequence status information
Cout <"vec1.size ():" <vec1.size () <Endl; // print the number of Members
Cout <"vec1.empty ():" <vec1.empty () <Endl; // clear
}

Push_back () is a standard function that puts data into a vector or deque (double-end Queue. Insert () is a similar function, but it can be used in all containers, but its usage is more complex. End () is actually adding one at the end to let the loop run correctly -- the pointer it returns points to the data closest to the array boundary.

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.