You can find the answer in this FAQ: How to iterate over all non zero elements?
In your case it would be:
typedef boost::numeric::ublas::compressed_matrix<int>::iterator1 it1_t; typedef boost::numeric::ublas::compressed_matrix<int>::iterator2 it2_t; for (it1_t it1 = adjacency.begin1(); it1 != adjacency.end1(); it1++) { for (it2_t it2 = it1.begin(); it2 != it1.end(); it2++) { std::cout << "(" << it2.index1() << "," << it2.index2() << ") = "; std::cout << *it2 << std::endl; }
}
原贴连接:http://stackoverflow.com/questions/1795658/looping-over-the-non-zero-elements-of-a-ublas-sparse-matrix