How to remove a range of elements from set in C++

2 Answers

0 votes
#include <iostream>       
#include <set> 
           
using namespace std; 
    
void printSet(set<int> st) {
    for (auto n: st) {
        cout << n << ' ' ; 
    } 
}
    
int main ()
{
    set<int> st;
    
    for (int i = 1; i < 10; i++) 
        st.insert(i);
  
    printSet(st); 
    
    st.erase(8);
    
    std::set<int>::iterator it;
    it = st.find(7);

    st.erase(it, st.end());
    
    cout << endl;
    printSet(st); 

    return 0;
} 
      
       
       
       
/*
run:
       
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 
    
*/

 



answered Apr 8, 2020 by avibootz
0 votes
#include <iostream>       
#include <set> 
           
using namespace std; 
    
void printSet(set<int> st) {
    for (auto n: st) {
        cout << n << ' ' ; 
    } 
}
    
int main ()
{
    set<int> st;
    
    for (int i = 1; i <= 10; i++) 
        st.insert(i);
  
    printSet(st); 
    
    st.erase(8);
    
    std::set<int>::iterator it1;
    it1 = st.find(7);

    std::set<int>::iterator it2;
    it2 = st.find(10);

    st.erase(it1, it2);
    
    cout << endl;
    printSet(st); 

    return 0;
} 
      
       
       
       
/*
run:
       
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 10 
    
*/

 



answered Apr 8, 2020 by avibootz

Related questions

1 answer 141 views
1 answer 187 views
1 answer 169 views
1 answer 140 views
...