Vectors in C++ are dynamic arrays that can change size, making them a flexible alternative to traditional arrays.
Vectors are declared using the std::vector
template class. You must include the <vector>
header.
Use the push_back()
method to add elements to the end of the vector.
Elements in a vector can be accessed using the []
operator or the at()
method.
You can use iterators or range-based for loops to iterate over vector elements.
The pop_back()
method removes the last element, while erase()
can remove specific elements.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
for(int i = 0; i < numbers.size(); i++) {
cout << numbers[i] << " ";
}
return 0;
}
The size()
method returns the number of elements, while capacity()
shows the allocated storage.
Vectors can be resized using the resize()
method, which can also initialize new elements.
The clear()
method removes all elements, reducing the size to zero.
The swap()
method exchanges the contents of two vectors efficiently.
Use the empty()
method to check if a vector has no elements.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
numbers.resize(3);
cout << "Resized Size: " << numbers.size() << endl;
numbers.clear();
cout << "Cleared Size: " << numbers.size() << endl;
return 0;
}
Console Output:
10 20 30
Size: 5
Capacity: 5
Resized Size: 3
Cleared Size: 0
The emplace_back()
method constructs elements in place, which can be more efficient than push_back()
.
Vectors can hold other vectors, creating multi-dimensional arrays.
Use the sort()
function from the <algorithm>
library to sort vector elements.
The find()
method locates elements in a vector, returning an iterator to the found element.
Custom comparators can be used with sort()
to define custom sorting logic.
Vectors can be copied using the assignment operator or the copy()
method.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {4, 2, 5, 1, 3};
sort(numbers.begin(), numbers.end());
vector<vector<int>> matrix = {{1, 2}, {3, 4}, {5, 6}};
for(const auto& row : matrix) {
for(int num : row) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
Iterators provide a way to traverse vectors, offering more control than range-based loops.
The reverse()
function reverses the order of elements in the vector.
The insert()
method allows you to add elements at specific positions.
The erase()
method removes elements from specified positions.
Use unique()
to remove consecutive duplicates, followed by erase()
to resize the vector.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 2, 3, 4, 4, 5};
auto it = unique(numbers.begin(), numbers.end());
numbers.erase(it, numbers.end());
for(int num : numbers) {
cout << num << " ";
}
return 0;
}
Console Output:
1 2 3 4 5
1 2
3 4
5 6
Capacity is the amount of space that has been allocated for the vector, which may be larger than the current size.
The reserve()
method pre-allocates memory, which can improve performance by reducing reallocations.
The shrink_to_fit()
method reduces capacity to fit the current size, freeing unused memory.
Vectors support copy construction, allowing one vector to be initialized as a copy of another.
C++11 introduced move semantics, optimizing performance by transferring resources instead of copying.
The allocator
class provides a way to define custom memory management strategies for vectors.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
numbers.reserve(10);
cout << "Capacity after reserve: " << numbers.capacity() << endl;
for(int i = 0; i < 5; i++) {
numbers.push_back(i);
}
numbers.shrink_to_fit();
cout << "Capacity after shrink_to_fit: " << numbers.capacity() << endl;
return 0;
}
Pre-allocating memory and using move semantics can significantly enhance performance when working with large vectors.
Vectors adhere to the RAII (Resource Acquisition Is Initialization) principle, automatically managing memory.
Vectors provide strong exception safety guarantees, ensuring consistent states even during exceptions.
Certain operations may invalidate iterators, requiring careful management when modifying vectors.
Custom allocators allow for specialized memory management, which can be useful in resource-constrained environments.
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main() {
vector<int, allocator<int>> numbers(5, 1);
for(int num : numbers) {
cout << num << " ";
}
return 0;
}
Console Output:
Capacity after reserve: 10
Capacity after shrink_to_fit: 5
1 1 1 1 1
Vectors provide various methods for accessing elements, including front()
, back()
, and data()
.
Understanding the difference between size and capacity is crucial for efficient vector usage.
Modifiers like assign()
, insert()
, and erase()
allow you to change the contents of a vector.
Vectors support relational operators like ==
, !=
, <
, and >
for comparison.
The swap()
method exchanges the contents of two vectors efficiently.
Standard algorithms like sort()
and reverse()
can be applied to vectors for various operations.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
cout << "Front: " << numbers.front() << endl;
cout << "Back: " << numbers.back() << endl;
vector<int> other = {5, 15, 25};
swap(numbers, other);
for(int num : numbers) {
cout << num << " ";
}
return 0;
}
Range-based loops provide a concise way to iterate over vector elements, enhancing readability.
Custom comparators allow for sorting vectors based on user-defined criteria.
C++11 lambdas provide a powerful way to define inline functions, often used with algorithms.
The find()
algorithm checks for the presence of an element, returning an iterator.
The all_of()
algorithm checks if all elements satisfy a condition, useful for validation.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
bool all_positive = all_of(numbers.begin(), numbers.end(), [](int i) { return i > 0; });
cout << (all_positive ? "All positive" : "Not all positive") << endl;
return 0;
}
Console Output:
Front: 10
Back: 50
5 15 25
All positive
Use references and iterators to avoid unnecessary copies when working with large vectors.
Use reserve()
to pre-allocate memory when the size of the vector is known beforehand.
Move semantics can significantly enhance performance by transferring resources instead of copying.
Use range-based loops or iterators for efficient iteration over vector elements.
Ensure exception safety by catching and handling exceptions appropriately when using vectors.
Be mindful of vector capacity and use shrink_to_fit()
to free unused memory.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
numbers.reserve(100); // Pre-allocate memory for 100 elements
for(int i = 0; i < 100; i++) {
numbers.push_back(i);
}
try {
cout << numbers.at(101) << endl; // Throws an exception
} catch (const out_of_range& e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
}
Use const
references to access vector elements when modifications are not needed.
Utilize STL algorithms like sort()
and find()
for efficient operations on vectors.
Be cautious of operations that may invalidate iterators, such as erase()
and insert()
.
Insert elements efficiently by minimizing shifts, especially in large vectors.
Vectors are not thread-safe, so ensure proper synchronization when accessing them from multiple threads.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {3, 1, 4, 1, 5, 9};
sort(numbers.begin(), numbers.end());
for(const int& num : numbers) {
cout << num << " ";
}
return 0;
}
Console Output:
Exception: vector::_M_range_check: __n (which is 101) >= this->size() (which is 100)
1 1 3 4 5 9
Vectors are ideal for implementing dynamic arrays, offering flexibility and ease of use.
Vectors can be used to implement complex data structures like stacks, queues, and graphs.
Vectors are commonly used in algorithm implementations due to their dynamic nature and ease of access.
Vectors provide an efficient way to store and manipulate large datasets in memory.
Vectors are widely used in game development for managing collections of objects, such as game entities.
Vectors are used in scientific computing for handling large arrays of data and performing complex calculations.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> stack;
stack.push_back(10); // Push
stack.push_back(20);
stack.pop_back(); // Pop
if(!stack.empty()) {
cout << "Top element: " << stack.back() << endl;
}
return 0;
}
Vectors are well-suited for handling large datasets, providing efficient access and modification capabilities.
Vectors can be used in real-time systems for managing dynamic data with minimal overhead.
Vectors are used in machine learning for storing feature vectors and performing operations on them.
Vectors provide a robust framework for financial modeling, allowing for efficient data manipulation and analysis.
Vectors can be used in web development for managing collections of data and performing client-side operations.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> queue;
queue.push_back(10); // Enqueue
queue.push_back(20);
if(!queue.empty()) {
cout << "Front element: " << queue.front() << endl;
queue.erase(queue.begin()); // Dequeue
}
return 0;
}
Console Output:
Top element: 10
Front element: 10
Vectors may have higher memory overhead compared to static arrays due to dynamic resizing.
Frequent resizing and copying can introduce performance overhead, especially with large datasets.
Certain operations, such as insertions and deletions, can invalidate iterators, leading to undefined behavior if not handled properly.
Vectors are not inherently thread-safe, requiring external synchronization when accessed by multiple threads.
Vectors require contiguous memory allocation, which may lead to fragmentation issues in certain scenarios.
While flexible, vectors may not be suitable for all use cases, such as those requiring constant-time insertions and deletions.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
auto it = numbers.begin();
numbers.push_back(6); // Invalidate iterator
cout << *it << endl; // Undefined behavior
return 0;
}
For extremely large datasets, consider alternative data structures like linked lists or deques.
Custom allocators can help mitigate some of the memory overhead associated with vectors.
When using vectors in concurrent environments, ensure proper synchronization to avoid race conditions.
Consider other STL containers like deque
or list
for specific use cases where vectors may not be optimal.
Use reserve()
to minimize memory fragmentation by pre-allocating the required capacity.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
numbers.reserve(1000); // Minimize fragmentation
for(int i = 0; i < 1000; i++) {
numbers.push_back(i);
}
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
return 0;
}
Console Output:
Size: 1000
Capacity: 1000
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies