The Basics of C++ Vector

Ji Kim·2020년 8월 7일
0

C++

목록 보기
1/1
post-thumbnail

1. What is Vector?

Vector in C++ is a sequence container representing arrays that can change in size (dynamic allocation)

2. Vector vs. Array

  1. Vector uses dynamic allocation in which it allocates additional size with more element insertion, whereas Array is a fixed size container which the size can not be reset once initialized
  2. Vector occupies much more memory in exchange while the Array is a memory efficient data structure
  3. Vector is from the C++ template class while the Array is the lower-level data structure within C++

3. When Should I Use Vector?

  1. When data are need to be saved dynamically (i.e. - when the amount of data is not fixed)
  2. When insertion / removal of data in the middle of the structure is not frequent
  3. When the amount of data needed to be saved is minimal
  4. When randomly accessing the index of the data

4. Pros and Cons of Vector in C++

Pros

1.Easy to Add / Remove Last Element

Code

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    for (vector<int>::size_type i = 0; i < v.size(); ++i)
        cout << v[i] << endl;
    
    return 0;
}

Output

10
20
30
40
50

Function push_back() adds a new element at the end of the vector.

Code

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    for (vector<int>::size_type i = 0; i < v.size(); ++i)
        cout << v[i] << endl;
    cout << endl;
    
    v.pop_back(); // delete last element 50
    v.pop_back(); // delete last element 40

    for (vector<int>::size_type i = 0; i < v.size(); ++i)
        cout << v[i] << endl;

    return 0;
}

Output

10
20
30
40
50

10
20
30

Function pop_back() removes the last element in the vector

2.Easy to Access the Element by Index

Vector is convenienet to access the target element using the index.

Code

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    cout << v[0] << endl;
    cout << v[1] << endl;
    cout << v[2] << endl;  

    return 0;
}

Output

10 // 0th element of vector "v";
20 // 1st element of vector "v";
30 // 2nd element of vector "v";

Cons

1. Inefficienct When Performing Insertion / Deletion in the Middle

As the picture shown above, when performing the middle deletion of element "D" of the third index, entire elements of the vector must be adjusted hence hindering the efficiency.

2. Inefficient When Searching Target Element in Large-scale Data

Assuming there is a vector size of 10,000 consisted of random characters.

If wish to find unique character "K", coincidentally located at the last index of the vector, then the program will perform 10,000 searches to generate the result.

5. How to Implement Vector in C++

1. Header File

#inlcude <vector> // imports vector class from C++ std 

2. Declare & Create Vector

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int> v; // declare int-type vector name "v"
    	vector<int> v(10); // vector of ten elements of 0
    	vector<int> v(10, 5); // vector of ten elements of 5
}

3. Add / Delete Elements

int main()
{
    vector<int> v;

    v.push_back(10); 
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    v.insert(1, 15); // insert element 15 at index 1

    v.pop_back();
    v.pop_back();
    v.pop_back();
    
    return 0;
}

4. Check Size

int main()
{
    vector<int> v;

    v.push_back(10); 
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    v.insert(1, 15); // insert element 15 at index 1

    v.pop_back();
    v.pop_back();
    v.pop_back();
    
    cout << v.size() << endl; // vector "v" has a size of 3
}

4. Check is or not Empty

int main()
{
    vector<int> v;

    v.push_back(10); 
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    v.insert(1, 15); // insert element 15 at index 1

    v.pop_back();
    v.pop_back();
    v.pop_back();
    
    cout << v.empty() << endl; // whether the vector is empty or not 
    
}
profile
if this then that

1개의 댓글

comment-user-thumbnail
2023년 6월 19일

If you're in search of the ultimate relaxation experience and want to invest in a massage chair that delivers unparalleled comfort, quality, and functionality, I wholeheartedly recommend Massage Chair Genius. visit, click for more info, Use this website, click to read more, find more info.

답글 달기