Note that there are some explanatory texts on larger screens.

plurals
  1. POBlitz++ arrays as keys for maps
    primarykey
    data
    text
    <p>I am trying to use blitz++ arrays, since I understand they generally offer higher performance than other forms of arrays. Is it possible to use blitz++ arrays as keys in a map? Trying </p> <pre><code>#include &lt;map&gt; #include &lt;blitz/array.h&gt; using namespace std; map&lt;blitz::Array&lt;int,1&gt;,int&gt; testmap; blitz::Array&lt;int,1&gt; B(3); B = 1,2,3; testmap.insert(make_pair(B,2)); </code></pre> <p>does not compile. Here's the error:</p> <blockquote> <p>In file included from /usr/include/c++/4.6/string:50:0,</p> <p>/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less&lt;_Tp>::operator()(const _Tp&amp;, const _Tp&amp;) const [with _Tp = blitz::Array]’:</p> <p>/usr/include/c++/4.6/bits/stl_function.h:236:22: error: cannot convert ‘blitz::BzBinaryExprResult, blitz::Array >::T_result {aka blitz::_bz_ArrayExpr, blitz::FastArrayIterator, blitz::Less > >}’ to ‘bool’ in return</p> </blockquote> <p>Is this a matter of requiring a definition of the <code>&lt;</code> operator, and if so, can/should I define it myself?</p> <p><strong>ANSWER</strong></p> <p>As suggested by Jimmy Thompson, a possible solution is to define:</p> <pre><code>struct MyComparison { bool operator() (const blitz::Array&lt;int, 1&gt; &amp;lhs, const blitz::Array&lt;int, 1&gt; &amp;rhs) const { if (lhs.size() &lt; rhs.size()) {return true;} else if (lhs.size() &gt; rhs.size()) {return false;} else { for (int i=0; i&lt;lhs.size(); i++) { if (lhs(i)&lt;rhs(i)) {return true;} else if(lhs(i)&gt;rhs(i)) {return false;} } } } }; </code></pre> <p>Then</p> <pre><code>map&lt;blitz::Array&lt;int,1&gt;,int, MyComparison&gt; testmap; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload