What is C ++ vector? How to understand-Alibaba Cloud Developer Community

Recently, several friends asked me how the vector in C ++ understands it. Many people have never touched it. I was confused all the time. In fact, vector is easy to understand. It is an array that can dynamically create the number of object elements. If you don't know what an array is, don't waste your time reading it. Because the definition of the number of array elements in C ++ can only use a constant value or a value that can be obtained at compile time. Such

const int ay=5; int array[ay];

or

int array[5];

not supported

int ay; int array[ay];

this definition. Therefore, the number of array elements cannot be dynamically defined in C ++ except the NEW expression (which is troublesome). Then how to solve this problem.

C ++ released the standard library vector

when using it, you need to first lead the file

#include <vector>using std::vector;

next, use a few simple columns to illustrate the difference between them and arrays.

01 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

02 create Time: 2009/09/21 21:11

03 File name: test1.cpp

04 file suffix: cpp

05 document author: QianLiang

06 07 =====================================================

08 function description: explain what a vector is and its difference from an array

09 -----------------------------------------------------------------

10 other instructions:

11 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

12 #include <iostream>

13 #include <vector>

14 using namespace std;

15

16 int main()

17 {

18 /* array: number of static defined elements */

19 const int ay=5;

20 int array1[ay];

21

22 /* vector: Dynamically Define the number of elements */

23 int vy;

24 cin&gt;&gt;vy;

25 vector <int>ivec1(vy);

26

27 /* test the default value of each element. Assume that the vy input above is 5 */

28 for (int I =0; I

29 {

30 cout&lt;&lt;"array:\t" &lt; <array1[i]<<endl; //它输出的全是随即数

31 cout&lt;&lt;"vector:\t"&lt; <ivec1[i]<<endl;//它将全部元素初始化为0

32 cout&lt; <endl; 33 }

34

35 /* test their assignment Security */

36 int array2[6];

37 // array1 = array2; ← this statement is wrong.

38 vector <int>ivec2(3);

39 ivec1 = ivec2; // This statement is correct because it can dynamically determine the number of elements

40

41

42 /* only the above three tests are enough to show that using vector is much safer than array,

43 first, it is the number of dynamic management elements, which avoids many cross-border problems.

44 second, vector automatically adds each element when they are not initialized

45 This type of security default value, which also avoids some problems.

46

47 let's look at the use of vector, which has four constructors

48 vector v1; ← The vector stores the object of T type. The default constructor v1 is empty.

49 vector <T>v2(v1); ← V2 is a copy of V1.

50 vector <T>v3(n, I); ← v3 contains n elements with a value of I.

51 vector v4(n); ← v4 contains n copies of elements with value initialization. */

52

53 /* vector operations */

54 cout&lt; <ivec1.empty();//如果ivec1为空则返回true(1),否则返回false(0)

55 cout&lt; <ivec1.size();//返回ivec1中元素的个数;

56 ivec1.push_back(t) // add an element with a value of t to the end of ivec1.

57

58 return 0;

59}

when to use vectors and when to use arrays?

Because vector is a standard library, while array is a built-in type. So if you pursue the execution efficiency of the program, then use arrays. **You can use an array when determining the number of array elements. Use vector when the pointer and the NEW operator are confused because it has a built-in memory manager.

The following is a programming exercise about vector.

01 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

02 create Time: 2009/09/21 21:18

03 File name: exercise 3-13.cpp

04 file suffix: cpp

05 document author: QianLiang

06

07 =====================================================

08 function description: read a set of integers to the vector object, calculate and output the sum of each pair of adjacent elements, such

09 if the number of elements Read is odd, the user is prompted that the last element is not summed

10, and output its value. Then modify the program: the first and the last elements are paired (the first and the most

11 the last one, the second and the last two, and so on), calculate the sum of each pair of elements

12 and output.

13 -----------------------------------------------------------------

14 Other notes:

15 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

16 #include <iostream>

17 #include <vector>

18 using namespace std;

19

20 int main()

21 {

22 vector <int>ve;

23 int a(0);

24 const int CI(2);

25

26 cout&lt;&lt;"input an integer, and the program calculates the sum of their adjacent numbers. When input 0, the program starts to calculate" &lt; <endl;

27 /* request the user to enter an integer, each number is an element of ve, if the user enters 0

28 The program will stop the request */

29 while (cin&gt;&gt;a &amp;&amp; a! = 0)

30 {

31 ve.push_back(a);

32}

33

34 /* count the number of ve elements minus 1 and assign it to variable B */

35 int B (ve.size()-1);

36

37 /* determine whether the user directly enters 0 or any key on the keyboard */

38 if (ve.size()==0)

39 {

40 cout&lt;&lt;"no element" &lt; <endl;

41}

42

43 /* determine whether the number of input elements is odd */

44 else if (ve.size()%CI! = 0)

45 {

46 /* determine whether the user has entered only one number */

47 if (ve.size()==1)

48 {

49 cout&lt;&lt;"one element cannot be calculated" &lt; <endl;

50}

51 else

52 {

53 cout&lt;&lt;"the number of elements you entered is odd, and the last number cannot be added" &lt; <endl;

54 for (int I =0; I <b;i+=CI)

55 {

56 cout&lt;&lt;"no" &lt; <i+1<<"组数:"<<ve[i]+ve[i+1]<<endl;

57}

58

59 cout&lt;&lt;"The last element you entered is:" &lt; <ve[b]<<endl;

60}

61}

62

63 /* determine whether the number of elements entered by the user is even */

64 else if (ve.size()%CI == 0)

65 {

66 for (int I =0; I <b;i+=CI)

67 {

68 cout&lt;&lt;"no" &lt; <i+1<<"组数:"<<ve[i]+ve[i+1]<<endl;

69}

70}

71

72 return 0;

73}

this article is transferred from the blog of Nongfu Mountain Spring Villa, original link: http://www.cnblogs.com/yaowen/archive/2013/01/15/2861894.html,如需转载请自行联系原作者

Please read this disclaimer carefully before you start to use the service. By using the service, you acknowledge that you have agreed to and accepted the content of this disclaimer in full. You may choose not to use the service if you do not agree to this disclaimer. This document is automatically generated based on public content on the Internet captured by Machine Learning Platform for AI. The copyright of the information in this document, such as web pages, images, and data, belongs to their respective author and publisher. Such automatically generated content does not reflect the views or opinions of Alibaba Cloud. It is your responsibility to determine the legality, accuracy, authenticity, practicality, and completeness of the content. We recommend that you consult a professional if you have any doubt in this regard. Alibaba Cloud accepts no responsibility for any consequences on account of your use of the content without verification. If you have feedback or you find that this document uses some content in which you have rights and interests, please contact us through this link: https://www.alibabacloud.com/campaign/contact-us-feedback. We will handle the matter according to relevant regulations.
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now