Vectors in C++ STL | C++ Tutorials for
Beginners
The Standard Template Library (STL) in C++ provides several useful
features for efficient programming. One of the most important
components of STL is the Vector data structure. A Vector is a dynamic
array that can resize itself automatically as elements are added or
removed. Here's a brief overview of how to initialize and manipulate
Vectors in C++.
Initializing Vectors in C++
You can initialize a Vector in C++ in several ways - using default
initialization, initializer list, or by copying an existing Vector. Here's an
example:
#include <vector>
using namespace std;
// Initialize a vector with default values
vector<int> vec1;
// Initialize a vector with initializer list
vector<int> vec2 = {1, 2, 3, 4, 5};
// Initialize a vector by copying another vector
vector<int> vec3 = vec2;
Iterators and Vector Functions
Iterators are a powerful feature of the STL that allow you to traverse
and modify the elements of a Container, including Vectors. You can
use iterators to access, insert, or remove elements from a Vector.
Here are some common Vector functions that use iterators:
vec.begin(); // Returns an iterator pointing to the first
element
vec.end(); // Returns an iterator pointing to the end of
the vector
vec.insert(it, val); // Inserts a value at a specific
iterator
vec.erase(it); // Erases the element at the given
iterator
, vec.push_back(val); // Adds a value to the end of the
vector
vec.pop_back(); // Removes the last element of the
vector
vec.size(); // Returns the number of elements in
the vector
Accessing and Modifying Vectors
You can access and modify the elements of a Vector using the index
operator [], at(), or front() and back() functions. Here are some
examples:
vec[0] = 1; // Access and modify the element at index 0
vec.at(1) = 2; // Access and modify the element at
index 1 using the at() function
vec.front() = 3; // Access and modify the first element
using the front() function
vec.back() = 4; // Access and modify the last element
using the back() function
Arrays vs Vectors in C++
Vectors have several advantages over traditional arrays in C++,
including automatic resizing, dynamic memory management, and
built-in functions like push_back(), pop_back(), and size(). However,
Vectors can be slightly slower than arrays due to their dynamic nature.
Dynamic Arrays: Vectors
Vectors are essentially dynamic arrays that can grow or shrink in size
as necessary. This allows you to write more efficient code that avoids
the limitations of fixed-size arrays.
Bound Checking in Arrays vs Vectors
Vectors provide built-in bound checking, which means that you can't
access an element outside its bounds as you would with a traditional
array. This can help prevent common programming errors and improve
code reliability.
int arr[5];
arr[5] = 10; // This is an error because the array has a
size of 5
vector<int> vec(5);
vec[5] = 10; // This is also an error, and the program
will throw an exception
Beginners
The Standard Template Library (STL) in C++ provides several useful
features for efficient programming. One of the most important
components of STL is the Vector data structure. A Vector is a dynamic
array that can resize itself automatically as elements are added or
removed. Here's a brief overview of how to initialize and manipulate
Vectors in C++.
Initializing Vectors in C++
You can initialize a Vector in C++ in several ways - using default
initialization, initializer list, or by copying an existing Vector. Here's an
example:
#include <vector>
using namespace std;
// Initialize a vector with default values
vector<int> vec1;
// Initialize a vector with initializer list
vector<int> vec2 = {1, 2, 3, 4, 5};
// Initialize a vector by copying another vector
vector<int> vec3 = vec2;
Iterators and Vector Functions
Iterators are a powerful feature of the STL that allow you to traverse
and modify the elements of a Container, including Vectors. You can
use iterators to access, insert, or remove elements from a Vector.
Here are some common Vector functions that use iterators:
vec.begin(); // Returns an iterator pointing to the first
element
vec.end(); // Returns an iterator pointing to the end of
the vector
vec.insert(it, val); // Inserts a value at a specific
iterator
vec.erase(it); // Erases the element at the given
iterator
, vec.push_back(val); // Adds a value to the end of the
vector
vec.pop_back(); // Removes the last element of the
vector
vec.size(); // Returns the number of elements in
the vector
Accessing and Modifying Vectors
You can access and modify the elements of a Vector using the index
operator [], at(), or front() and back() functions. Here are some
examples:
vec[0] = 1; // Access and modify the element at index 0
vec.at(1) = 2; // Access and modify the element at
index 1 using the at() function
vec.front() = 3; // Access and modify the first element
using the front() function
vec.back() = 4; // Access and modify the last element
using the back() function
Arrays vs Vectors in C++
Vectors have several advantages over traditional arrays in C++,
including automatic resizing, dynamic memory management, and
built-in functions like push_back(), pop_back(), and size(). However,
Vectors can be slightly slower than arrays due to their dynamic nature.
Dynamic Arrays: Vectors
Vectors are essentially dynamic arrays that can grow or shrink in size
as necessary. This allows you to write more efficient code that avoids
the limitations of fixed-size arrays.
Bound Checking in Arrays vs Vectors
Vectors provide built-in bound checking, which means that you can't
access an element outside its bounds as you would with a traditional
array. This can help prevent common programming errors and improve
code reliability.
int arr[5];
arr[5] = 10; // This is an error because the array has a
size of 5
vector<int> vec(5);
vec[5] = 10; // This is also an error, and the program
will throw an exception