Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,936 questions

51,873 answers

573 users

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 122 views
1 answer 171 views
1 answer 154 views
1 answer 131 views
...