STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
STXXL Vector

This section introduces into the STXXL vector container (to learn more about the structure of stxxl::vector, see section Vector).

Creating a STXXL vector

Before we can use a STXXL vector, we first have to define and then to instantiate a vector object. To manage the configuration of the vector type we use the generator template. A minimal configuration is shown below - as one can see the value_type (integer in our case) is the only only stricly neccessary parameter. See stxxl::VECTOR_GENERATOR for additional configuration parameters and information.

vector my_vector; // creates empty vector object

Insert elements

We can fill the vector by calling push_back() which appends a new value at the end:

for (int i = 0; i < 1024*1024; i++)
{
my_vector.push_back(i);
}
// my_vector stores: 0 1 2 3 4 5 6 [...] 1024*1024

Access elements

To read and/or modify the values of a STXXL vector, simply use the []-operator:

std::cout << "element at position 99: " << my_vector[99] << std::endl;
my_vector[99] = 0; // zeroing element at position 99

In addition, the STXXL vector provides different iterators to advance the vector which can be used as follows:

// create iterator which starts at the beginning of my_vector
vector::iterator iter = my_vector.begin();
// access an element
std::cout << "first element: " << *iter << std::endl;
// go to next element
iter++;

Alongside with the many advantages of iterators, there are several things which need to bear in mind when using them. Details are described in More Notes.

Delete elements

The operation pop_back() removes the last element of the vector (without returning it). The following code snippet is emptying my_vector:

// empty() returns true, if the vector is empty
while (!my_vector.empty()) {
my_vector.pop_back();
}

Determine size / Check whether vector is empty

To determine the number of elements a vector currently stores, call size():

std::cout << "size of vector: " << my_vector.size() << std::endl;

To check if the vector is empty, call the empty() function which returns true in that case:

std::cout << "vector empty? " << my_vector.empty() << std::endl;

A minimal working example of STXXL's vector

(See examples/containers/vector1.cpp for the sourcecode of the following example).

#include <stxxl/vector>
#include <iostream>
int main()
{
vector my_vector;
for (int i = 0; i < 1024 * 1024; i++)
{
my_vector.push_back(i + 2);
}
std::cout << my_vector[99] << std::endl;
my_vector[100] = 0;
while (!my_vector.empty())
{
my_vector.pop_back();
}
return 0;
}

See examples/containers/vector2.cpp for the sourcecode of a more comprehensive example.