Vector container in STL container

Source: Internet
Author: User

STLYesC ++Class Library.STLThe containers in are queue containers and associated containers, container adapter congtainer adapters: stack, queue, priority queue), bit_set, and string_package.

In the series, I will introduce the list, vector, deque and other queue containers, and association containers such as set, multisets, map, and multimaps. There are a total of seven basic container classes.

Queue container ordered container): the queue container stores the set of T-type values 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 -- 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:

1) default constructor

The default constructor constructs an empty vector with an initial length of 0, for example, vector <int> v1;

2) constructor with a single integer Parameter

This parameter describes the initial vector size. This constructor also has an optional parameter, which is a T-type instance that describes the initial values of each member of each vector type, such as: vector <int> v2 (n, 0 ); if: n is defined in advance, its member values are initialized to 0;

3) copy constructor

Copy the constructor to construct a new vector and use it as the complete copy of an existing vector, for example, vector <int> v3 (v2 );

4) constructor with two constant parameters

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.

For example, vector <int> v4 (first, last)

The following example uses the fourth constructor. You can try other methods on your own.

 
 
  1. // Program: initialization demo
  2. # Include <cstring>
  3. # Include <vector>
  4. # Include <iostream>
  5. Using namespace std;
  6. Int ar [10] = {12, 45,234, 64, 12, 35, 63, 23, 12, 55 };
  7. Char * str = "Hello World ";
  8. Int main ()
  9. {
  10. Vector <int> vec1 (ar, ar + 10); // first = ar, last = ar + 10, excluding ar + 10
  11. Vector <char> vec2 (str, str + strlen (str); // first = str, last = str + strlen (str ),
  12. Cout <"vec1:" <endl;
  13. // Print vec1 and vec2. const_iterator is an iterator.
  14. // Of course, you can also use for (int I = 0; I <vec1.size (); I ++) cout <vec [I]; Output
  15. // Size () is a member function of the vector.
  16. For (vector <int >:: const_iterator p = vec1.begin (); p! = Vec1.end (); ++ p)
  17. Cout <* p;
  18. Cout <'\ n' <"vec2:" <endl;
  19. For (vector <char >:: const_iterator p1 = vec2.begin (); p1! = Vec2.end (); ++ p1)
  20. Cout <* p1;
  21. Cout <'\ n ';
  22. Return 0;
  23. }

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 ().

 
 
  1. # Include <iostream>
  2. # Include <vector>
  3. Using namespace std;
  4. Typedef vector <int> INTVECTOR; // custom type INTVECTOR
  5. // Test the function of the vector container.
  6. Int main ()
  7. {
  8. // The vec1 object is initially empty.
  9. INTVECTOR vec1;
  10. // The vec2 object initially has 10 elements with a value of 6.
  11. INTVECTOR vec2 (10, 6 );
  12. // The vec3 object initially has 3 elements with a value of 6, copying the structure
  13. INTVECTOR vec3 (vec2.begin (), vec2.begin () + 3 );
  14. // Declare a bidirectional iterator named I
  15. INTVECTOR: iterator I;
  16. // Display data in vec1 from front to back
  17. Cout <"vec1.begin () -- vec1.end ():" <endl;
  18. For (I = vec1.begin (); I! = Vec1.end (); ++ I)
  19. Cout <* I <"";
  20. Cout <endl;
  21. // Display data in vec2 from the beginning to the back
  22. Cout <"vec2.begin () -- vec2.end ():" <endl;
  23. For (I = vec2.begin (); I! = Vec2.end (); ++ I)
  24. Cout <* I <"";
  25. Cout <endl;
  26. // Display data in vec3 from front to back
  27. Cout <"vec3.begin () -- vec3.end ():" <endl;
  28. For (I = vec3.begin (); I! = Vec3.end (); ++ I)
  29. Cout <* I <"";
  30. Cout <endl;
  31. // Test the function for adding and inserting members. The vector cannot be inserted before.
  32. Vec1.push _ back (2); // Add a member from the end
  33. Vec1.push _ back (4 );
  34. Vec1.insert (vec1.begin () +); // insert member 5 at the first position of vec1
  35. // Insert all members of vec3 from the first position of vec1
  36. Vec1.insert (vec1.begin () + 1, vec3.begin (), vec3.end ());
  37. Cout <"after push () and insert () now the vec1 is:" <endl;
  38. For (I = vec1.begin (); I! = Vec1.end (); ++ I)
  39. Cout <* I <"";
  40. Cout <endl;
  41. // Test the value assignment function
  42. Vec2.assign (8, 1); // assign a value to vec2 again. The initial values of the eight members are 1.
  43. Cout <"vec2.assign (8, 1):" <endl;
  44. For (I = vec2.begin (); I! = Vec2.end (); ++ I)
  45. Cout <* I <"";
  46. Cout <endl;
  47. // Test the reference class function
  48. Cout <"vec1.front () =" <vec1.front () <endl; // The 0th member of vec1
  49. Cout <"vec1.back () =" <vec1.back () <endl; // The last member of vec1
  50. Cout <"vec1.at (4) =" <vec1.at (4) <endl; // the fifth member of vec1
  51. Cout <"vec1 [4] =" <vec1 [4] <endl;
  52. // Test removal and Deletion
  53. Vec1.pop _ back (); // remove the last Member from vec1
  54. Vec1.erase (vec1.begin () + 1, vec1.end ()-2); // delete a member
  55. Cout <"vec1.pop _ back () and vec1.erase ():" <endl;
  56. For (I = vec1.begin (); I! = Vec1.end (); ++ I)
  57. Cout <* I <"";
  58. Cout <endl;
  59. // Display the sequence status information
  60. Cout <"vec1.size ():" <vec1.size () <endl; // print the number of Members
  61. Cout <"vec1.empty ():" <vec1.empty () <endl; // clear
  62. }

Push_back () is a standard function that puts data into a vector) or a deque dual-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.

In Java, the concept of vectors also exists. A vector in Java is a collection of objects. Each element does not have to be of the same type. It can be added or deleted and cannot be directly added to the original data type.

We hope that the introduction will help you better understand vector containers. See the next article>

Related Article

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.