jeudi 6 février 2020

Deleting empty elements from vector

I am trying to delete empty entries from std::vector. Here is a sample code, but something is wrong here.

#include <iostream>
#include <string>
#include<vector>
#include <cctype>

int main()
{
    std::vector<std::string> s1 = {"a"," ", "", "b","c","   ","d"};
    for (auto it = s1.begin(); it != s1.end() && isspace(*it); )
{
        it = s1.erase(it);
}

    std::cout<<"vector size = "<<s1.size();
    for (auto &i:s1) 
        std::cout<<i<<"\n";      

}

I am running a for loop to find out empty elements and deleting from there. There should be STL method too, but not sure how it will work.

Aucun commentaire:

Enregistrer un commentaire