Guest User

C++ std::copy

a guest
Sep 28th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef vector<char> binary_data;
  9.  
  10. static struct binary_data_console
  11. {
  12.     static void are_equal(const string expected, const binary_data* actual)
  13.     {
  14.         static const char quote = '"';
  15.         const char* binary_data_c_str = actual->data();
  16.         cout << quote << expected << quote;
  17.         cout << (strcmp(expected.c_str(), binary_data_c_str) == 0 ? " == " : " != ");
  18.         cout << quote << binary_data_c_str << quote << endl;
  19.     }
  20.  
  21.     static void are_equal(const string expected, const shared_ptr<binary_data> actual)
  22.     {
  23.         are_equal(expected, actual.get());
  24.     }
  25.  
  26.     static void log(const binary_data* vec)
  27.     {
  28.         cout << "data: " << (char*)vec->data() << endl;
  29.         cout << "capacity: " << vec->capacity() << endl;
  30.         cout << "size: " << vec->size() << endl << endl;
  31.     }
  32.  
  33.     static void log(const shared_ptr<binary_data> vec)
  34.     {
  35.         log(vec.get());
  36.     }
  37. };
  38.  
  39. void pause()
  40. {
  41.     cout << "Press enter to continue ...";
  42.     cin.ignore();
  43. }
  44.  
  45. int main()
  46. {
  47.     shared_ptr<binary_data> smart_vec1(new binary_data({ 'A', 'u', 'd', 'i', '\0' }));
  48.     shared_ptr<binary_data> smart_vec2(new binary_data({ 'B', 'M', 'W', '\0' }));
  49.     smart_vec2->resize(smart_vec1->size());
  50.     std::copy(smart_vec1->begin(), smart_vec1->end(), smart_vec2->begin());
  51.     binary_data_console::are_equal("Audi", smart_vec2);
  52.     binary_data_console::log(smart_vec2);
  53.  
  54.     cout << "-------------------------------------------------" << endl << endl;
  55.  
  56.     binary_data* vec1 = new binary_data({ 'A', 'u', 'd', 'i', '\0' });
  57.     binary_data* vec2 = new binary_data({ 'B', 'M', 'W', '\0' });
  58.     vec2->resize(vec1->size());
  59.     std::copy(vec1->begin(), vec1->end(), vec2->begin());
  60.     binary_data_console::are_equal("Audi", vec2);
  61.     binary_data_console::log(vec2);
  62.  
  63.     delete vec1;
  64.     delete vec2;
  65.     pause();
  66. }
Add Comment
Please, Sign In to add comment