C++/Vector/vector push pop heap

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

Demonstrating the STL vector push_back functions

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <vector>
  4. include <string>
  5. 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;

} /* qwer

*/        
   
   
 </source>


Inserting Elements in a vector Using the push_back Method

<source lang="cpp">

  1. include <iostream>
  2. include <vector>

int main () {

   std::vector <int> v;
   // Insert sample integers into the vector
   v.push_back (50);
   v.push_back (1);
   v.push_back (987);
   v.push_back (1001);
   std::cout << v.size () << " Elements";
   return 0;

}


 </source>


Perform the heapsort with push_heap and pop_heap

<source lang="cpp">

  1. 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;

} /* 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

*/        
   
   
 </source>


Read data from a file and save that to vector

<source lang="cpp">

  1. include <algorithm>
  2. include <fstream>
  3. include <iomanip>
  4. include <iostream>
  5. include <iterator>
  6. include <numeric>
  7. include <vector>

using namespace std; int main( ) {

   ifstream stock_file( "data.txt" );
   vector<float> price( (istream_iterator<float>( stock_file )),istream_iterator<float>() );
   vector<float> percent_change( price.size() );
   adjacent_difference( price.begin(), price.end(),percent_change.begin() );
   copy( price.begin(), price.begin()+5,ostream_iterator<float>( cout,  "  " ) );

}


 </source>