C++/Data Structure/Vector Utility — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:24, 25 мая 2010

Another way to sort a sequence into descending order.

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
  vector<char> vectorObject(26);
  int i;
  for(i = 0; i <vectorObject.size(); i++) 
     vectorObject[ i ] = "A"+i;
  cout << "Original ordering of vectorObject:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  
  sort(vectorObject.begin(), vectorObject.end(), not2(less<char>()));   // sort into desceding order
  cout << "After sorting vectorObject using not2(less<char>()):";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  return 0;
}


A vector may allocate more memory than it currently needs.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
  vector<char> vectorObject(10);
  cout << "Initial size: " << vectorObject.size() << endl;
  cout << "Initial capacity: " << vectorObject.capacity();
  cout << "\n\n";
 
  vectorObject.push_back("X");
  cout << "Size after push_back: " << vectorObject.size() << endl;
  cout << "New capacity: " << vectorObject.capacity();
  cout << "\n\n";
  vectorObject.resize(100);
  cout << "Size after resize: " << vectorObject.size() << endl;
  cout << "Capacity after resize: " << vectorObject.capacity();
  cout << "\n\n";
  vectorObject.push_back("Y");
  cout << "Size after push_back: " << vectorObject.size() << endl;
  cout << "New capacity: " << vectorObject.capacity();
  cout << "\n\n";
  return 0;
}


Demonstrate back_insert_iterator in vector

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
  vector<int> vectorObject, vectorObject2;
  vector<int>::iterator itr;
  int i;
  for(i = 0; i <5; i++)
    vectorObject.push_back(i);
  cout << "Original contents of vectorObject: ";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  cout << endl;

  back_insert_iterator<vector<int> > bck_i_itr(vectorObject);   // create a back_insert_iterator to vectorObject
  *bck_i_itr++ = 100;        // insert rather than overwrite at end
  *bck_i_itr = 200;
  cout << "vectorObject after insertion: ";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  cout << endl;
  cout << "Size of vectorObject2 before copy: " << vectorObject2.size() << endl;
  copy(vectorObject.begin(), vectorObject.end(), back_inserter(vectorObject2));   // copy vectorObject to vectorObject2 using back inserter
  cout << "Size of vectorObject2 after copy: " << vectorObject2.size() << endl;
  cout << "Contents of vectorObject2 after insertion: ";
  itr = vectorObject2.begin();
  while(itr != vectorObject2.end())
    cout << *itr++ << " ";
  return 0;
}


Demonstrate binary_search() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject;
  bool result;
  int i;
  for(i = 0; i <10; i++)
    vectorObject.push_back("A" + i);
  cout << "Contents of vectorObject: ";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ];
  cout << endl;
  
  cout << "Looking for F.";
  result = binary_search(vectorObject.begin(), vectorObject.end(), "F");
  if(result)
    cout << "F Found";
  else
    cout << "F Not Found";
  cout << "Looking for X.\n";
  result = binary_search(vectorObject.begin(), vectorObject.end(), "X");
  if(result)
    cout << "X Found\n";
  else
    cout << "X Not Found\n";
  return 0;
}


Demonstrate count_if and not1 in vector

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject;
  int num;
  int i;
  for(i = 0; i <26; i++) 
     vectorObject.push_back(i+"A");
  cout << "Sequence contains:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  num = count_if(vectorObject.begin(), vectorObject.end(), bind2nd(greater<int>(), "r"));
  cout << "There are " << num;
  cout << " elements greater than E.\n";
  num = count_if(vectorObject.begin(), vectorObject.end(), not1(bind2nd(greater<int>(), "r")));
  cout << "There are " << num;
  cout << " elements not greater than E.";
  return 0;
}


Demonstrate find() and find_if() in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
bool iscomma(char ch) {
  if(ch == ",") 
     return true;
  return false;
}
int main()
{
  vector<char> vectorObject;
  vector<char>::iterator p;
  char str[] = "One, Two, Three, Four, Five, Six";
  int i;
  for(i = 0; i < strlen(str); i++)
    vectorObject.push_back(str[ i ]);
  cout << "Contents of vectorObject: ";
  for(i = 0; i < vectorObject.size(); i++)
    cout << vectorObject[ i ];
  cout << endl;
  
  p = find(vectorObject.begin(), vectorObject.end(), "T");          // find the first T
  cout << "Sequence beginning with T: ";
  while(p != vectorObject.end())
    cout << *p++;
  cout << endl;
  p = find_if(vectorObject.begin(), vectorObject.end(), iscomma);   // find first comma
  cout << "After find first comma: ";
  while(p != vectorObject.end())
    cout << *p++;
  cout << endl;
  return 0;
}


Demonstrate front_insert_iterator in vector

#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main()
{
  list<int> vectorObject, vectorObject2;
  list<int>::iterator itr;
  int i;
  for(i = 0; i <5; i++)
    vectorObject.push_back(i);
  cout << "Original contents of vectorObject: ";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  cout << endl;
  front_insert_iterator<list<int> > frnt_i_itr(vectorObject);  // create a front_insert_iterator to vectorObject

  *frnt_i_itr++ = 100;   // insert rather than overwrite at front
  *frnt_i_itr = 200;
  cout << "vectorObject after insertion: ";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  cout << endl;
  cout << "Size of vectorObject2 before copy: " << vectorObject2.size() << endl;
  copy(vectorObject.begin(), vectorObject.end(), front_inserter(vectorObject2));
  cout << "Size of vectorObject2 after copy: " << vectorObject2.size() << endl;
  cout << "Contents of vectorObject2 after insertion: ";
  itr = vectorObject2.begin();
  while(itr != vectorObject2.end())
    cout << *itr++ << " ";
  return 0;
}


Demonstrate insert_iterator in a vector

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
  vector<int> vectorObject;
  vector<int>::iterator itr;
  int i;
  for(i = 0; i <5; i++)
    vectorObject.push_back( i );
  cout << "Original contents of vectorObject: ";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  cout << endl;
  itr = vectorObject.begin();
  itr += 2; // point to element 2

  insert_iterator<vector<int> > i_itr(vectorObject, itr);  // create insert_iterator to element 2
 
  *i_itr++ = 100;    // insert rather than overwrite
  *i_itr = 200;
  cout << "vectorObject after insertion: ";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  return 0;
}


Demonstrate lower_bound() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject;
  vector<char>::iterator p;
  int i;
  for(i = 0; i <10; i++)
    vectorObject.push_back("A" + i);
  cout << "Contents of vectorObject: ";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ];
  cout << endl;
  cout << "Looking for F...";
  p = lower_bound(vectorObject.begin(), vectorObject.end(), "F");
  if(p != vectorObject.end())
    cout << *p << " Found";
  else
    cout << "F Not Found";
  cout << "\n Looking for X.";
  p = lower_bound(vectorObject.begin(), vectorObject.end(), "X");
  if(p != vectorObject.end())
    cout << *p << " Found";
  else
    cout << "X Not Found";
  return 0;
}


Demonstrate mismatch() in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <utility>
using namespace std;
int main()
{
  vector<char> vectorObject, vectorObject2;
  pair<vector<char>::iterator, vector<char>::iterator> p;
  char str1[] = "O, T, T, T";
  char str2[] = "O, T, F, F, N";
  int i;
  for(i = 0; i <strlen(str1); i++)
    vectorObject.push_back(str1[ i ]);
  for(i = 0; i <strlen(str2); i++)
    vectorObject2.push_back(str2[ i ]);
  cout << "Contents of vectorObject:  ";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ];
  cout << endl;
  cout << "Contents of vectorObject2: ";
  for(i = 0; i <vectorObject2.size(); i++)
    cout << vectorObject2[ i ];
  cout << endl;

  p = mismatch(vectorObject.begin(), vectorObject.end(), vectorObject2.begin());   // find the first mismatch
  if(p.first != vectorObject.end()) {                        // mismatch found
    cout << "The character " << *p.first;
    cout << " in vectorObject mismatches the character ";
    cout << *p.second << " in vectorObject2.";
  }
  return 0;
}


Demonstrate random_shuffle() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject;
  int i;
  for(i = 0; i <26; i++) 
     vectorObject.push_back("A"+i);
  cout << "Original contents of vectorObject:\n";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ];
  cout << endl;
  random_shuffle(vectorObject.begin(), vectorObject.end());
  cout << "Shuffled contents of vectorObject:\n";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ];
  return 0;
}


Demonstrate search() in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
  vector<char> vectorObject, vectorObject2;
  vector<char>::iterator p;
  char str1[] = "O, T, T, T";
  char str2[] = "T";
  int i;
  for(i = 0; i <strlen(str1); i++)
    vectorObject.push_back(str1[ i ]);
  for(i = 0; i <strlen(str2); i++)
    vectorObject2.push_back(str2[ i ]);
  cout << "Contents of vectorObject: ";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ];
  cout << endl;
  p = search(vectorObject.begin(), vectorObject.end(), vectorObject2.begin(), vectorObject2.end()); // find the first Two
  cout << "Sequence beginning with T: ";
  
  while(p != vectorObject.end())
    cout << *p++;
  cout << endl;
  return 0;
}


Demonstrating partial_sort() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void init_vectorObject(vector<int> &vectorObject)
{
  vectorObject[0] = 1;
  vectorObject[1] = 10;
  vectorObject[2] = 2;
  vectorObject[3] = -3;
  vectorObject[4] = 9;
  vectorObject[5] = 4;
  vectorObject[6] = 8;
  vectorObject[7] = 5;
  vectorObject[8] = 7;
  vectorObject[9] = 6;
}
int main()
{
  vector<int> vectorObject(10);
  int i;
  init_vectorObject(vectorObject);
  cout << "Original order:";
  for(i = 0; i <10; i++) 
    cout << vectorObject[ i ] << " ";
  cout << endl;
  partial_sort(vectorObject.begin(), vectorObject.begin()+5, vectorObject.end());
  cout << "Order after partial sorting:";
  for(i = 0; i <10; i++) 
    cout << vectorObject[ i ] << " ";
  cout << endl;
  return 0;
}


Demonstrating remove() and replace() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject;
  vector<char>::iterator p, p_end;
  int i;
  for(i = 0; i <5; i++) {
    vectorObject.push_back("A"+i);
    vectorObject.push_back("A"+i);
    vectorObject.push_back("A"+i);
  }
  cout << "Original contents of vectorObject:";
  for(p = vectorObject.begin(); p < vectorObject.end(); p++)
    cout << *p << " ";
  cout << endl;
  
  p_end = remove(vectorObject.begin(), vectorObject.end(), "C");    // remove all C"s
  cout << "Sequence after removing all C"s:";
  for(p = vectorObject.begin(); p < p_end; p++)
    cout << *p << " ";
  cout << endl;

  replace(vectorObject.begin(), vectorObject.end(), "D", "X");     // replace D"s with digits X"s
  cout << "Sequence after replacement:";
  for(p = vectorObject.begin(); p < p_end; p++)
    cout << *p << " ";
  cout << endl;
  return 0;
}


Demonstrating sort() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void init_v(vector<int> &vectorObject)
{
  vectorObject[0] = 1;
  vectorObject[1] = 10;
  vectorObject[2] = 2;
  vectorObject[3] = -3;
  vectorObject[4] = 9;
  vectorObject[5] = 4;
  vectorObject[6] = 8;
  vectorObject[7] = 5;
  vectorObject[8] = 7;
  vectorObject[9] = 6;
}
int main()
{
  vector<int> vectorObject(10);
  int i;
  // sort the entire container
  init_v(vectorObject);
  cout << "Sort an entire container.";
  cout << "Original order:";
  for(i = 0; i <10; i++) 
    cout << vectorObject[ i ] << " ";
  cout << endl;
  sort(vectorObject.begin(), vectorObject.end());
  cout << "Order after sorting container:";
  for(i = 0; i <10; i++) 
    cout << vectorObject[ i ] << " ";
  cout << endl;
  // sort a subset of the container
  init_v(vectorObject); 
  
  cout << "Sort a subset of a container.";
  cout << "Original order:";
  for(i = 0; i <10; i++) 
    cout << vectorObject[ i ] << " ";
  cout << endl;
  sort(vectorObject.begin() + 2, vectorObject.end() - 2);
  cout << "Order after sorting vectorObject[2] through vectorObject[7]:";
  for(i = 0; i <10; i++) 
    cout << vectorObject[ i ] << " ";
  cout << endl;
  return 0;
}


Demonstrating unique() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject;
  vector<char>::iterator p, p_end;
  int i;
  for(i = 0; i <5; i++) {
    vectorObject.push_back("A"+i);
    vectorObject.push_back("A"+i);
    vectorObject.push_back("A"+i);
  }
  cout << "Original contents of vectorObject:\n";
  for(p = vectorObject.begin(); p < vectorObject.end(); p++)
    cout << *p << " ";
  cout << endl;
  
  p_end = unique(vectorObject.begin(), vectorObject.end());       // remove duplicates
  cout << "Sequence after removing duplicates:\n";
  for(p = vectorObject.begin(); p < p_end; p++)
    cout << *p << " ";
  cout << endl;
  return 0;
}


Exchange elements from two different types of containers.

#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject(10);
  deque<char> deq(10);
  int i;
  for(i = 0; i < 10; i++) 
     vectorObject[ i ] = "A" + i;
  for(i = 0; i < 10; i++) 
     deq[ i ] = "0" + i;
  cout << "Original contents of vectorObject:";
  for(i = 0; i < 10; i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  
  cout << "Original contents of deq:";
  for(i = 0; i < 10; i++)
    cout << deq[ i ] << " ";
  cout << endl;
  // swap ranges in vectorObject and deq
  swap_ranges(vectorObject.begin() + 2, vectorObject.end() - 3, deq.begin() + 4);
  cout << "Contents of vectorObject after swap:";
  for(i = 0; i < 10; i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  
  cout << "Contents of deq after swap:";
  for(i = 0; i < 10; i++)
    cout << deq[ i ] << " ";
  cout << endl;
  return 0;
}


Find minimum and maximum inside a vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<int> vectorObject(5);
  int i;
  vectorObject[0] = 1;
  vectorObject[1] = 2;
  vectorObject[2] = 5;
  vectorObject[3] = 9;
  vectorObject[4] = 2;
  cout << "Contents of vectorObject: ";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  cout << "Maximum element is: ";
  cout << *max_element(vectorObject.begin(), vectorObject.end());
  cout << endl;
  cout << "Minimum element is: ";
  cout << *min_element(vectorObject.begin(), vectorObject.end());
  return 0;
}


Generating a sequence in vector

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

double f()                  // A simple generator function.
{
  static double val = 1.0;
  double t;
  t = 1.0 / val;
  val += val;
  return t;
}
int main()
{
  vector<double> vectorObject(5);
  int i;

  generate(vectorObject.begin(), vectorObject.end(), f);     // generate a series
  cout << "Series: ";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  return 0;
}


Insert one vector into another using an insert iterator

#include <iostream>
#include <iterator>
#include <vector>
#include <string>
using namespace std;
int main()
{
  vector<string> vectorObject, vectorObject2;
  vector<string>::iterator itr;
  vectorObject.push_back("T");
  vectorObject.push_back("S");
  vectorObject.push_back("A");
  vectorObject.push_back("P");
  vectorObject2.push_back("A");
  vectorObject2.push_back("I");
  vectorObject2.push_back("S");
  cout << "Original size of vectorObject: " << vectorObject.size() << endl;
  cout << "Original contents of vectorObject:\n";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  cout << endl;
  copy(vectorObject2.begin(), vectorObject2.end(), inserter(vectorObject, vectorObject.begin() + 2));
  cout << "Size of vectorObject after insertion: ";
  cout << vectorObject.size() << endl;
  cout << "Contents of vectorObject after insertion:";
  itr = vectorObject.begin();
  while(itr != vectorObject.end())
    cout << *itr++ << " ";
  return 0;
}


Merge two sequences

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject1, vectorObject2, vectorObject3(26);
  int i;
  for(i = 0; i <26; i+=2) 
      vectorObject1.push_back("A"+i);
  for(i = 0; i <26; i+=2) 
      vectorObject2.push_back("B"+i);
  cout << "Original contents of vectorObject1:";
  for(i = 0; i <vectorObject1.size(); i++)
    cout << vectorObject1[ i ];
  cout << endl;
  cout << "Original contents of vectorObject2:";
  for(i = 0; i <vectorObject2.size(); i++)
    cout << vectorObject2[ i ];
  cout << endl;
  
  merge(vectorObject1.begin(), vectorObject1.end(),    // merge sequences
        vectorObject2.begin(), vectorObject2.end(),
        vectorObject3.begin());
  cout << "Result of merge:\n";
  for(i = 0; i <vectorObject3.size(); i++)
    cout << vectorObject3[ i ];
  return 0;
}


Right-rotate a sequence in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<int> vectorObject;
  vector<int>::iterator p;
  int i;
  
  for(i = 0; i <10; i++) 
     vectorObject.push_back(i);
  cout << "Original ordering: ";
  for(p=vectorObject.begin(); p<vectorObject.end(); p++)
    cout << *p << " ";
  cout << endl;
  // rotate right two positions using reverse iterators
  rotate(vectorObject.rbegin(), vectorObject.rbegin()+2, vectorObject.rend());
  cout << "Order after two right rotates: ";
  for(p=vectorObject.begin(); p<vectorObject.end(); p++)
    cout << *p << " ";
  cout << endl;
  return 0;
}


Rotate a sequence in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<int> vectorObject;
  vector<int>::iterator p;
  int i;
  
  for(i = 0; i <10; i++) 
     vectorObject.push_back(i);
  cout << "Original ordering: ";
  for(p=vectorObject.begin(); p<vectorObject.end(); p++)
    cout << *p << " ";
  cout << endl;
  rotate(vectorObject.begin(), vectorObject.begin()+1, vectorObject.end());   // rotate left one position
  cout << "Order after left rotate: ";
  for(p=vectorObject.begin(); p<vectorObject.end(); p++)
    cout << *p << " ";
  cout << endl;
  return 0;
}


set_union, set_difference, set_symmetric_difference, set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject1, vectorObject2, v_res(26), vectorObject3;
  vector<char>::iterator p, pres_end;
  int i;
  for(i = 0; i <20; i++) 
      vectorObject1.push_back("A"+i);
  for(i=10; i <26; i++) 
      vectorObject2.push_back("A"+i);
  cout << "Contents of vectorObject1:\n";
  for(i = 0; i <vectorObject1.size(); i++)
    cout << vectorObject1[ i ];
  cout << endl;
  cout << "Contents of vectorObject2:\n";
  for(i = 0; i <vectorObject2.size(); i++)
    cout << vectorObject2[ i ];
  cout << endl;
  pres_end = set_union(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());
  cout << "Set union: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;
  pres_end = set_difference(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());
  cout << "Set difference: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;
  pres_end = set_symmetric_difference(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());
  cout << "Set symmetric difference: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;
  pres_end = set_intersection(vectorObject1.begin(), vectorObject1.end(), vectorObject2.begin(), vectorObject2.end(), v_res.begin());
  cout << "Set intersection: ";
  for(p=v_res.begin(); p!=pres_end; p++)
    cout << *p;
  cout << endl;
  // includes 
  vectorObject3.push_back("A");
  vectorObject3.push_back("C");
  vectorObject3.push_back("D");
  if(includes(vectorObject1.begin(), vectorObject1.end(), vectorObject3.begin(), vectorObject3.end()))
      cout << "vectorObject1 includes all of vectorObject3.";
  else
      cout << "vectorObject3 contains elements not found in vectorObject1.";
  return 0;
}


Sorting a vector into descending order in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
  vector<char> vectorObject(26);
  int i;
  for(i = 0; i <vectorObject.size(); i++) 
     vectorObject[ i ] = "A"+i;
  cout << "Original ordering of vectorObject:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  // sort into desceding order
  sort(vectorObject.begin(), vectorObject.end(), greater<char>());
  cout << "After sorting vectorObject using greater():";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
    
  return 0;
}


Transforming a sequence in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
double midpoint(double a, double b)
{
  return ((a-b) / 2) + b;
}
int main()
{
  vector<double> vectorObject1(5), vectorObject2(5), vectorObject3(5);
  int i;
  vectorObject1[0] = 110.0;
  vectorObject1[1] = 918.6;
  vectorObject1[2] = 112.23;
  vectorObject1[3] = 818.8;
  vectorObject1[4] = -212.01;
  vectorObject2[0] = 21.0;
  vectorObject2[1] = 31.3;
  vectorObject2[2] = 41.19;
  vectorObject2[3] = 155.0;
  vectorObject2[4] = -2.0;
  cout << "Values in vectorObject1: ";
  for(i = 0; i <vectorObject1.size(); i++)
    cout << vectorObject1[ i ] << " ";
  cout << endl;
  cout << "Values in vectorObject2: ";
  for(i = 0; i <vectorObject2.size(); i++)
    cout << vectorObject2[ i ] << " ";
  cout << endl;
  
  transform(vectorObject1.begin(), // find midpoints between elements in vectorObject1 those in vectorObject2
            vectorObject1.end(), 
            vectorObject2.begin(),
            vectorObject3.begin(), 
            midpoint);
  cout << "Midpoints: ";
  for(i = 0; i <vectorObject3.size(); i++)
    cout << vectorObject3[ i ] << " ";
  return 0;
}


Use a function adaptor in vector

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
using namespace std;
int main()
{
  vector<char *> vectorObject;
  vector<char *>::iterator p;
  int i;
  vectorObject.push_back("One");
  vectorObject.push_back("Two");
  vectorObject.push_back("Three");
  vectorObject.push_back("Four");
  vectorObject.push_back("Five");
  cout << "Sequence contains:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  cout << "Searching sequence for Three.\n";
  // use a pointer-to-function adaptor
  p = find_if(vectorObject.begin(), vectorObject.end(), not1(bind2nd(ptr_fun(strcmp), "Three")));
  if(p != vectorObject.end()) {
    cout << "Found.";
    cout << "Sequence from that point is:";
    do {
      cout << *p++ << " ";
    } while (p != vectorObject.end());
  }
  return 0;
}


Use a unary function object to determine even/odd.

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class isEven: public unary_function<int, bool> {
public:
  result_type operator()(argument_type i)
  { 
    return (result_type) !(i%2);
  }
};
int main()
{
  vector<int> vectorObject;
  int i;
  for(i = 1; i < 20; i++) 
     vectorObject.push_back(i);
  cout << "Sequence:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  i = count_if(vectorObject.begin(), vectorObject.end(), isEven());
  cout << i << " numbers are evenly divisible by 2.";
  return 0;
}


Use for_each() in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(int i)
{
  cout << i << " ";
}
int main()
{
  vector<int> vectorObject;
  int i;
  for(i = 0; i <10; i++) 
     vectorObject.push_back(i);
  cout << "Contents of vectorObject: ";
  for_each(vectorObject.begin(), vectorObject.end(), show);
  cout << endl;
  return 0;
}


Use the logical_not unary function object in vector

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
  vector<bool> vectorObject;
  int i;
  for(i = 1; i < 10; i++) 
     vectorObject.push_back((bool)(i % 2));
  cout << boolalpha;                // turn on boolalpha I/O flag
  cout << "Original contents of vectorObject:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
 
  // use the logical_not function object
  transform(vectorObject.begin(), vectorObject.end(), vectorObject.begin(), logical_not<bool>()); 
  cout << "Inverted contents of vectorObject:";
  for(i = 0; i <vectorObject.size(); i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  return 0;
}


Using copy() in Vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject(10);
  vector<char> vectorObject2(10);
  int i;
  for(i = 0; i < 10; i++) 
     vectorObject[ i ] = "A" + i;
  for(i = 0; i < 10; i++) 
     vectorObject2[ i ] = "z";
  
  copy(vectorObject.begin(), vectorObject.end(), vectorObject2.begin());   // copy all of vectorObject into vectorObject2
  cout << "Contents of vectorObject2 after copy:\n";
  for(i = 0; i < 10; i++)
    cout << vectorObject2[ i ] << " ";
  cout << endl;
  
  for(i = 0; i <10; i++)                  // re-initialize vectorObject2
     vectorObject2[ i ] = "z";
  
  copy(vectorObject.begin() + 2, vectorObject.end() - 2, vectorObject2.begin()); // now copy just part of vectorObject into vectorObject2
  cout << "Contents of vectorObject2 after subsequence copy:\n";
  for(i = 0; i <10; i++)
    cout << vectorObject2[ i ] << " ";
  cout << endl;

  for(i = 0; i <10; i++)                  // re-initialize vectorObject2
     vectorObject2[ i ] = "z";
  // now copy part of vectorObject into middle of vectorObject2
  copy(vectorObject.begin() + 2, vectorObject.end() - 2, vectorObject2.begin() + 3);
  cout << "Contents of vectorObject2 after copy into middle:\n";
  for(i = 0; i <10; i++)
    cout << vectorObject2[ i ] << " ";
  cout << endl;
  return 0;
}


Using swap_ranges() in Vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  vector<char> vectorObject(10);
  vector<char> vectorObject2(10);
  int i;
  for(i = 0; i < 10; i++) 
     vectorObject[ i ] = "A" + i;
  for(i = 0; i < 10; i++) 
     vectorObject2[ i ]  = "0" + i;
  cout << "Original contents of vectorObject:";
  for(i = 0; i < 10; i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  
  cout << "Original contents of vectorObject2:";
  for(i = 0; i < 10; i++)
    cout << vectorObject2[ i ] << " ";
  cout << endl;
  // swap ranges in vectorObject and vectorObject2
  swap_ranges(vectorObject.begin() + 2, vectorObject.end() - 3, vectorObject2.begin() + 4);
  cout << "Contents of vectorObject after swap:";
  for(i = 0; i < 10; i++)
    cout << vectorObject[ i ] << " ";
  cout << endl;
  
  cout << "Contents of vectorObject2 after swap:";
  for(i = 0; i < 10; i++)
    cout << vectorObject2[ i ] << " ";
  cout << endl;
  return 0;
}