Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your first problem is related to the way arrays are (not) handled in C++. Arrays live a kind of very fragile shadow existence where, if you as much as look at them in a funny way, they are converted into pointers. Your function doesn't take two pointers-to-arrays as you expect. It takes two pointers to pointers.</p> <p>In other words, you lose all information about the <em>size</em> of the arrays. <code>sizeof(a)</code> doesn't give you the size of the array. It gives you the size of a pointer to a pointer.</p> <p>So you have two options: the quick and dirty ad-hoc solution is to pass the array sizes explicitly:</p> <pre><code>gboolean foo (gchar** a, int a_size, gchar** b, int b_size) </code></pre> <p>Alternatively, and much nicer, you can use vectors instead of arrays:</p> <pre><code>gboolean foo (const std::vector&lt;gchar*&gt;&amp; a, const std::vector&lt;gchar*&gt;&amp; b) </code></pre> <p>Vectors are dynamically sized arrays, and as such, they know their size. <code>a.size()</code> will give you the number of elements in a vector. But they also have two convenient member functions, <code>begin()</code> and <code>end()</code>, designed to work with the standard library algorithms.</p> <p>So, to sort a vector:</p> <pre><code>std::sort(a.begin(), a.end()); </code></pre> <p>And likewise for <code>std::includes</code>.</p> <p>Your <em>second</em> problem is that you don't operate on strings, but on char pointers. In other words, <code>std::sort</code> will sort by pointer address, rather than by string contents.</p> <p>Again, you have two options:</p> <p>If you insist on using char pointers instead of strings, you can specify a custom comparer for <code>std::sort</code> (using a lambda because you mentioned you were ok with them in a comment)</p> <pre><code>std::sort(a.begin(), a.end(), [](gchar* lhs, gchar* rhs) { return strcmp(lhs, rhs) &lt; 0; }); </code></pre> <p>Likewise, <code>std::includes</code> takes an optional fifth parameter used to compare elements. The same lambda could be used there.</p> <p>Alternatively, you simply use <code>std::string</code> instead of your char pointers. Then the default comparer works:</p> <pre><code>gboolean foo (const std::vector&lt;std::string&gt;&amp; a, const std::vector&lt;std::string&gt;&amp; b) { gboolean result; std::sort (a.begin(), a.end()); std::sort (b.begin(), b.end()); result = std::includes (b.begin(), b.end(), a.begin(), a.end()); return result; } </code></pre> <p>Simpler, cleaner and safer.</p>
 

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