vector | array | others | |
1 | template class (c++ only) | built-in language construct (both c/c++) | |
2 | dynamic arrays with list interface | statically or dynamically implemented with primitive data type | see ex.1 |
3 | resizable | fixed size | see ex.2 |
4 | automatically deallocated from heap memory | have to be explicitly deallocated | see ex.3 |
5 | size can be determined in O(1) | size cannot be determined of dynamically defined | |
6 | maintains variables keeping track of the size of container all the time | seperate parameter is also passed to the function | see ex.4 |
7 | reallocation is done implicitly when vectors become larger than its capacity | no reallocation is done implicitly | |
8 | can be returned from a function | cannot be returned unless dynamically allocated from a function | see ex.5 and 6 |
9 | can be copied or assigned directly | cannot be copied or assigned directly | see ex.7 |
primitive data type is basic type or built-in type.
#include <bits/stdc++.h>
using namespace std;
/*ex.1*/
int main()
{
int array[100]; // Static Implementation
int* arr = new int[100]; // Dynamic Implementation
vector<int> v; // Vector's Implementation
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*ex.2*/
int main()
{
int array[100]; // Static Implementation
cout << "Size of Array " << sizeof(array) / sizeof(array[0]) << "\n";
vector<int> v; // Vector's Implementation
// Inserting Values in Vector
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
cout << "Size of vector Before Removal=" << v.size() << "\n";
// Output Values of vector
for (auto it : v)
cout << it << " ";
v.erase(v.begin() + 2); // Remove 3rd element
cout << "\nSize of vector After removal=" << v.size() << "\n";
// Output Values of vector
for (auto it : v)
cout << it << " ";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*ex.3*/
int main()
{
int* arr = new int[100]; // Dynamic Implementation
delete[] arr; // array Explicitly deallocated
vector<int> v; // Automatic deallocation when variable goes out of scope
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*ex.4*/
int main()
{
int* arr = new int[100]; // Dynamic Implementation
cout << "Size of array= ";
cout << sizeof(arr) / sizeof(*arr) << "\n"; // Pointer cannot be used to get size of
// block pointed by it
return 0;
}
// Program to demonstrate arrays cannot be returned
#include <bits/stdc++.h>
using namespace std;
/*ex.5*/
int* getValues()
{
int arr[10]; // Array defined locally
for (int i = 0; i < 10; i++) // Putting Values in array
arr[i] = i + 1;
return arr; // returning pointer to array
}
// main function
int main()
{
int* array; // pointer of int type
array = getValues(); // Call function to get arr
for (int i = 0; i < 10; i++) { // Printing Values
cout << "*(array + " << i << ") : ";
cout << *(array + i) << endl;
}
return 0;
}
// Program to demonstrate vector can be returned
#include <bits/stdc++.h>
using namespace std;
/*ex.6*/
// Function returning vector
vector<int> getValues()
{
vector<int> v; // Vector defined locally
for (int i = 0; i < 10; i++) // Inserting values in Vector
v.push_back(i + 1);
return v; // returning pointer to array
}
// main function
int main()
{
vector<int> get;
get = getValues(); // Call function to get v
// Output Values of vector
for (auto it : get)
cout << it << " ";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/*ex.7*/
// main function
int main()
{
vector<int> v; // Vector defined locally
for (int i = 0; i < 10; i++)
v.push_back(i + 1);
vector<int> get;
get = v; // Copying vector v into vector get
cout << "vector get:\n";
for (auto it : get)
cout << it << " ";
int arr[10];
for (int i = 0; i < 10; i++) // Putting Values in array
arr[i] = i + 1;
int copyArr[10];
copyArr = arr; // Error
return 0;
}
revised from: https://www.geeksforgeeks.org/advantages-of-vector-over-array-in-c/