|< C++ STL Container 2 | Main | C++ STL Container 4 >| Site Index | Download |


 

 

 

 

 

 

MODULE 27a

 THE STL CONTAINER PROGRAMMING PART 3

vector, deque

 

 

 

 

 

 

My Training Period: xx hours

 

This is a continuation from previous tutorial, 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 C++ container class programming abilities that supposed to be acquired:

 

 

 

What we have in this page?

  1. vector, max_size() program example

  2. vector, pop_back(), back(), and push_back() program example

  3. vector, rbegin() program example

  4. vector, rend() and rbegin() program example

  5. vector, resize() and size() program example

  6. vector, reserve(), capacity() and size() program example

  7. vector, swap() program example

  8. A vector Class Template Operator

  9. vector operator[ ] program example

  10. A vector Class Template Specializations

  11. vector<bool> Class Members

  12. vector_bool, flip() program example

  13. vector_bool, swap() program example

  14. Nested Classes

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 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.

Continuation from the previous Module...

  • The return value is the maximum possible length of the vector.

// 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:

 

C++ STL Container vector max_size()

 

// 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;

}

 

Output:

C++ STL Container vector pop_back() and back()

// 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;

}

 

Output:

 

C++ STL Container vector rbegin()

// 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;

}

 

Output:

 

C++ STL Container vector rend() and rbegin()

// 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;

}

 

Output:

C++ STL Container vector resize() and size()  

// 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;

}

 

Output:

 

C++ STL Container vector reserve()

 

// 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;

}

 

Output:

 

C++ STL Container vector swap()

 

A vector Class Template Operator

 

Operator

Description

operator[ ]

Returns a reference to the vector element at a specified position.

 

Table 27.6

// 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;

}

 

Output:

 

C++ STL Container vector operator [ ]

 

A vector Class Template Specializations

 

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

 

vector<bool> Class

 

vector<bool> Class Members

 

vector<bool> Typedefs

 

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

 

vector<bool> Member Functions

 

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;

}

 

Output:

 

C++ STL Container vector_bool flip()

 

// 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;

}

 

Output:

 

C++ STL Container vector_bool swap()

 

Nested Classes

 

Nested class

Description

vector<bool> reference

A nested class whose objects are able to provide references to elements (single bits) within a vector<bool> object.

 

Table 27.10

 

------------------------------------------End of C++ STL vector programming-----------------------------------

---www.tenouk.com---

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ Container related reading:

 

  1. The source code for this tutorial is available in C++ STL Container source code.

  2. A complete C & C++ Standard Library documentation that includes STL.

  3. Check the best selling C / C++ books at Amazon.com.

 

 

 

 

 

 

 

|< C++ STL Container 2 | Main | C++ STL Container 4 >| Site Index | Download |


C++ STL Container Classes:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9 | Part 10 | Part 11