This is a continuation from previous tutorial, compiled usingVC++7.0/.Net, win32 empty console mode application. g++ examples given at the end of this Module. The source code for this tutorial is available inC++ STL Container source code.
|
Continuation from the previous Module...
// vector, max_size() #include <vector> #include <iostream> using namespace std;
int main() { vector <int> vec1; vector <int>::size_type i; i = vec1.max_size(); cout<<"The max possible length of the vector is "<<i<<endl; return 0; }
Output:
// vector, pop_back(), back(), and push_back() #include <vector> #include <iostream> using namespace std;
int main() { vector <int> vec1;
vec1.push_back(4); vec1.push_back(7); vec1.push_back(3);
cout<<"vec1 data: "; for(int i=0; i<vec1.size(); ++i) cout<<vec1[i]<<' '; cout<<endl; cout<<"\nOperation: vec1.back()\n"; cout<<vec1.back()<<endl; cout<<"\nOperation: push_back(2)\n"; vec1.push_back(2); cout<<vec1.back()<<endl; cout<<"New vec1 data: "; for(i=0; i<vec1.size(); ++i) cout<<vec1[i]<<' '; cout<<endl; cout<<"\nOperation: vec1.pop_back()\n"; vec1.pop_back(); cout<<vec1.back()<<endl; cout<<"New vec1 data: "; for(i=0; i<vec1.size(); ++i) cout<<vec1[i]<<' '; cout<<endl; return 0; }
|
The return value is a reverse random-access iterator addressing the first element in a reversed vector or addressing what had been the last element in the un reversed vector.
If the return value of rbegin() is assigned to a const_reverse_iterator, the vector object cannot be modified. If the return value of rbegin() is assigned to a reverse_iterator, the vector object can be modified.
// vector, rbegin()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator vec1_Iter;
vector <int>::reverse_iterator vec1_rIter;
vec1.push_back(10);
vec1.push_back(7);
vec1.push_back(3);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"\nOperation: vec1.begin()\n";
vec1_Iter = vec1.begin();
cout<<"The first element of vec1 is "<<*vec1_Iter<<endl;
cout<<"\nOperation: vec1.rbegin()\n";
vec1_rIter = vec1.rbegin();
cout<<"The first element of the reversed vec1 is "<<*vec1_rIter<<endl;
return 0;
}
The return value is a reverse random-access iterator that addresses the location succeeding the last element in a reversed vector (the location that had preceded the first element in the un-reversed vector).
rend() is used with a reversed vector just as end() is used with a vector.
If the return value of rend() is assigned to aconst_reverse_iterator, then the vector object cannot be modified. If the return value of rend() is assigned to a reverse_iterator, then the vector object can be modified.
rend() can be used to test to whether a reverse iterator has reached the end of its vector.
The value returned by rend() should not be dereferenced.
// vector, rend() and rbegin()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::reverse_iterator vec1_rIter;
vec1.push_back(7);
vec1.push_back(3);
vec1.push_back(4);
vec1.push_back(1);
cout<<"Operation: vec1.rbegin() and vec1.rend()\n";
cout<<"vec1 data: ";
for(vec1_rIter = vec1.rbegin(); vec1_rIter != vec1.rend(); vec1_rIter++)
cout<<*vec1_rIter<<' ';
cout<<endl;
return 0;
}
If the container's size is less than the requested size,_Newsize, elements are added to the vector until it reaches the requested size.
If the container's size is larger than the requested size, the elements closest to the end of the container are deleted until the container reaches the size _Newsize. If the present size of the container is the same as the requested size, no action is taken.
size() reflects the current size of the vector.
// vector, resize() and size()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(40);
vec1.push_back(20);
vec1.push_back(10);
vec1.push_back(12);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
// resize to 5 and add data at the end...
cout<<"\nOperation: vec1.resize(5,30)\n";
vec1.resize(5,30);
cout<<"The size of vec1 is "<<vec1.size()<<endl;
cout<<"The value of the last object is "<<vec1.back()<<endl;
cout<<"\nNew vec1 data: ";
for(i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"\nOperation: vec1.resize(4)\n";
vec1.resize(4);
cout<<"\nNew vec1 data: ";
for(i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"\nThe new size of vec1 is "<<vec1.size()<<endl;
cout<<"The value of the last object is "<<vec1.back()<<endl;
return 0;
}
![]() |
The return value is the current length of the vector.
// vector, reserve(), capacity() and size()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(4);
vec1.push_back(2);
vec1.push_back(10);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"\nOperation: vec1.capacity()"<<endl;
cout<<"Current capacity of vec1 = "<<vec1.capacity()<<endl;
cout<<"\nOperation: vec1.reserve(10)"<<endl;
vec1.reserve(10);
cout<<"Current capacity of vec1 = "<<vec1.capacity()<<endl;
cout<<"\nOperation: vec1.size()"<<endl;
cout<<"Current size of vec1 = "<<vec1.size()<<endl;
return 0;
}
// vector, swap()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2;
vec1.push_back(4);
vec1.push_back(7);
vec1.push_back(2);
vec1.push_back(12);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
vec2.push_back(11);
vec2.push_back(21);
vec2.push_back(30);
cout<<"vec2 data: ";
for(i=0; i<vec2.size(); ++i)
cout<<vec2[i]<<' ';
cout<<endl;
cout<<"The number of elements in vec1 = "<<vec1.size()<<endl;
cout<<"The number of elements in vec2 = "<<vec2.size()<<endl;
cout<<endl;
cout<<"Operation: vec1.swap(vec2)\n"<<endl;
vec1.swap(vec2);
cout<<"The number of elements in v1 = "<<vec1.size()<<endl;
cout<<"The number of elements in v2 = "<<vec2.size()<<endl;
cout<<"vec1 data: ";
for(i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"vec2 data: ";
for(i=0; i<vec2.size(); ++i)
cout<<vec2[i]<<' ';
cout<<endl;
return 0;
}
Operator | Description |
operator[ ] | Returns a reference to the vector element at a specified position. |
Table 27.6 |
The return value is, if the position specified is greater than the size of the container, the result is undefined.
If the return value of operator[ ] is assigned to a const_reference, the vector object cannot be modified. If the return value of operator[ ] is assigned to a reference, the vector object can be modified.
// vector operator[ ]
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(10);
vec1.push_back(9);
vec1.push_back(8);
vec1.push_back(12);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"Operation: int& j = vec1[2]\n";
int& j = vec1[2];
cout<<"The third integer of vec1 is "<<j<<endl;
return 0;
}
Specialization | Description |
vector<bool> | A full specialization of the template class vector for elements of type bool with an allocator for the underlying type used by the specialization. |
Table 27.7 |
Typedef | Description |
const_iterator | A type that describes an object that can serve as a constant random-access iterator for the sequence of Boolean elements contained by the vector. |
const_pointer | A type that describes an object that can serve as a constant pointer to a Boolean element of the sequence contained by the vector. |
const_reference | A type that describes an object that can serve as a constant reference to a Boolean element of the sequence contained by the vector. |
iterator | A type that describes an object that can serve as a random-access iterator for a sequence of Boolean elements contained by a vector. |
pointer | A type that describes an object that can serve as a constant pointer to a Boolean element of the sequence contained by the vector. |
Table 27.8 |
Member function | Description |
flip() | Reverses all bits in the vector. |
swap() | Exchanges the elements of two vectors with Boolean elements. |
Table 27.9 |
// vector_bool, flip()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
_Bvector vecbool;
vecbool.push_back(1);
vecbool.push_back(0);
cout<<"The vector is: "<<vecbool.front()<<" "<<vecbool.back()<<endl;
cout<<"\nOperation: vecbool.flip()\n";
vecbool.flip();
cout<<"The flipped vector is: "<<vecbool.front()<<" "<<vecbool.back()<<endl;
return 0;
}
// vector_bool, swap()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
_Bvector vec1, vec2;
vec1.push_back(0);
vec1.push_back(0);
vec2.push_back(1);
vec2.push_back(1);
cout<<"The vector vec1 is: "<<vec1.front()<<" "<<vec1.back()<<endl;
cout<<"The vector vec2 is: "<<vec2.front()<<" "<<vec2.back()<<endl;
cout<<"Operation: swap(vec1, vec2);\n";
swap(vec1, vec2);
cout<<"After swapping, vec1 is: "<<vec1.front()<<" "<<vec1.back()<<endl;
cout<<"After swapping, vec2 is: "<<vec2.front()<<" "<<vec2.back()<<endl;
return 0;
}
Nested class | Description |
vector<bool> reference | A nested class whose objects are able to provide references to elements (single bits) within avector<bool> object. |
Table 27.10 |
------------------------------------------End of C++ STL vector programming-----------------------------------
---www.tenouk.com---
The source code for this tutorial is available inC++ STL Container source code.
Acomplete C++ Standard Library documentation that includes STL.
Check thebest selling C / C++ books at Amazon.com.