|< C++ STL Container 1 | Main | C++ STL Container 3 >| Site Index | Download |


 

 

 

 

 

MODULE 27

 THE STL CONTAINER PART 2

vector, deque

 

 

 

 

 

My Training Period: xx hours

 

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 C++ programming on STL container skills that should be acquired:

 

 

What do we have in this page?

  1. vector constructors program example

  2. vector, assign() program example

  3. vector, at() program example

  4. vector, back() and front() program example

  5. vector, begin() program example

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

  7. vector, clear(), empty() and size() program example

  8. vector, begin(), end() program example

  9. vector, erase(), begin() and end() program example

  10. vector, insert(), begin(), end() program example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • The following section demonstrates the program examples using the member functions and the typedefs

  • Vector constructor, constructs a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of all or part of some other vector.

  • All constructors store an allocator object and initialize the vector.

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

 

C++ STL Container vector constructor

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

}

 

Output:

 

C++ STL Container vector assign()

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

}

 

Output:

C++ STL Container vector at()

  • For back(), the return value is the last element of the vector. If the vector is empty, the return value is undefined.

  • If the return value of back() is assigned to a const_reference, the vector object cannot be modified. If the return value of back() is assigned to a reference, the vector object can be modified.

  • For front(), the return value is a reference to the first element in the vector object. If the vector is empty, the return is undefined.

  • If the return value of front() is assigned to a const_reference, the vector object cannot be modified. If the return value of front() is assigned to a reference, the vector object can be modified.

 

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

}

 

Output:

 

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

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

}

 

Output:

 

C++ STL Container vector begin()

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

}

 

Output:

 

C++ STL Container vector capacity()

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

}

 

Output:

 

C++ STL Container vector clear() and empty()

// vector, begin(), end()

#include <vector>

#include <iostream>

 

int main( )

{

   using namespace std;

   vector <int> vec1;

   vector <int>::iterator vec1_Iter;

   vec1.push_back(9);

   vec1.push_back(2);

   vec1.push_back(7);

   vec1.push_back(3);

   cout<<"Operation: vec1.begin() and vec1.end()"<<endl;

   cout<<"vec1 data: ";

   for(vec1_Iter = vec1.begin(); vec1_Iter != vec1.end(); vec1_Iter++)

      cout<<*vec1_Iter<<' ';

   cout<<endl;

   return 0;

}

 

Output:

 

C++ STL Container vector begin() and end()

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

}

 

Output:

 

C++ STL Container vector erase() and begin()

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

}

 

Output:

 

C++ STL Container vector insert()

 

 

---www.tenouk.com---

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ STL 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 1 | Main | C++ STL Container 3 >| 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