Array Inbuilt Functions in C++
C++ provides several built-in functions for working with arrays, making it easier and more efficient to manipulate and process data stored in arrays. These functions are part of the C++ Standard Template Library (STL) and offer a wide range of functionalities, from sorting and searching to filling and copying arrays.
Here are some of the most commonly used array inbuilt functions in C++:
1. std::fill()
This function fills all elements of an array with a specific value.
#include
#include
#include
int main() {
std::array arr = {1, 2, 3, 4, 5};
// Fill all elements with value 0
std::fill(arr.begin(), arr.end(), 0);
// Print the filled array
for (int i = 0; i < arr.size(); i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
0 0 0 0 0
2. std::copy()
This function copies elements from one array to another.
#include
#include
#include
int main() {
std::array source = {1, 2, 3, 4, 5};
std::array destination;
// Copy elements from source to destination
std::copy(source.begin(), source.end(), destination.begin());
// Print the copied array
for (int i = 0; i < destination.size(); i++) {
std::cout << destination[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
1 2 3 4 5
3. std::sort()
This function sorts elements of an array in ascending order.
#include
#include
#include
int main() {
std::array arr = {5, 2, 4, 1, 3};
// Sort the array
std::sort(arr.begin(), arr.end());
// Print the sorted array
for (int i = 0; i < arr.size(); i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
1 2 3 4 5
4. std::reverse()
This function reverses the order of elements in an array.
#include
#include
#include
int main() {
std::array arr = {1, 2, 3, 4, 5};
// Reverse the array
std::reverse(arr.begin(), arr.end());
// Print the reversed array
for (int i = 0; i < arr.size(); i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
5 4 3 2 1
5. std::search()
This function searches for a specific sequence of elements within an array.
#include
#include
#include
int main() {
std::array arr = {1, 2, 3, 4, 5, 6, 7};
std::array search_sequence = {3, 4, 5};
// Search for the sequence in the array
auto it = std::search(arr.begin(), arr.end(), search_sequence.begin(), search_sequence.end());
// If the sequence is found, print its position
if (it != arr.end()) {
std::cout << "Sequence found at position: " << std::distance(arr.begin(), it) << std::endl;
} else {
std::cout << "Sequence not found." << std::endl;
}
return 0;
}
Output:
Sequence found at position: 2
6. std::min_element()
and std::max_element()
These functions return iterators to the smallest and largest element in an array, respectively.
#include
#include
#include
int main() {
std::array arr = {5, 2, 4, 1, 3};
// Find the minimum and maximum elements
auto min_it = std::min_element(arr.begin(), arr.end());
auto max_it = std::max_element(arr.begin(), arr.end());
std::cout << "Minimum element: " << *min_it << std::endl;
std::cout << "Maximum element: " << *max_it << std::endl;
return 0;
}
Output:
Minimum element: 1
Maximum element: 5
7. std::unique()
This function removes consecutive duplicate elements from an array.
#include
#include
#include
int main() {
std::array arr = {1, 2, 2, 3, 3, 3, 4};
// Remove duplicate elements
auto new_end = std::unique(arr.begin(), arr.end());
// Print the array with unique elements
for (auto it = arr.begin(); it != new_end; ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Output:
1 2 3 4
These are just a few examples of the many built-in array functions available in C++. Using these functions can significantly simplify your code and improve its efficiency when working with arrays.