jeudi 28 novembre 2019

Erasing all elements till the first occurrence of an element in a set in C++ STL

I need to delete all the elements from beginning to the first occurrence of the variable c in a set, But it's not working. What am I doing wrong? need some help.

#include<iostream>
#include<set>
using namespace std;
int main(){

    set<char> s;
    char c = 'y';

    s.insert('y');
    s.insert('n');
    s.insert('a');

    set<char>::iterator it = s.find(c);
    s.erase(s.begin(),it);
    s.insert(c);

    for(auto a : s){
        cout<<a<<" ";
    }

    return 0;
}

Output: 'y'

It is supposed to give : 'n' 'a' 'y' right?

Aucun commentaire:

Enregistrer un commentaire