CPP/STL/Set/RemoveElement

From ProgrammingExamples
< CPP‎ | STL/Set
Jump to: navigation, search

RemoveElement.cpp

#include <iostream>
#include <set>
#include <algorithm>
 
void Output(std::set<unsigned int> S)
{
  // Output all of the elements in the set
  for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++)
  {
    std::cout << " " << *it1;
  }
  std::cout << std::endl;
}
 
int main()
{
  std::set<unsigned int> S;
 
  for(unsigned int i = 0; i < 10; i++)
    {
    S.insert(i);
    }
 
  Output(S);
 
  for(std::set<unsigned int>::iterator it1 = S.begin(); it1 != S.end(); it1++)
  {
    if(*it1 == 4)
    {
      S.erase(it1);
    }
  }
 
  Output(S);
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(RemoveElement)
 
ADD_EXECUTABLE(RemoveElement RemoveElement.cpp)