remove empty string : remove « STL Algorithms Modifying sequence operations « C++






remove empty string

  
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

void removeEmptyStrings(vector<string>& strings)
{
  vector<string>::iterator it = remove_if(strings.begin(), strings.end(),mem_fun_ref(&string::empty));
  // erase the removed elements
  strings.erase(it, strings.end());
}

void printString(const string& str)
{
  cout << str << " ";
}

int main(int argc, char** argv)
{
  vector<string> myVector;
  myVector.push_back("A");
  myVector.push_back("B");
  myVector.push_back("C");
  myVector.push_back("D");
  myVector.push_back("E");
  myVector.push_back("F");

  removeEmptyStrings(myVector);
  cout << "Size is " << myVector.size() << endl;
  for_each(myVector.begin(), myVector.end(), &printString);
  cout << endl;
  return (0);
}
  
    
  








Related examples in the same category

1.Use the generic remove algorithm
2.Use std::remove to delete all element in a vector by value
3.Remove an element and then erase that element
4.Combine remove and erase together
5.Remove all instances of '0'
6.Remove copy Elements if They Meet a Criterion