{"id":5111,"date":"2014-03-30T19:00:30","date_gmt":"2014-03-30T19:00:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-economically-store-a-sparse-matrix-during-the-process-of-element-filling-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:00:30","modified_gmt":"2014-03-30T19:00:30","slug":"how-can-i-economically-store-a-sparse-matrix-during-the-process-of-element-filling-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-economically-store-a-sparse-matrix-during-the-process-of-element-filling-collection-of-common-programming-errors\/","title":{"rendered":"How can I economically store a sparse matrix during the process of element filling?-Collection of common programming errors"},"content":{"rendered":"<p>std::map might be what you&#8217;re looking for, it&#8217;s a key -&gt; 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:<\/p>\n<pre><code>std::map sparseMatrix;\n\n\/\/ Add some edges.\nsparseMatrix[0].insert(1); \/\/ Add an edge from vertex 0 to 1.\nsparseMatrix[4].insert(2); \/\/ Add an edge from vertex 4 to 2.\nsparseMatrix[0].insert(1); \/\/ Edge already exists, no data added to the set.\n<\/code><\/pre>\n<p>This representation lets you represent a directed graph, it&#8217;s analogous to an edge list. The behaviour of a set also prevents you from having two edges that are &#8216;identical&#8217; (a-&gt;b and c-&gt;d, where a=b and c=d), which is nice, a behaviour you&#8217;d get if you used an adjacency matrix. You can iterate al the edges like so:<\/p>\n<pre><code>for(std::map::const_iterator i = sparseMatrix.begin();\n    i != sparseMatrix.end();\n    ++i)\n{\n    for(std::set::const_iterator j = i-&gt;second.begin();\n        j != i-&gt;second.end();\n        ++j)\n    {\n        std::cout<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>std::map might be what you&#8217;re looking for, it&#8217;s a key -&gt; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5111","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5111"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5111\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}