C++: initialize std::vector

The following code initializes 1 and 2 dimensional std::vector with its size and initial value. The output should be

declare 1 dimensional vector of size 4 with initial value 3
generated vector is size 4 vector:
vec[0] = 3
vec[1] = 3
vec[2] = 3
vec[3] = 3
declare 2 dimensional vector of size 3×4 with initial value 2
generated vector with method 1 is size 3×4 vector:
vec[0][0] = 2
vec[0][1] = 2
vec[0][2] = 2
vec[0][3] = 2
vec[1][0] = 2
vec[1][1] = 2
vec[1][2] = 2
vec[1][3] = 2
vec[2][0] = 2
vec[2][1] = 2
vec[2][2] = 2
vec[2][3] = 2
generated vector with method 2 is size 3×4 vector:
vec[0][0] = 2
vec[0][1] = 2
vec[0][2] = 2
vec[0][3] = 2
vec[1][0] = 2
vec[1][1] = 2
vec[1][2] = 2
vec[1][3] = 2
vec[2][0] = 2
vec[2][1] = 2
vec[2][2] = 2
vec[2][3] = 2

// STL
#include <vector>
#include <iostream>

//! initializes a 1 dimensional vector with its size and initial value.
template <typename ValueType>
void initVector1D(
    const int size,
    const ValueType value = (ValueType)0.0
)
{
    std::cout << "declare 1 dimensional vector of size " << size << " with initial value " << value << std::endl;
    std::vector<ValueType> vec(size, value);
    std::cout << "generated vector is size " << vec.size() << " vector: " << std::endl;
    for(int n = 0; n < vec.size(); ++n)
    {
        std::cout << "  vec[" << n << "] = " << vec[n] << std::endl;
    }
}

//! initializes a 2 dimensional vector with its size and initial value.
template <typename ValueType>
void initVector2D(
    const int size1,
    const int size2,
    const ValueType value = (ValueType)0.0
)
{
    std::cout << "declare 2 dimensional vector of size " << size1 << "x" << size2 << " with initial value " << value << std::endl;
    {
        std::vector< std::vector<ValueType> > vec(size1, std::vector<ValueType>(size2, value));
        std::cout << "generated vector with method 1 is size " << vec.size() << "x" << vec[0].size() << " vector: " << std::endl;
        for(int n = 0; n < vec.size(); ++n)
        {
            for(int m = 0; m < vec[n].size(); ++m)
            {
                std::cout << "  vec[" << n << "][" << m << "] = " << vec[n][m] << std::endl;
            }
        }
    }
    {
        std::vector<ValueType> vecTmp(size2, value);
        std::vector< std::vector<ValueType> > vec(size1, vecTmp);
        std::cout << "generated vector with method 2 is size " << vec.size() << "x" << vec[0].size() << " vector: " << std::endl;
        for(int n = 0; n < vec.size(); ++n)
        {
            for(int m = 0; m < vec[n].size(); ++m)
            {
                std::cout << "  vec[" << n << "][" << m << "] = " << vec[n][m] << std::endl;
            }
        }
    }
}

int main()
{
    initVector1D(4,3.0f);
    initVector2D(3,4,2);

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s