Partitioning a Range - C++ STL Algorithm

C++ examples for STL Algorithm:partition

Description

Partitioning a Range

Demo Code

#include <iostream>
#include <istream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>

using namespace std;
template<typename C>
void printContainer(const C& c, char delim = ',', ostream& out = cout) {
   printRange(c.begin(), c.end(), delim, out);
}

template<typename Fwd>
void printRange(Fwd first, Fwd last, char delim = ',', ostream& out = cout) {
   out << "{";
   while (first != last) {
      out << *first;//from  www .  j  a v a 2 s  .c  o m
      if (++first != last)
         out << delim << ' ';
   }
   out << "}" << endl;
}

int main() {

   cout << "Enter a series of strings: ";
   istream_iterator<string> start(cin);
   istream_iterator<string> end; // This creates a "marker"
   vector<string> v(start, end);

   vector<string>::iterator p = partition(v.begin(), v.end(), bind2nd(less<string>(), "foo"));
   printContainer(v);

   cout << "*p = " << *p << endl;
}

Result


Related Tutorials