Boost logo

Ublas :

Subject: Re: [ublas] Accessing the indices of non-zero elements in a UBLAS sparse vector
From: Oswin Krause (Oswin.Krause_at_[hidden])
Date: 2011-09-21 12:47:45


> Hello,
> How can I know the index of the first non-zero element in a
> sparse_vector in ublas and each subsequent nonzero element as well?
> The function begin() gives me an iterator that can be used to know the
> first non-zero value stored, not its index in the vector.
> Tarek
Iterators offer a method index() which returns the desired result. But
remember that only the const_iterator is really sparse!

for(SparseVector::const_iterator pos = sparseVec.begin(); pos !=
sparseVec.end();++pos){
std::cout << pos.index()<< " "<< *pos;
}

Greetings,
Oswin