C++如何使用迭代器从Set中删除一系列值
给定一个Set,任务是使用Iterator从该Set中删除一系列值。
例子:
输入:set = [10 20 30 40 50 60 70 80 90],
start_iterator = 3,
end_iterator = 8
输出:10 20 80 90
输入:set = [1 2 3 4 5],
start_iterator = 1,
end_iterator = 3
输出:3 4 5
C++
过程: 在这种方法中,从集合中删除一系列元素。这是通过两个迭代器的帮助完成的。第一个迭代器指向范围的起始元素,第二个迭代器指向范围的最后一个元素。第一个迭代器是 排他的 而最后一个迭代器是 全包含的 这意味着最后一个迭代器所指的元素也将被删除。
语法:
iterator erase (const_iterator startPositionIterator_exclusive,
const_iterator endingPositionIterator_inclusive);
C++
下面是以上方法的实现:
程序:
// C++ program to delete an element
// of a Set by passing its value
#include <iostream>
#include <set>
using namespace std;
// Function to print the set
void printSet(set<int> myset)
{
// Get the iterator
set<int>::iterator it;
// printing all the elements of the set
for (it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
// Function to delete the element of set
void deleteRange(set<int> myset)
{
// printing all the elements of the set
cout << "\nSet originally: ";
printSet(myset);
// Get the starting Iterator at 3rd element
set<int>::iterator start_itr = myset.begin();
start_itr++;
start_itr++;
// Get the ending Iterator at 2nd last element
set<int>::iterator end_itr = myset.end();
end_itr--;
end_itr--;
// Erase the elements in the range
// of the iterators passed as the parameter
myset.erase(start_itr, end_itr);
// printing all the elements of the set
cout << "Set after deletion of range"
<< " from 3rd till 2nd last: ";
printSet(myset);
}
// Driver code
int main()
{
set<int> myset;
// Get the set
for (int i = 1; i < 10; i++)
myset.insert(i * 10);
// Delete an element from the Set
deleteRange(myset);
return 0;
}
C++
Set originally: 10 20 30 40 50 60 70 80 90
Set after deletion of range from 3rd till 2nd last: 10 20 80 90
C++