constructors

#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main () { string str[]={"Alex","John","Robert"}; // empty vector object vector<int> v1; // creates vector with 10 empty elements vector<int> v2(10); // creates vector with 10 elements, // and assign value 0 for each vector<int> v3(10,0); // creates vector and assigns // values from string array vector<string> v4(str+0,str+3); vector<string>::iterator sIt = v4.begin(); while ( sIt != v4.end() ) cout << *sIt++ << " "; cout << endl; // copy constructor vector<string> v5(v4); for ( int i=0; i<3; i++ ) cout << v5[i] << " "; cout << endl; return 0; } OUTPUT: // Alex John Robert // Alex John Robert assign
#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main () { int ary[]={1,2,3,4,5}; vector<int> v; // assign to the "v" the contains of "ary" v.assign(ary,ary+5); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; // replace v for 3 copies of 100 v.assign(3,100); copy(v.begin(),v.end(), ostream_iterator(cout," ")); cout << endl; return 0; } OUTPUT: // 1 2 3 4 5 // 100 100 100 at
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v(3,0); v[0] = 100; v.at(1) = 200; for ( int i=0; i<3; i++ ) cout << v.at(i) << " "; cout << endl; return 0; } OUTPUT: // 100 200 0 back
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; template<class T, class D> class Member { public: Member(T t, D d) : name(t), sal(d) {} void print(); private: T name; D sal; }; template<class T, class D> void Member::print() { cout << name << " " << sal << endl; } //====================================== int main () { typedef Member<string,double> M; vector<M> v; v.push_back(M("Robert",60000)); v.push_back(M("Linda",75000)); vector<M>::iterator It = v.begin(); cout << "Entire vector:" << endl; while ( It != v.end() ) (It++)->print(); cout << endl; cout << "Return from back()" << endl; v.back().print(); return 0; } OUTPUT: // Entire vector: // Robert 60000 // Linda 75000 // // Return from back() // Linda 75000 begin
#include <iostream> #include <vector> #include <iterator> #include <numeric> using namespace std; int main () { vector<int> v(5); iota(v.begin(),v.end(),1); vector<int>::iterator It = v.begin(); while ( It != v.end() ) cout << *It++ << " "; cout << endl; // third element of the vector It = v.begin()+2; cout << *It << endl; return 0; } OUTPUT: // 1 2 3 4 5 // 3 capacity
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v(10); cout << "Size of v = " << v.size() << endl; cout << "Capacity of v = " << v.capacity() << endl; v.resize(100); cout << "After resizing:" << endl; cout << "Size of v = " << v.size() << endl; cout << "Capacity of v = " << v.capacity() << endl; return 0; } OUTPUT: // Size of v = 10 // Capacity of v = 10 // After resizing: // Size of v = 100 // Capacity of v = 100 clear
#include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================== int main () { vector<int> v(10); Print<int> print; fill(v.begin(),v.end(),5); cout << "Vector v : "; for_each(v.begin(),v.end(),print); cout << endl; cout << "Size of v = " << v.size() << endl; cout << "v.clear" << endl; v.clear(); cout << "Vector v : "; for_each(v.begin(),v.end(),print); cout << endl; cout << "Size of v = " << v.size() << endl; cout << "Vector v is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; return 0; } // Vector v : 5 5 5 5 5 5 5 5 5 5 // Size of v = 10 // v.clear // Vector v : // Size of v = 0 // Vector v is empty empty
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v; cout << "Vector is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; v.push_back(100); cout << "Vector is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; return 0; } // Vector is empty // Vector is not empty end
#include <iostream> #include <vector> #include <iterator> #include <numeric> using namespace std; int main () { vector<int> v(5); iota(v.begin(),v.end(),1); vector<int>::iterator It = v.begin(); while ( It != v.end() ) cout << *It++ << " "; cout << endl; // last element of the vector It = v.end()-1; cout << *It << endl; return 0; } OUTPUT: // 1 2 3 4 5 // 5 erase
#include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; int main () { vector<int> v(10); vector<int>::iterator It; for ( int i=0; i<10; i++ ) v[i] = i+1; copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; It = v.begin()+2; // remove third element v.erase(It); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; It = v.begin(); // remove 2 elements from beginning fo v v.erase(It,It+2); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; return 0; } OUTPUT: // 1 2 3 4 5 6 7 8 9 10 // 1 2 4 5 6 7 8 9 10 // 4 5 6 7 8 9 10 front
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; template<class T, class D> class Member { public: Member(T t, D d) : name(t), sal(d) {} void print(); private: T name; D sal; }; template<class T, class D> void Member::print() { cout << name << " " << sal << endl; } //====================================== int main () { typedef Member<string,double> M; vector<M> v; v.push_back(M("Linda",75000)); v.push_back(M("Robert",60000)); vector<M>::iterator It = v.begin(); cout << "Entire vector:" << endl; while ( It != v.end() ) (It++)->print(); cout << endl; cout << "Return from front()" << endl; v.front().print(); return 0; } OUTPUT: // Entire vector: // Linda 75000 // Robert 60000 // // Return from front() // Linda 75000 insert
#include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { int ary[5]; fill(ary,ary+5,1); vector<int> v; vector<int>::iterator It; Print<int> print; copy(ary,ary+5, back_inserter(v)); cout << "vector v : "; for_each(v.begin(),v.end(),print); cout << endl; It = v.begin(); // insert value "5" at the position "It" cout << "v.insert(It,5) : "; v.insert(It,5); for_each(v.begin(),v.end(),print); cout << endl; // insert range ary+2 - ary+5 at the position "It" It = v.begin()+5; cout << "v.insert(It,ary+2,ary+5 : "; v.insert(It,ary+2,ary+5); for_each(v.begin(),v.end(),print); cout << endl; // insert 2 value of "20" at the position "It" It = v.end()-2; cout << "v.insert(It,2,20) : "; v.insert(It,2,20); for_each(v.begin(),v.end(),print); cout << endl; return 0; } OUTPUT: // vector v : 1 1 1 1 1 // v.insert(It,5) : 5 1 1 1 1 1 // v.insert(It,ary+2,ary+5 : 5 1 1 1 1 1 1 1 1 // v.insert(It,2,20) : 5 1 1 1 1 1 1 20 20 1 1 max_size
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v(10); cout << "Size of v = " << v.size() << endl; cout << "Max_size of v = " << v.max_size() << endl; return 0; } OUTPUT: // Size of v = 10 // Max_size of v = 1073741823 pop_back
#include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { vector<int> v; Print<int> print; for ( int i=0; i<5; i++ ) v.push_back(i+1); while ( !v.empty() ) { for_each(v.begin(),v.end(),print); cout << endl; v.pop_back(); } return 0; } OUTPUT: // 1 2 3 4 5 // 1 2 3 4 // 1 2 3 // 1 2 // 1 push_back
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; template <class T> class Name { public: Name(T t) : name(t) {} void print() { cout << name << " "; } private: T name; }; //============================= int main () { typedef Name<string> N; typedef vector<N> V; V v; N n1("Robert"); N n2("Alex"); v.push_back(n1); v.push_back(n2); // unnamed object of the type Name v.push_back(N("Linda")); V::iterator It = v.begin(); while ( It != v.end() ) (It++)->print(); cout << endl; return 0; } OUTPUT: // Robert Alex Linda rbegin and rend
#include <iostream> #include <iomanip> #include <vector> #include <string> #include <algorithm> #include <iterator> using namespace std; class ID { friend bool operator < ( const ID&, const ID& ); public: ID(string name,int score) : name(name), score(score) {} void display () { cout.setf(ios::left); cout << setw(3) << score << name << endl; } private: string name; int score; }; //----------------------------------------------------- // comperation function for sorting bool operator < ( const ID& a, const ID& b ) { return a.score < b.score; } //----------------------------------------------------- typedef vector<ID> Vector; // new name for existing datatype int main () { Vector v; Vector::iterator Iter; v.push_back(ID("Smith A",96)); v.push_back(ID("Amstrong B.",91)); v.push_back(ID("Watson D.",82)); for ( Iter = v.begin(); Iter != v.end(); Iter++ ) Iter->display(); sort(v.begin(),v.end()); // sort algorithm cout << endl << "Sorted by Score" << endl; cout << "===============" << endl; for ( Iter = v.begin(); Iter != v.end(); Iter++ ) Iter->display(); cout << endl << "Reverse output" << endl; cout << "===============" << endl; Vector::reverse_iterator r = v.rbegin(); while ( r != v.rend() ) cout << r->display(); cout << endl; return 0; } OUTPUT: // 96 Smith A. // 91 Amstrong B. // 82 Watson D. // // Sorted by Score // =============== // 82 Watson D. // 91 Amstrong B. // 96 Smith A. // // Reverse output // =============== // 96 Smith A. // 91 Amstrong B. // 82 Watson D. reserve
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v(5,0); // 5 elements, each - value 0 /*------------------------------------------------*/ cout << "Size of v = " << v.size() << endl; cout << "Capacity v = " << v.capacity() << endl; cout << "Value of each element is - "; for ( int i = 0; i < v.size(); i++ ) cout << v[i] << " "; cout << endl; v[0] = 5; // new value for first element v[1] = 8; v.push_back(3); // creates new (6th) element of vector, v.push_back(7); // automatically increases size cout << endl; // capacity of vector v cout << "Size of v = " << v.size() << endl; cout << "Capacity v = " << v.capacity() << endl; cout << "Value of each element is - "; for ( int i = 0; i < v.size(); i++ ) cout << v[i] << " "; cout << endl << endl; v.reserve(100); // increase capacity to 100 cout << "Size of v1_int = " << v.size() << endl; cout << "Capacity v1_int = " << v.capacity() << endl; int size = sizeof(v); // how big is vector itself cout << "sizeof v = " << size << endl; return 0; } OUTPUT: // Size of v = 5 // Capacity v = 5 // Value of each element is - 0 0 0 0 0 // // Size of v = 7 // Capacity v = 10 // Value of each element is - 5 8 0 0 0 3 7 // // Size of v = 7 // Capacity v = 100 // sizeof v = 12 resize
#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main () { vector<int> v(5); for ( int i=0; i<5; i++ ) v[i] = i*2; copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; v.resize(7,100); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; v.resize(4); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; return 0; } OUTPUT: // 0 2 4 6 8 // 0 2 4 6 8 100 100 // 0 2 4 6 size
#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { vector<char> v(5); Print<char> print; cout << "Size of v = " << v.size() << endl; fill(v.begin(),v.end(),'*'); for_each(v.begin(),v.end(),print); cout << endl; for ( int i=0; i < v.size(); i++ ) cout << v[i] << " "; cout << endl; for ( int i=0; i<5; i++ ) { cout << "Size of v = "; for_each(v.begin(),v.end(),print); cout << endl; v.pop_back(); } return 0; } OUTPUT: // Size of v = 5 // * * * * * // * * * * * // Size of v = * * * * * // Size of v = * * * * // Size of v = * * * // Size of v = * * // Size of v = * swap
#include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { int ary[] = {1,2,3,4,5,6,7,8,9,10}; Print print; vector<int> v1(ary,ary+7); vector<int> v2(ary+7,ary+10); cout << "Vector v1 : "; for_each(v1.begin(),v1.end(),print); cout << endl; cout << "Size of v1 = " << v1.size() << endl << endl; cout << "Vector v2 : "; for_each(v2.begin(),v2.end(),print); cout << endl; cout << "Size of v2 = " << v2.size() << endl << endl; v1.swap(v2); cout << "After swapping:" << endl; cout << "Vector v1 : "; for_each(v1.begin(),v1.end(),print); cout << endl; cout << "Size of v1 = " << v1.size() << endl << endl; cout << "Vector v2 : "; for_each(v2.begin(),v2.end(),print); cout << endl; cout << "Size of v2 = " << v2.size() << endl << endl; return 0; } OUTPUT: // Vector v1 : 1 2 3 4 5 6 7 // Size of v1 = 7 // // Vector v2 : 8 9 10 // Size of v2 = 3 // // After swapping: // Vector v1 : 8 9 10 // Size of v1 = 3 // // Vector v2 : 1 2 3 4 5 6 7 // Size of v2 = 7