How can I economically store a sparse matrix during the process of element filling?-Collection of common programming errors

std::map might be what you’re looking for, it’s a key -> value map type. Combine this with std::set, which is a unique collection of elements. So, you could use a map of std::set, like so:

std::map sparseMatrix;

// Add some edges.
sparseMatrix[0].insert(1); // Add an edge from vertex 0 to 1.
sparseMatrix[4].insert(2); // Add an edge from vertex 4 to 2.
sparseMatrix[0].insert(1); // Edge already exists, no data added to the set.

This representation lets you represent a directed graph, it’s analogous to an edge list. The behaviour of a set also prevents you from having two edges that are ‘identical’ (a->b and c->d, where a=b and c=d), which is nice, a behaviour you’d get if you used an adjacency matrix. You can iterate al the edges like so:

for(std::map::const_iterator i = sparseMatrix.begin();
    i != sparseMatrix.end();
    ++i)
{
    for(std::set::const_iterator j = i->second.begin();
        j != i->second.end();
        ++j)
    {
        std::cout