Additional C++

Using STL Containers

Example 1

// Adding element to a vector and traversals forward and backward
#include <iostream>
#include <vector>

int main() {
    using namespace std;

    // Fill the vector with the numbers 0 through 9
    vector<int> qIntVector;
    for (int iIndex = 0; iIndex < 10; ++iIndex) {
        qIntVector.push_back(iIndex);
    }

    // Traverse the vector forward and output the integers
    vector<int>::iterator qIter;
    for (qIter = qIntVector.begin(); qIter != qIntVector.end(); ++qIter) {
        cout << "  " << (*qIter);
    }
    cout << endl;

    // Traverse the vector backward and output the integers
    vector<int>::reverse_iterator qRevIter;
    for (   qRevIter = qIntVector.rbegin();
        qRevIter != qIntVector.rend();
        ++qRevIter) {

        cout << "  " << (*qRevIter);
    }
    cout << endl;

    return 0;
}


Output:







Example 2

// Adding and removing elements from the front and back
#include <iostream>
#include <list>

void PrintList(const std::list<int>& kqrList) {
    std::list<int>::const_iterator qConstIter;
    for (qConstIter = kqrList.begin(); qConstIter != kqrList.end(); ++qConstIter) {

        std::cout << "  " << (*qConstIter);
    }
    std::cout << std::endl;
}

int main() {
    using namespace std;

    // Push 1,2,3 on the back
    list<int> qIntList;
    qIntList.push_back(1);
    qIntList.push_back(2);
    qIntList.push_back(3);
    PrintList(qIntList);

    // Push two elements on the front
    qIntList.push_front(4);
    qIntList.push_front(5);
    PrintList(qIntList);

    // Pop one element off of the back
    qIntList.pop_back();
    PrintList(qIntList);

    // Pop one element off of the front
    qIntList.pop_front();
    PrintList(qIntList);

    return 0;
}


Output:







Example 3

// An example or erasing and inserting elements
#include <iostream>
#include <vector>

void PrintVector(const std::vector<int>& kqrVector) {
    std::vector<int>::const_iterator qIter;
    for (qIter = kqrVector.begin(); qIter != kqrVector.end(); ++qIter) {
        std::cout << "  " << (*qIter);
    }
    std::cout << std::endl;
}


int main() {
    using namespace std;

    // Fill two vectors with the numbers 0 through 9
    vector<int> qVector1;
    vector<int> qVector2;
    for (int iIndex = 0; iIndex < 10; ++iIndex) {
        qVector1.push_back(iIndex);
        qVector2.push_back(iIndex);
    }
    cout << endl << "Vectors:" << endl;
    PrintVector(qVector1);
    PrintVector(qVector2);

    // Erase the numbers 3 through 5 from vector #1
    std::vector<int>::iterator qStartIter = qVector1.begin();
    std::vector<int>::iterator qEndIter = qVector1.begin();
    // Advance the iterators to the 3 and 6 elements in the vector
    advance(qStartIter, 3);
    advance(qEndIter, 6);
    // erase() removes from qStartIter upto but including qEndIter
    qVector1.erase(qStartIter, qEndIter);
    cout << endl << "Vectors:" << endl;
    PrintVector(qVector1);
    PrintVector(qVector2);

    // Insert the numbers 5 through 7 from vector #2
    qStartIter      = qVector2.begin();
    qEndIter        = qVector2.begin();
    // Advance the iterators to the 5 and8elements in the vector
    advance(qStartIter, 5);
    advance(qEndIter, 8);
    // insert() inserts from qStartIter up to but including qEndIter
    std::vector<int>::iterator qInsertIter    = qVector1.begin();
    // Advance the iterator to insert at the 5th element
    advance(qInsertIter, 5);
    qVector1.insert(qInsertIter, qStartIter, qEndIter);
    cout << endl << "Vectors:" << endl;
    PrintVector(qVector1);
    PrintVector(qVector2);

    return 0;
}


Output:







Example 4

// Example using accessors, sizes and resizing
#include <iostream>
#include <vector>

void PrintVector(const std::vector<int>& kqrVector) {
    std::vector<int>::const_iterator qIter;
    for (qIter = kqrVector.begin(); qIter != kqrVector.end(); ++qIter) {
        std::cout << "  " << (*qIter);
    }
    std::cout << std::endl;
}


int main() {
    using namespace std;

    // Fill a vector with the numbers 0 through 9
    vector<int> qVector1;
    for (int iIndex = 0; iIndex < 10; ++iIndex) {
        qVector1.push_back(iIndex);
    }

    // Accessing elements
    cout << "Third element at(3): " << qVector1.at(3) << endl;
    cout << "Fifth element [5]: " << qVector1[5] << endl;
    cout << "First element front(): " << qVector1.front() << endl;
    cout << "Last element back(): " << qVector1.back() << endl << endl;

    // Number of allocated elements in the vector
    cout << "capacity(): " << (unsigned int)qVector1.capacity() << endl;
    // Expand the vector allocation using reserve()
    qVector1.reserve(20);
    cout << "New capacity(): " << (unsigned int)qVector1.capacity() << endl;
    // Actual number of elements in the vector
    cout << "size(): " << (unsigned int)qVector1.size() << endl << endl;

    // Resize with zero filling
    qVector1.resize(12);
    PrintVector(qVector1);
    // Resize with truncation
    qVector1.resize(7);
    PrintVector(qVector1);
    // Resize with specified extension
    qVector1.resize(10, 21);
    PrintVector(qVector1);

    return 0;
}


Output:







Example 5

// An example using the swap(), clear(), and empty() functions
#include <iostream>
#include <vector>

void PrintVector(const std::vector<int>& kqrVector) {
    std::vector<int>::const_iterator qIter;
    for (qIter = kqrVector.begin(); qIter != kqrVector.end(); ++qIter) {
        std::cout << "  " << (*qIter);
    }
    std::cout << std::endl;
}


int main() {
    using namespace std;

    // Fill two vectors with the numbers 0 through 9
    // and 13 through 22
    vector<int> qVector1;
    vector<int> qVector2;
    for (int iIndex = 0; iIndex < 10; ++iIndex) {
        qVector1.push_back(iIndex);
        qVector2.push_back(iIndex + 13);
    }
    cout << endl << "Vectors:" << endl;
    PrintVector(qVector1);
    PrintVector(qVector2);

    // Swap the entries in both vectors
    swap(qVector1, qVector2);
    cout << endl << "Vectors:" << endl;
    PrintVector(qVector1);
    PrintVector(qVector2);

    // Clear vector 1
    qVector1.clear();
    cout << endl << "Vectors:" << endl;
    PrintVector(qVector1);
    PrintVector(qVector2);

    // Report whether the vectors are empty using empty()
    cout << endl;
    cout << "Is vectors #1 empty? : " << qVector1.empty() << endl;
    cout << "Is vectors #2 empty? : " << qVector2.empty() << endl;
    return 0;
}


Output:







 

© 2007–2024 XoaX.net LLC. All rights reserved.