Learning the standard library type of C ++ from scratch (2): Vector class introduction and routine

Source: Internet
Author: User

1. Vector type of the standard library

A vector is a set of objects of the same type.

The data structure of a vector is very similar to an array, allowing efficient and convenient access to a single element.

Vector is a class template)

template <   class Type,    class Allocator = allocator<Type> >class vector

To use vector, the header file must be included.

# Include <vector>

Using STD: vector;

Vector object initialization:

The vector class defines several constructors.

Vector <t> V1;

// The object whose vector storage type is T. The default constructor V1 is null.

Vector <t> V2 (V1); // V2 is a copy of V1.

Vector <t> V3 (n, I); // V3 contains n elements whose values are I.

Vector <t> V4 (N );

// V4 contains N copies of elements whose values are initialized


Common Vector member functions:



Example 1:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Include <vector>
# Include <iostream>
Using namespace STD;

Typedef vector <int> intvec;

// Void showvec (const intvec & V)
//{
// Unsigned int I;
// For (I = 0; I <v. Size (); I ++)
//{
// Cout <V [I] <"";
//}
// Cout <Endl;
//}

// Void showvec (intvec & V)
//{
// Intvec: iterator it;
// For (IT = V. Begin (); it! = V. End (); ++ it)
//{
// Cout <* It <"";
//}
//
// Cout <Endl;
//}

Void showvec (const intvec & V)
{
Intvec: const_iterator it;
For (IT = V. Begin (); it! = V. End (); ++ it) // All iterators are overloaded! = Operator, but some are not overloaded.
{
Cout <* It <"";
}

Cout <Endl;
}

Int main (void)
{
Intvec V;
V. push_back (1 );
V. push_back (2 );
V. push_back (3 );

Showvec (v );

Return 0;
}

Example 2:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Include <vector>
# Include <iostream>
# Include <algorithm>
Using namespace STD;

Typedef vector <int> intvec;

Void showvec (const intvec & V)
{
Intvec: const_iterator it;
For (IT = V. Begin (); it! = V. End (); ++ it)
{
Cout <* It <"";
}

Cout <Endl;
}

Int main (void)
{
Intvec V;
V. push_back (1 );
V. push_back (2 );
V. push_back (3 );
V. push_back (4 );
V. push_back (5 );
V. push_back (3 );
// Cout <v. Back () <Endl;
// V. pop_back ();

Showvec (v );

// V. Erase (V. Begin () + 2 );
// V. Erase (V. Begin (), V. Begin () + 2 );

V. Erase (remove (V. Begin (), V. End (), 3), V. End ());
Showvec (v );

Return 0;
}


Example 3:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Include <vector>
# Include <iostream>
# Include <algorithm>
Using namespace STD;

Typedef vector <int> intvec;

Void showvec (const intvec & V)
{
Intvec: const_iterator it;
For (IT = V. Begin (); it! = V. End (); ++ it)
{
Cout <* It <"";
}

Cout <Endl;
}

Int main (void)
{
Intvec V;
V. push_back (1 );
V. push_back (2 );
V. push_back (3 );
V. push_back (4 );
V. push_back (5 );
V. push_back (3 );

Showvec (v );

// V. Erase (remove (V. Begin (), V. End (), 3), V. End ());
Intvec: iterator it;
For (IT = V. Begin (); it! = V. End ();/* ++ it */)
{
If (* It = 3)
{
It = V. Erase (it); // erase returns the next element of the currently deleted element.
}
Else
++ It;
}
Showvec (v );

Return 0;
}



Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

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.