#include #include #include using namespace std; vector vect; // insert at the end of the array for(unsigned long nCount=0; nCount < 6; nCount++) vect.push_back(10 - nCount); vect vector vec2{1,4,3,2,0} vector vec3{"abc","def","ghi"}; vec3[1] for(unsigned long nIndex=0; nIndex < vect.size(); nIndex++) cout << vect[nIndex] << " "; cout << endl; vect.at(12) vect[12] std::vector::iterator it1 = vect.begin(); *it1 it1++; *it1 auto it3 = vect.end(); --it3; *it3 // don't do this! *vect.end() std::vector myvector; // set some values (from 1 to 10) for(int i = 1; i <= 10; i++) myvector.push_back(i); // erase the 7th element myvector.erase(myvector.begin() + 6); // erase the first 3 elements: myvector.erase(myvector.begin(), myvector.begin() + 3); std::cout << "myvector contains:"; for(unsigned i = 0; i < myvector.size(); ++i) std::cout << ' ' << myvector[i]; std::cout << '\n'; std::vector vec(3, 100); vec // std::vector ::iterator it; auto it = vec.begin(); it = vec.insert(it, 200); vec vec.insert(it, 2, 300); vec // "it" no longer valid, get a new one: it = vec.begin(); std::vector othervec(2, 400); vec.insert(it + 2, othervec.begin(), othervec.end()); vec int myarray[] = { 501,502,503 }; vec.insert(vec.begin(), myarray, myarray + 3); std::cout << " vec contains:"; for(it = vec.begin(); it < vec.end(); it++) std::cout << ' ' << *it; std::vector myvector; // set some initial content: for(int i = 1; i < 10; i++) myvector.push_back(i); myvector myvector.resize(5); myvector myvector.resize(8, 100); myvector myvector.resize(12); std::cout << "myvector contains:"; for(int i = 0; i v; for (int i = 0; i < 10; i++) v.push_back(i); for(int i = 0;i < 10; i++) cout << "v[" << i << "]=" << v[i] << " "; cout << endl; // randomly reorder all the elements of the container v random_shuffle(v.begin(), v.end()); for(int i = 0;i < 10; i++) cout << "v[" << i << "]=" << v[i] << " "; cout << endl; // returns iterator positioned at the value 7 if exists between begin() and end() auto index = find(v.begin(), v.end(), 7); cout << "Location of 7 in new scrambled vector is " << std::distance(v.begin(), index) << endl; bool oddNum(int i) { return (i % 2 == 1); // return true; // else // return false; }// End of oddNum // returns the count of numbers that are odd between begin() and end() int n = count_if(v.begin(), v.end(), oddNum); cout << "There are " << n << " odd numbers." << endl; // sorting vector v3{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; sort(v3.begin(), v3.end()); v3 sort(v.begin(), v.end()); v