###E###
Copyright: anatoliyurb Edited by snowman
constructors
Vector
#include
#include
#include
#include
using namespace std;
int main ()
{
string str[]={"Alex","John","Robert"};
// empty vector object
vector v1;
// creates vector with 10 empty elements
vector v2(10);
// creates vector with 10 elements,
// and assign value 0 for each
vector v3(10,0);
// creates vector and assigns
// values from string array
vector v4(str+0,str+3);
vector::iterator sIt = v4.begin();
while ( sIt != v4.end() )
cout << *sIt++ << " ";
cout << endl;
// copy constructor
vector 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
#include
#include
#include
using namespace std;
- 2 -
Copyright: anatoliyurb Edited by snowman
int main ()
{
int ary[]={1,2,3,4,5};
vector v;
// assign to the "v" the contains of "ary"
v.assign(ary,ary+5);
copy(v.begin(),v.end(),
ostream_iterator(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
#include
using namespace std;
int main ()
{
vector 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
#include
- 3 -
Copyright: anatoliyurb Edited by snowman
#include
#include
using namespace std;
template
class Member
{
public:
Member(T t, D d) : name(t), sal(d) {}
void print();
private:
T name;
D sal;
};
template
void Member::print()
{
cout << name << " " << sal << endl;
}
//======================================
int main ()
{
typedef Member M;
vector v;
v.push_back(M("Robert",60000));
v.push_back(M("Linda",75000));
vector::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
- 4 -
Copyright: anatoliyurb Edited by snowman
#include
#include
#include
#include
using namespace std;
int main ()
{
vector v(5);
iota(v.begin(),v.end(),1);
vector::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
#include
using namespace std;
int main ()
{
vector 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;
- 5 -
Copyright: anatoliyurb Edited by snowman
}
OUTPUT:
// Size of v = 10
// Capacity of v = 10
// After resizing:
// Size of v = 100
// Capacity of v = 100
clear
#include
#include
#include
using namespace std;
template
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//==============================
int main ()
{
vector v(10);
Print 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 ";
- 6 -
Copyright: anatoliyurb Edited by snowman
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
#include
using namespace std;
int main ()
{
vector 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
#include
#include
#include
using namespace std;
int main ()
{
vector v(5);
- 7 -
Copyright: anatoliyurb Edited by snowman
iota(v.begin(),v.end(),1);
vector
::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
#include
#include
#include
using namespace std;
int main ()
{
vector v(10);
vector::iterator It;
for ( int i=0; i<10; i++ )
v[i] = i+1;
copy(v.begin(),v.end(),
ostream_iterator(cout," "));
cout << endl;
It = v.begin()+2;
// remove third element
v.erase(It);
copy(v.begin(),v.end(),
ostream_iterator(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(cout," "));
cout << endl;
- 8 -