The erase function is available in multiple C++ STL containers, but its usage and behavior can vary slightly between them.
Here is an overview of how erase is used in different containers:
std::vector
Erase: Removes elements based on iterator position or range of iterators.
Usage:
erase(iterator): Removes the element at the specified position.
erase(iterator, iterator): Removes all elements in the specified range.
Example with std::vector:
cpp
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Erase element at position 2 (value 3)
vec.erase(vec.begin() + 2);
// Erase elements from position 1 to position 3 (values 2 and 4)
vec.erase(vec.begin() + 1, vec.begin() + 3);
for (const auto& elem : vec) {
std::cout << elem << " "; // Output: 1 5
}
std::cout << std::endl;
return 0;
}
std::deque
Erase: Similar to std::vector, it removes elements based on iterator position or range of iterators.
Usage:
erase(iterator): Removes the element at the specified position.
erase(iterator, iterator): Removes all elements in the specified range.
Example with std::deque:
cpp
#include <iostream>
#include <deque>
int main() {
std::deque<int> deq = {1, 2, 3, 4, 5};
// Erase element at position 2 (value 3)
deq.erase(deq.begin() + 2);
// Erase elements from position 1 to position 3 (values 2 and 4)
deq.erase(deq.begin() + 1, deq.begin() + 3);
for (const auto& elem : deq) {
std::cout << elem << " "; // Output: 1 5
}
std::cout << std::endl;
return 0;
}
std::list
Erase: Removes elements based on iterator position or range of iterators.
Usage:
erase(iterator): Removes the element at the specified position.
erase(iterator, iterator): Removes all elements in the specified range.
Example with std::list:
cpp
#include <iostream>
#include <list>
int main() {
std::list<int> lst = {1, 2, 3, 4, 5};
// Erase element at position 2 (value 3)
auto it = lst.begin();
std::advance(it, 2);
lst.erase(it);
// Erase elements from position 1 to position 3 (values 2 and 4)
auto it1 = lst.begin();
auto it2 = lst.begin();
std::advance(it1, 1);
std::advance(it2, 3);
lst.erase(it1, it2);
for (const auto& elem : lst) {
std::cout << elem << " "; // Output: 1 5
}
std::cout << std::endl;
return 0;
}
std::set
Erase: Removes elements based on the value or iterator position.
Usage:
erase(value): Removes the element with the specified value.
erase(iterator): Removes the element at the specified position.
erase(iterator, iterator): Removes all elements in the specified range.
Example with std::set:
cpp
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
// Erase element with value 3
mySet.erase(3);
// Erase elements from position 1 to position 3
auto it1 = mySet.find(2);
auto it2 = mySet.find(5);
mySet.erase(it1, it2);
for (const auto& elem : mySet) {
std::cout << elem << " "; // Output: 1 5
}
std::cout << std::endl;
return 0;
}
std::map
Erase: Removes elements based on the key or iterator position.
Usage:
erase(key): Removes the element with the specified key.
erase(iterator): Removes the element at the specified position.
erase(iterator, iterator): Removes all elements in the specified range.
Example with std::map:
cpp
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
// Erase element with key 3
myMap.erase(3);
// Erase elements from position 1 to position 3
auto it1 = myMap.find(2);
auto it2 = myMap.find(5);
myMap.erase(it1, it2);
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
// Output:
// 1: one
// 5: five
}
return 0;
}
Summary
std::vector, std::deque, std::list: erase can remove elements by iterator or range of iterators.
std::set, std::map: erase can remove elements by value (or key) or by iterator or range of iterators.
The erase function is versatile and commonly used across many STL containers, though the specifics of its usage depend on the
type of container.
To embed this project on your website, copy the following code and paste it into your website's HTML: