// test_vector.cpp #include #include #include #include using namespace std ; int vect1(void); int vect2(void); int vect3(void); int vect4(void); int vect5(void); int vect6(void); int vect7(void); int vect8(void); int vect9(void); int vect10(void); int vect11(void); typedef vector INTVECTOR; int main() { // Dynamically allocated vector begins with 0 elements. INTVECTOR theVector; // Add one element to the end of the vector, an int with the value 42. theVector.push_back(42) ; // Show statistics about vector. cout << "theVector's size is: " << theVector.size() << endl; cout << "theVector's maximum size is: " << theVector.max_size() << endl; cout << "theVector's capacity is: " << theVector.capacity() << endl; // Ensure there's room for at least 1000 elements. theVector.reserve(1000); cout << endl << "After reserving storage for 1000 elements:" << endl; cout << "theVector's size is: " << theVector.size() << endl; cout << "theVector's maximum size is: " << theVector.max_size() << endl; cout << "theVector's capacity is: " << theVector.capacity() << endl; // Ensure there's room for at least 2000 elements. theVector.resize(2000); cout << endl << "After resizing storage to 2000 elements:" << endl; cout << "theVector's size is: " << theVector.size() << endl; cout << "theVector's maximum size is: " << theVector.max_size() << endl; cout << "theVector's capacity is: " << theVector.capacity() << endl; cout << endl; vect1(); vect2(); vect3(); vect4(); vect5(); vect6(); vect7(); vect8(); vect9(); vect10(); vect11(); return 0; } int vect1() { vector v(5); int i, total; for(i=0; i<5; i++) v[i] = i; total = accumulate(v.begin(), v.end(), 0); cout << "vect1 accumulate Summation of v is: " << total << endl; cout << endl; return 0; } int vect2() { vector v(10), r(10); int i; for(i=0; i<10; i++) v[i] = i*2; cout << "vect2 Original sequence: "; for(i=0; i<10; i++) cout << v[i] << " "; cout << endl; adjacent_difference(v.begin(), v.end(), r.begin()); cout << "adjacent_difference Resulting sequence: "; for(i=0; i<10; i++) cout << r[i] << " "; cout << endl << endl; return 0; } // Demonstrate inner_product() int vect3() { vector v1(5), v2(5); int i, total; cout << "vect3" << endl; for(i=0; i<5; i++) v1[i] = i; for(i=0; i<5; i++) v2[i] = i+2; total = inner_product(v1.begin(), v1.end(), v2.begin(), 0); cout << "Inner product is: " << total << endl << endl; return 0; } int vect4() { vector v(5), r(5); int i; for(i=0; i<5; i++) v[i] = i; cout << "vect4 Original sequence: "; for(i=0; i<5; i++) cout << v[i] << " "; cout << endl; cout << "doing partial sum" << endl; partial_sum(v.begin(), v.end(), r.begin()); cout << "Resulting sequence: "; for(i=0; i<5; i++) cout << r[i] << " "; cout << endl << endl; return 0; } #include int vect5() { vector v(10); // create a vector of length 10 int i; // display original size of v cout << "vect5 Size = " << v.size() << endl; // assign the elements of the vector some values for(i=0; i<10; i++) v[i] = i + 'a'; // display contents of vector cout << "Current Contents:\n"; for(i=0; i v(10); // create a vector of length 10 vector::iterator p; // create an iterator int i; cout << "vect6" << endl; // assign elements in vector a value p = v.begin(); i = 0; while(p != v.end()) { *p = i + 'a'; p++; i++; } // display contents of vector cout << "Original contents:\n"; p = v.begin(); while(p != v.end()) { cout << *p << " "; p++; } cout << "\n\n"; // change contents of vector p = v.begin(); while(p != v.end()) { *p = toupper(*p); p++; } // display contents of vector cout << "Modified Contents:\n"; p = v.begin(); while(p != v.end()) { cout << *p << " "; p++; } cout << endl << endl; return 0; } // Demonstrate insert and erase. int vect7() { vector v(10); vector v2; char str[] = ""; int i; cout << "vect7" << endl; // initialize v for(i=0; i<10; i++) v[i] = i + 'a'; // copy characters in str into v2 for(i=0; str[i]; i++) v2.push_back(str[i]); // display original contents of vector cout << "Original contents of v:\n"; for(i=0; i::iterator p = v.begin(); p += 2; // point to 3rd element // insert 10 X's into v v.insert(p, 10, 'X'); // display contents after insertion cout << "Size after inserting X's = " << v.size() << endl; cout << "Contents after insert:\n"; for(i=0; i class DailyTemp { int temp; public: DailyTemp() { temp = 0; } DailyTemp(int x) { temp = x; } DailyTemp &operator=(int x) { temp = x; return *this; } double get_temp() { return temp; } }; bool operator<(DailyTemp a, DailyTemp b) { return a.get_temp() < b.get_temp(); } bool operator==(DailyTemp a, DailyTemp b) { return a.get_temp() == b.get_temp(); } int vect8() { vector v; int i; cout << "vect8" << endl; for(i=0; i<7; i++) v.push_back(DailyTemp(60 + rand()%30)); cout << "Farenheit temperatures:\n"; for(i=0; i v; int i; cout << "vect9" << endl; for(i=0; i < 10; i++) { if(rand() % 2) v.push_back(true); else v.push_back(false); } cout << "Sequence:\n"; for(i=0; i v, v2(30); int i; cout << "vect10" << endl; for(i=0; str[i]; i++) v.push_back(str[i]); // **** demonstrate remove_copy **** cout << "Input sequence:\n"; for(i=0; i v; // create zero-length int vector vector cv(5); // create 5-element char vector vector cv2(5, 'x'); // initialize a 5-element char vector vector iv2(v); // create int vector from an int vector vector v3(v.begin(), v.end()); // create a vector from iterators int i; cout << "vect11" << endl; for(i=0; i<10; i++) v.push_back(i); cout << "Initial: "; for(i=0; i