// Demonstrate the use of the Standard Template Library "vector" class. // George F. Riley, ECE3090 Georgia Tech, Fall 2009 // A vector is a variable length array. It starts out as "zero" length // and grows or shrinks as needed. Further, the vector is a array // of any arbitrary type, using the C++ "templates" feature. #include #include //#include "GFRVec.h" // Uncomment to use the "buggy" one #include "GFRVec1.h" // Comment out this one if using the above one class A { public: A(); // Default constructor A(int); // Non-Default Constructor A(const A&); // A copy constructor is used by the compile whenever A& operator=(const A&); // Assignment operator ~A(); // Destructor public: int x; // Single data member }; typedef std::vector AVec_t; // Define a type that is vector of A's typedef std::vector APVec_t;// Define a type that is vector of A pointers // Uncomment to use our own implementation (and comment out above) //typedef GFRVec AVec_t; // Define a type that is vector of A's //typedef GFRVec APVec_t;// Define a type that is vector of A pointers A::A() { std::cout << "Hello from A::A() Default constructor" << std::endl; } A::A(int i) : x(i) { std::cout << "Hello from A::A(int) constructor" << std::endl; } A::A(const A& a) : x(a.x) { std::cout << "Hello from A Copy constructor" << std::endl; } A& A::operator=(const A& rhs) { std::cout << "Hello from A Assignment operator" << std::endl; x = rhs.x; } A::~A() { std::cout << "Hello from A Destructor" << std::endl; } //NewPage int main() { std::cout << "Creating A Vector"; getchar(); AVec_t av0; std::cout << "Adding an three elements to av0"; getchar(); av0.push_back(A(2)); // Elements are appended using "push_back" std::cout << "After first push_back"; getchar(); av0.push_back(A(10)); // Elements are appended using "push_back" std::cout << "After second push_back"; getchar(); av0.push_back(A(100)); // Elements are appended using "push_back" // Number of elements in a vector can be queried with "size()" std::cout << "After third push_back, size av0 is "<