Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's valid... but a for loop probably isn't what you want. When you use two for loops, your inner loop keeps going back to the start every time the outer loop loops. So if your vectors contain:</p> <pre><code>farray: 10 9 8 4 3 sarray: 7 6 4 3 1 </code></pre> <p>Then your final array will contain something like:</p> <pre><code>10 10 10 10 10 9 9 9 9 9 8 8 8 8 8 7 6 4 4 4 7 6 4 3 3 </code></pre> <p>because you are testing every single combination, and adding the larger one to the final list. A better solution might be to remember an iterator for each list, and just use one loop. Rather than looping over a list, just go through both of them together - if sarray has the larger number, then increment your sarray iterator, and compare that with the old farray iterator. Stop your loop when both sarray and farray are empty.</p> <pre><code>vector&lt;int&gt; fiter = farray.begin(); vector&lt;int&gt; siter = sarray.begin(); vector&lt;int&gt; final; // Let's traverse both farray and sarray. // We'll want to stop this loop once we've traversed both lists. while (fiter != farray.end() &amp;&amp; siter != sarray.end()) { if (fiter == farray.end()) { // we must have gone right through farray - // so use the value from sarray, and go to the next one final.push_back(*siter); siter++; } else if (siter == sarray.end()) { // we must have gone right through sarray - // so use the value from farray, and go to the next one final.push_back(*fiter); fiter++; } else if (*siter &gt; *fiter) { // siter is the bigger of the two - add it to the final list, and // go to the next sarray entry final.push_back(*siter); siter++; } else // *fiter &gt;= *siter { // fiter is the bigger of the two - add it to the final list, and // go to the next farray entry final.push_back(*fiter); fiter++; } } </code></pre> <p>I haven't tested it - and if this is for homework, then <em>please</em> try to understand what I've done, go away and write it yourself, rather than copy+paste.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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