Program examples compiled using VC++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 in C++ STL Container source code.
The Standard Template Library (STL) is a generic library that provides solutions to managing collections of data with an efficient algorithm.
The STL provides a collection of classes that meet different kind of tasks, with algorithms that operate on the classes. STL components are templates, as you have learned in Module 24, it can be used for arbitrary data types.
Furthermore, STL also provides a framework for other collection of user defined classes or algorithms. The traditional programming such as dynamic arrays, linked lists, binary trees, search algorithms and other data structures routines can be implemented using STL more efficiently and easily.
To easily understand this topic, you should have good understanding of the traditional arrays data type and operations that can be done on arrays elements such as comparison, sorting, deletion, modification, insertion etc. and as well as templates.
|
|
// vector constructors #include <vector> #include <iostream> using namespace std;
int main() { vector <int>::iterator vec0Iter, vec1Iter, vec2Iter, vec3Iter, vec4Iter, vec5Iter;
// create an empty vector vec0 vector <int> vec0; // create a vector vec1 with 10 elements of default value 0 vector <int> vec1(10); // create a vector vec2 with 7 elements of value 13 vector <int> vec2(7, 13); // create a vector vec3 with 5 elements of value 3 and with the allocator of vector vec2 vector <int> vec3(5, 3, vec2.get_allocator()); // vector vec4, a copy of vector vec2 vector <int> vec4(vec2); // create a vector vec5 by copying the range of vec4[_First, _Last) vector <int> vec5(vec4.begin() + 1, vec4.begin() + 3); cout<<"Operation: vector <int> vec0\n"; cout<<"vec0 data: "; for(vec0Iter = vec0.begin(); vec0Iter != vec0.end(); vec0Iter++) cout<<" "<<*vec0Iter; cout<<endl; cout<<"\nOperation: vector <int> vec1(10)\n"; cout<<"vec1 data: "; for(vec1Iter = vec1.begin(); vec1Iter != vec1.end(); vec1Iter++) cout<<" "<<*vec1Iter; cout<<endl; cout<<"\nOperation: vector <int> vec2(7, 13)\n"; cout<<"vec2 data: "; for(vec2Iter = vec2.begin(); vec2Iter != vec2.end(); vec2Iter++) cout<<" "<<*vec2Iter; cout<<endl; cout<<"\nOperation: vector <int> vec3(5, 3, vec2.get_allocator())\n"; cout<<"vec3 data: "; for(vec3Iter = vec3.begin(); vec3Iter != vec3.end(); vec3Iter++) cout<<" "<<*vec3Iter; cout<<endl; cout<<"\nOperation: vector <int> vec4(vec2)\n"; cout<<"vec4 data: "; for(vec4Iter = vec4.begin(); vec4Iter != vec4.end(); vec4Iter++) cout<<" "<<*vec4Iter; cout<<endl; cout<<"\nOperation: vector <int> vec5(vec4.begin()+1, vec4.begin()+3)\n"; cout<<"vec5 data: "; for(vec5Iter = vec5.begin(); vec5Iter != vec5.end(); vec5Iter++) cout<<" "<<*vec5Iter; cout<<endl; return 0; }
Output:
|
After erasing any existing elements in a vector, assign() either inserts a specified range of elements from the original vector into a vector or inserts copies of a new element of a specified value into a vector.
// vector, assign()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec2;
vector <int>::iterator Iter;
vec2.push_back(1);
vec2.push_back(5);
vec2.push_back(3);
vec2.push_back(4);
vec2.push_back(5);
vec2.push_back(3);
vec2.push_back(7);
vec2.push_back(8);
vec2.push_back(4);
cout<<"Operation: vec2.begin() and vec2.end()"<<endl;
cout<<"vec2 data: ";
for(Iter = vec2.begin(); Iter != vec2.end(); Iter++)
cout<<*Iter<<" ";
cout<<"\n\n";
cout<<"Operation: vec2.assign(vec2.begin()+1, vec2.begin()+3)"<<endl;
vec2.assign(vec2.begin()+2, vec2.begin()+8);
cout<<"vec2 data: ";
for(Iter = vec2.begin(); Iter != vec2.end(); Iter++)
cout<<*Iter<<" ";
cout<<"\n\n";
cout<<"Operation: vec2.assign(5, 7)"<<endl;
vec2.assign(5, 7);
cout<<"vec2 data: ";
for(Iter = vec2.begin(); Iter != vec2.end(); Iter++)
cout<<*Iter<<" ";
cout<<endl;
return 0;
}

The return value is a reference to the element subscripted in the argument. If _Off is greater than the size of the vector, at() throws an exception.
If the return value of at() is assigned to a const_reference, the vector object cannot be modified. If the return value of at() is assigned to a reference, the vector object can be modified.
// vector, at()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(1);
vec1.push_back(7);
vec1.push_back(4);
vec1.push_back(3);
// print all elements separated by a space
cout<<"The vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<"\n\nOperation: vec1.at(position)";
const int &x = vec1.at(1);
int &y = vec1.at(3);
int &z = vec1.at(0);
cout<<"\nThe 2nd element is "<<x<<endl;
cout<<"The 4th element is "<<y<<endl;
cout<<"The 1st element is "<<z<<endl;
return 0;
}
|
|
// vector, back() and front()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2;
vec1.push_back(12);
vec1.push_back(10);
vec1.push_back(7);
// print all elements separated by a space
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
int& x = vec1.back();
const int& y = vec1.front();
cout<<"\nOperation: x = vec1.back()\n";
cout<<"The last integer of vec1 is "<<x<<endl;
cout<<"Operation: y = vec1.front()\n";
cout<<"The 1st integer of vec1 is "<<y<<endl;
return 0;
}

The return value is a random-access iterator addressing the first element in the vector or to the location succeeding an empty vector.
If the return value of begin() is assigned to a const_iterator, the vector object cannot be modified. If the return value of begin() is assigned to an iterator, the vector object can be modified.
// vector, begin()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator vec1_Iter;
vec1.push_back(21);
vec1.push_back(12);
vec1.push_back(32);
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_Iter = 10\n";
*vec1_Iter = 10;
cout<<"new vec1 data: ";
for(i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"Operation: vec1.begin()\n";
vec1_Iter = vec1.begin();
cout<<"The first element of vec1 is now "<<*vec1_Iter<<endl;
return 0;
}

The return value is the current length of storage allocated for the vector.
The member function resize() will be more efficient if sufficient memory is allocated to accommodate it. Use a member function reserve() to specify the amount of memory allocated.
// vector, capacity() and size()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(3);
vec1.push_back(1);
vec1.push_back(6);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"Operation: vec1.capacity()\n";
cout<<"The length of storage allocated is "<<vec1.capacity()<<"."<<endl;
vec1.push_back(10);
vec1.push_back(12);
vec1.push_back(4);
cout<<"\nnew vec1 data: ";
for(i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"The length of storage allocated is now "<<vec1.capacity()<<"."<<endl;
return 0;
}

For empty(), the return value is true if the vector is empty; false if the vector is not empty.
// vector, clear(), empty() and size()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vec1.push_back(10);
vec1.push_back(20);
vec1.push_back(30);
cout<<"vec1 data: ";
for(int i=0; i<vec1.size(); ++i)
cout<<vec1[i]<<' ';
cout<<endl;
cout<<"The size of vec1 is "<<vec1.size()<<endl;
cout<<"\nOperation: vec1.empty()"<<endl;
if(vec1.empty())
cout<<"vec1 is empty"<<endl;
else
cout<<"vec1 is not empty"<<endl;
cout<<"\nOperation: vec1.clear()"<<endl;
vec1.clear();
cout<<"The size of vec1 after clearing is "<<vec1.size()<<endl;
cout<<"\nOperation: vec1.empty()"<<endl;
if(vec1.empty())
cout<<"vec1 is empty"<<endl;
else
cout<<"vec1 is not empty"<<endl;
return 0;
}
-----------------------------------------------------------------------------------------

For end(), the return value is a pointer to the end of the vector object. If the vector is empty, the result is undefined.
If the return value of end() is assigned to a variable of type const_iterator, the vector object cannot be modified. If the return value of end() is assigned to a variable of type iterator, the vector object can be modified.

The return value for erase() is an iterator that designates the first element remaining beyond any elements removed, or a pointer to the end of the vector if no such element exists.
// vector, erase(), begin() and end()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter;
vec1.push_back(3);
vec1.push_back(7);
vec1.push_back(22);
vec1.push_back(5);
vec1.push_back(12);
vec1.push_back(17);
cout<<"Original vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
cout<<"\nOperation: erase(vec1.begin()"<<endl;
vec1.erase(vec1.begin());
cout<<"New vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
cout<<"\nOperation: vec1.erase(vec1.begin()+1, vec1.begin()+3)"<<endl;
vec1.erase(vec1.begin() + 1, vec1.begin() + 3);
cout<<"New vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
return 0;
}

The return value is the first insert() function returns an iterator that point to the position where the new element was inserted into the vector.
// vector, insert(), begin(), end()
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter;
vec1.push_back(12);
vec1.push_back(100);
vec1.push_back(9);
vec1.push_back(21);
cout<<"Original vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
cout<<"\nOperation: vec1.insert(vec1.begin()+1, 17)"<<endl;
vec1.insert(vec1.begin()+1, 17);
cout<<"New vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
cout<<"\nOperation: vec1.insert(vec1.begin()+2, 3, 24)"<<endl;
vec1.insert(vec1.begin()+2, 3, 24);
cout<<"New vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
cout<<"\nOperation: vec1.insert(vec1.begin()+1, \n"
" vec1.begin()+2, vec1.begin()+4)"<<endl;
vec1.insert(vec1.begin()+1, vec1.begin()+2, vec1.begin()+4);
cout<<"New vec1 data: ";
for(Iter = vec1.begin(); Iter != vec1.end(); Iter++)
cout<<" "<<*Iter;
cout<<endl;
return 0;
}

---www.tenouk.com---
The source code for this tutorial is available in C++ STL Container source code.
A complete C & C++ Standard Library documentation that includes STL.
Check the best selling C / C++ books at Amazon.com.
2003-2005 © Tenouk. All rights reserved.