C++ Tutorial/vector/vector push pop heap

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Demonstrating the STL vector push_back functions

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>
  3. include <string>
  4. include <algorithm> // for reverse

using namespace std;

int main() {

 string s("qwer");
 vector<char> vector1(s.begin(), s.end());
 vector<char> vector2;
 vector<char>::iterator i;
 for (i = vector1.begin(); i != vector1.end(); ++i)
   vector2.push_back(*i);
 for (i = vector2.begin(); i != vector2.end(); ++i)
   cout << *i;
 return 0;

}</source>

qwer

Perform the heapsort with push_heap and pop_heap

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <algorithm>
  2. include <vector>
  3. include <iterator>

int main() {

  int a[ 10 ] = { 3, 10, 2, 7, 4, 8, 1, 9, 5, 6 };
  std::vector< int > v( a, a + 10 ); // copy of a
  std::vector< int > v2;
  std::ostream_iterator< int > output( cout, " " );
  for ( int i = 0; i < 10; i++ )
  {
     v2.push_back( a[ i ] );
     std::push_heap( v2.begin(), v2.end() );
     cout << "\nv2 after push_heap(a[" << i << "]): ";
     std::copy( v2.begin(), v2.end(), output );
  }
  for ( int j = 0; j < v2.size(); j++ )
  {
     cout << "\nv2 after " << v2[ 0 ] << " popped from heap\n";
     std::pop_heap( v2.begin(), v2.end() - j );
     std::copy( v2.begin(), v2.end(), output );
  }
  cout << endl;
  return 0;

}</source>

v2 after push_heap(a[0]): 3
v2 after push_heap(a[1]): 10 3
v2 after push_heap(a[2]): 10 3 2
v2 after push_heap(a[3]): 10 7 2 3
v2 after push_heap(a[4]): 10 7 2 3 4
v2 after push_heap(a[5]): 10 7 8 3 4 2
v2 after push_heap(a[6]): 10 7 8 3 4 2 1
v2 after push_heap(a[7]): 10 9 8 7 4 2 1 3
v2 after push_heap(a[8]): 10 9 8 7 4 2 1 3 5
v2 after push_heap(a[9]): 10 9 8 7 6 2 1 3 5 4
v2 after 10 popped from heap
9 7 8 5 6 2 1 3 4 10
v2 after 9 popped from heap
8 7 4 5 6 2 1 3 9 10
v2 after 8 popped from heap
7 6 4 5 3 2 1 8 9 10
v2 after 7 popped from heap
6 5 4 1 3 2 7 8 9 10
v2 after 6 popped from heap
5 3 4 1 2 6 7 8 9 10
v2 after 5 popped from heap
4 3 2 1 5 6 7 8 9 10
v2 after 4 popped from heap
3 1 2 4 5 6 7 8 9 10
v2 after 3 popped from heap
2 1 3 4 5 6 7 8 9 10
v2 after 2 popped from heap
1 2 3 4 5 6 7 8 9 10
v2 after 1 popped from heap
1 2 3 4 5 6 7 8 9 10

pop_back from a vector

<source lang="cpp">#include <iostream>

  1. include <string>
  2. include <vector>

using namespace std; int main() {

   vector<string> v;
   v.push_back("A");
   v.push_back("B");
   v.push_back("C");
   cout << v.size() << endl;
   for (int i = 0; i < v.size(); ++i)
       cout << v[i] << endl;
   v[0] = "battle axe";
   for (int i = 0; i < v.size(); ++i)
       cout << v[i] << endl;
   cout << v[0] << endl;
   cout << v[0].size() << endl;
   v.pop_back();
   for (int i = 0; i < v.size(); ++i)
       cout << v[i] << endl;
   v.clear();
   if (v.empty())
        cout << "\nYou have nothing.\n";
   else
        cout << "\nYou have at least one item.\n";
   return 0;

}</source>

Using pop_back to Erase the Last Element

<source lang="cpp">#include <iostream>

  1. include <vector>

int main () {

   using namespace std;
   vector <int> v;
   v.push_back (50);
   v.push_back (1);
   v.push_back (987);
   v.push_back (1001);
   cout << "The vector contains ";
   cout << v.size ();
   cout << " elements before calling pop_back" << endl;
   // Erase one element at the end
   v.pop_back ();
   cout << "The vector contains ";
   cout << v.size ();
   cout << " elements after calling pop_back" << endl;
   cout << "Enumerating items in the vector... " << endl;
   unsigned int nElementIndex = 0;
   while (nElementIndex < v.size ())
   {
       cout << "Element at position " << nElementIndex << " is: ";
       cout << v [nElementIndex] << endl;
       // move to the next element
       ++ nElementIndex;
   }
   return 0;

}</source>