Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. //#include <vector>        //replace this with your own class when ready
  4. #include "myVector.h"    //include this instead of the stl vector class when ready
  5. using namespace std;
  6.  
  7.  
  8. //Part 1:  Run the test code using the stl vector class.  Make sure you understand the output
  9. //           and how the vector class works.
  10. //Part 2:  Create your own vector class (in the file "myVector.h").  To test your vector class,
  11. //           Swap the #include statements above so that the test code uses your vector class instead of the stl vector class.
  12. //
  13. //           YOU MAY NOT CHANGE ANY PART OF THE TEST CODE other than swapping the included files.
  14. //         Your vector class must yield the exact same results from the test code as the stl vector class,
  15. //         with the following exceptions: The "capacity" method may return different values (ie, you may choose any resizing method you want,
  16. //           but you should consider what is the smartest approach with respect to efficiency).  Additionally, for the 0-parameter constructor and
  17. //           the 1-parameter, you do not need to initialize the values of the vector items (but you must for the 2-parameter constructor).
  18. //         Beyond these items, your code must behave the same as for the stl vector.
  19.  
  20. //template < class T>
  21.  
  22.  
  23. int main()
  24. {
  25.     vector<int> myvecA;   //ERROR OCCURS HERE
  26.     vector<int> myvecB(10); //ERROR OCCURS HERE
  27.     //vector<int> myvecC(5,-9);
  28.     //vector<string> myvecD(6,"Are we there yet?");
  29.    
  30.     //The size method should return how many items, abstractly,
  31.     //the vector currently holds.
  32.     cout << "Vector A size: " << myvecA.size() << endl;
  33.     cout << "Vector B size: " << myvecB.size() << endl;
  34. //    cout << "Vector C size: " << myvecC.size() << endl;
  35. //    cout << "Vector D size: " << myvecD.size() << endl;
  36.    
  37.     //Capacity should report how large the array holding the items is.
  38.     //This size will be at least that of 'size()', but could be larger.
  39.     cout << "Vector A capacity: " << myvecA.capacity() << endl;
  40.     cout << "Vector B capacity: " << myvecB.capacity() << endl;
  41. //    cout << "Vector C capacity: " << myvecC.capacity() << endl;
  42. //    cout << "Vector D capacity: " << myvecD.capacity() << endl;
  43.    
  44.     //You can access the items in the array
  45.     //with the '[]' operator.
  46.     cout << endl;
  47.     cout << "Vector B: " << endl;
  48.     myvecB[3] = 43;
  49.     myvecB[7] = 17;
  50.     for(int i=0; i<myvecB.size(); i++)
  51.         cout << myvecB[i] << endl;
  52.    
  53. //    cout << endl;
  54. //    cout << "Vector C: " << endl;
  55. //    myvecC[2] = 50;
  56. //    for(int i=0; i<myvecC.size(); i++)
  57. //        cout << myvecC[i] << endl;
  58. //    
  59. //    cout << endl;
  60. //    cout << "Vector D: " << endl;
  61. //    myvecD[5] = "Shut up kids.";
  62. //    for(int i=0; i<myvecD.size(); i++)
  63. //        cout << myvecD[i] << endl;
  64.    
  65.    
  66.     //An important ability of vectors is the ability to push items to the back
  67.     //of the vector, which may require increasing the capacity behind the scenes.
  68.     for(int i=0; i<16; i++)
  69.         myvecA.push_back(2380 + i);
  70.     cout << endl;
  71.     cout << "Vector A's size and capacity:" << endl;
  72.     cout << "Vector A size: " << myvecA.size() << endl;
  73.     cout << "Vector A capacity: " << myvecA.capacity() << endl;
  74.    
  75.    
  76.     //vector's also have full stack functionality.  Consider the "pop_back" method:
  77.     for(int i=0; i<10; i++)
  78.     {
  79.         cout << "About to pop: " << myvecA.back() << endl;
  80.         myvecA.pop_back();
  81.     }
  82.    
  83.     cout << endl;
  84.     cout << "Vector A's size and capacity:" << endl;
  85.     cout << "Vector A size: " << myvecA.size() << endl;
  86.     cout << "Vector A capacity: " << myvecA.capacity() << endl;
  87.    
  88.     //Here is some addtional demo of the [] operator and how it works,
  89.     //as well as the "push_back" method.
  90.     cout << endl;
  91.     for(int i=0; i<5; i++)
  92.         myvecB[i] = i*10;
  93.    
  94.     myvecB.push_back(9990);
  95.     myvecB.push_back(9991);
  96.     myvecB.push_back(9992);
  97.     myvecB.push_back(9993);
  98.     myvecB.push_back(9994);
  99.    
  100.     for(int i=0; i<myvecB.size(); i++)
  101.         cout << myvecB[i] << endl;
  102.    
  103.    
  104.     //Now we will see, for a large example, how the vector adjusts its capacity:
  105.     cout << endl;
  106.     int max = 100;
  107.     for(int i=0; i<max; i++)
  108.     {
  109.         int x = i*10000;
  110.         myvecB.push_back(x);
  111.         cout << "Pushing: " << x << ", size: " << myvecB.size() << ", capacity: " << myvecB.capacity() << endl;
  112.     }
  113.    
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement