qtconcurrentmap.html 27.4 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtconcurrentmap.cpp -->
  <title>Concurrent Map and Map-Reduce | Qt Concurrent 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtconcurrent-index.html">Qt Concurrent</a></td><td >Concurrent Map and Map-Reduce</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#concurrent-map">Concurrent Map</a></li>
<li class="level1"><a href="#concurrent-map-reduce">Concurrent Map-Reduce</a></li>
<li class="level1"><a href="#additional-api-features">Additional API Features</a></li>
<li class="level2"><a href="#using-iterators-instead-of-sequence">Using Iterators instead of Sequence</a></li>
<li class="level2"><a href="#blocking-variants">Blocking Variants</a></li>
<li class="level2"><a href="#using-member-functions">Using Member Functions</a></li>
<li class="level2"><a href="#using-function-objects">Using Function Objects</a></li>
<li class="level2"><a href="#wrapping-functions-that-take-multiple-arguments">Wrapping Functions that Take Multiple Arguments</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Concurrent Map and Map-Reduce</h1>
<span class="subtitle"></span>
<!-- $$$qtconcurrentmap.html-description -->
<div class="descr"> <a name="details"></a>
<p>The QtConcurrent::map(), QtConcurrent::mapped() and QtConcurrent::mappedReduced() functions run computations in parallel on the items in a sequence such as a <a href="../qtcore/qlist.html">QList</a> or a <a href="../qtcore/qvector.html">QVector</a>. QtConcurrent::map() modifies a sequence in-place, QtConcurrent::mapped() returns a new sequence containing the modified content, and QtConcurrent::mappedReduced() returns a single result.</p>
<p>These functions are a part of the <a href="qtconcurrent-index.html">Qt Concurrent</a> framework.</p>
<p>Each of the above functions has a blocking variant that returns the final result instead of a <a href="../qtcore/qfuture.html">QFuture</a>. You use them in the same way as the asynchronous variants.</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;

  <span class="comment">// Each call blocks until the entire operation is finished.</span>
  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMapped(images<span class="operator">,</span> scaled);

  <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMap(images<span class="operator">,</span> scale);

  <span class="type">QImage</span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMappedReduced(images<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage);

</pre>
<p>Note that the result types above are not <a href="../qtcore/qfuture.html">QFuture</a> objects, but real result types (in this case, <a href="../qtcore/qlist.html">QList</a>&lt;QImage&gt; and QImage).</p>
<a name="concurrent-map"></a>
<h2 id="concurrent-map">Concurrent Map</h2>
<p>QtConcurrent::mapped() takes an input sequence and a map function. This map function is then called for each item in the sequence, and a new sequence containing the return values from the map function is returned.</p>
<p>The map function must be of the form:</p>
<pre class="cpp">

  U function(<span class="keyword">const</span> T <span class="operator">&amp;</span>t);

</pre>
<p>T and U can be any type (and they can even be the same type), but T must match the type stored in the sequence. The function returns the modified or <i>mapped</i> content.</p>
<p>This example shows how to apply a scale function to all the items in a sequence:</p>
<pre class="cpp">

  <span class="type">QImage</span> scaled(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>image)
  {
      <span class="keyword">return</span> image<span class="operator">.</span>scaled(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>);
  }

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> scaled);

</pre>
<p>The results of the map are made available through <a href="../qtcore/qfuture.html">QFuture</a>. See the <a href="../qtcore/qfuture.html">QFuture</a> and <a href="../qtcore/qfuturewatcher.html">QFutureWatcher</a> documentation for more information on how to use <a href="../qtcore/qfuture.html">QFuture</a> in your applications.</p>
<p>If you want to modify a sequence in-place, use QtConcurrent::map(). The map function must then be of the form:</p>
<pre class="cpp">

  U function(T <span class="operator">&amp;</span>t);

</pre>
<p>Note that the return value and return type of the map function are not used.</p>
<p>Using QtConcurrent::map() is similar to using QtConcurrent::mapped():</p>
<pre class="cpp">

  <span class="type">void</span> scale(<span class="type">QImage</span> <span class="operator">&amp;</span>image)
  {
      image <span class="operator">=</span> image<span class="operator">.</span>scaled(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>);
  }

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>map(images<span class="operator">,</span> scale);

</pre>
<p>Since the sequence is modified in place, QtConcurrent::map() does not return any results via <a href="../qtcore/qfuture.html">QFuture</a>. However, you can still use <a href="../qtcore/qfuture.html">QFuture</a> and <a href="../qtcore/qfuturewatcher.html">QFutureWatcher</a> to monitor the status of the map.</p>
<a name="concurrent-map-reduce"></a>
<h2 id="concurrent-map-reduce">Concurrent Map-Reduce</h2>
<p>QtConcurrent::mappedReduced() is similar to QtConcurrent::mapped(), but instead of returning a sequence with the new results, the results are combined into a single value using a reduce function.</p>
<p>The reduce function must be of the form:</p>
<pre class="cpp">

  V function(T <span class="operator">&amp;</span>result<span class="operator">,</span> <span class="keyword">const</span> U <span class="operator">&amp;</span>intermediate)

</pre>
<p>T is the type of the final result, U is the return type of the map function. Note that the return value and return type of the reduce function are not used.</p>
<p>Call QtConcurrent::mappedReduced() like this:</p>
<pre class="cpp">

  <span class="type">void</span> addToCollage(<span class="type">QImage</span> <span class="operator">&amp;</span>collage<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>thumbnail)
  {
      <span class="type">QPainter</span> p(<span class="operator">&amp;</span>collage);
      <span class="keyword">static</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> offset <span class="operator">=</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      p<span class="operator">.</span>drawImage(offset<span class="operator">,</span> thumbnail);
      offset <span class="operator">+</span><span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  }

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(images<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage);

</pre>
<p>The reduce function will be called once for each result returned by the map function, and should merge the <i>intermediate</i> into the <i>result</i> variable. QtConcurrent::mappedReduced() guarantees that only one thread will call reduce at a time, so using a mutex to lock the result variable is not necessary. The <a href="qtconcurrent.html#ReduceOption-enum">QtConcurrent::ReduceOptions</a> enum provides a way to control the order in which the reduction is done. If <a href="qtconcurrent.html#ReduceOption-enum">QtConcurrent::UnorderedReduce</a> is used (the default), the order is undefined, while <a href="qtconcurrent.html#ReduceOption-enum">QtConcurrent::OrderedReduce</a> ensures that the reduction is done in the order of the original sequence.</p>
<a name="additional-api-features"></a>
<h2 id="additional-api-features">Additional API Features</h2>
<a name="using-iterators-instead-of-sequence"></a>
<h3 >Using Iterators instead of Sequence</h3>
<p>Each of the above functions has a variant that takes an iterator range instead of a sequence. You use them in the same way as the sequence variants:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;

  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">.</span>constBegin()<span class="operator">,</span> images<span class="operator">.</span>constEnd()<span class="operator">,</span> scaled);

  <span class="comment">// Map in-place only works on non-const iterators.</span>
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>map(images<span class="operator">.</span>begin()<span class="operator">,</span> images<span class="operator">.</span>end()<span class="operator">,</span> scale);

  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(images<span class="operator">.</span>constBegin()<span class="operator">,</span> images<span class="operator">.</span>constEnd()<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage);

</pre>
<a name="blocking-variants"></a>
<h3 >Blocking Variants</h3>
<p>Each of the above functions has a blocking variant that returns the final result instead of a <a href="../qtcore/qfuture.html">QFuture</a>. You use them in the same way as the asynchronous variants.</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;

  <span class="comment">// Each call blocks until the entire operation is finished.</span>
  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMapped(images<span class="operator">,</span> scaled);

  <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMap(images<span class="operator">,</span> scale);

  <span class="type">QImage</span> collage <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>blockingMappedReduced(images<span class="operator">,</span> scaled<span class="operator">,</span> addToCollage);

</pre>
<p>Note that the result types above are not <a href="../qtcore/qfuture.html">QFuture</a> objects, but real result types (in this case, <a href="../qtcore/qlist.html">QList</a>&lt;QImage&gt; and QImage).</p>
<a name="using-member-functions"></a>
<h3 >Using Member Functions</h3>
<p>QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept pointers to member functions. The member function class type must match the type stored in the sequence:</p>
<pre class="cpp">

  <span class="comment">// Squeeze all strings in a QStringList.</span>
  <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> squeezedStrings <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>map(strings<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>squeeze);

  <span class="comment">// Swap the rgb values of all pixels on a list of images.</span>
  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> bgrImages <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> <span class="operator">&amp;</span><span class="type">QImage</span><span class="operator">::</span>rgbSwapped);

  <span class="comment">// Create a set of the lengths of all strings in a list.</span>
  <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span> <span class="operator">&gt;</span> wordLengths <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(strings<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>length<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span><span class="operator">::</span>insert);

</pre>
<p>Note that when using QtConcurrent::mappedReduced(), you can mix the use of normal and member functions freely:</p>
<pre class="cpp">

  <span class="comment">// Can mix normal functions and member functions with QtConcurrent::mappedReduced().</span>

  <span class="comment">// Compute the average length of a list of strings.</span>
  <span class="keyword">extern</span> <span class="type">void</span> computeAverage(<span class="type">int</span> <span class="operator">&amp;</span>average<span class="operator">,</span> <span class="type">int</span> length);
  <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> strings <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span> averageWordLength <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(strings<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>length<span class="operator">,</span> computeAverage);

  <span class="comment">// Create a set of the color distribution of all images in a list.</span>
  <span class="keyword">extern</span> <span class="type">int</span> colorDistribution(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>string);
  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span> <span class="operator">&gt;</span> totalColorDistribution <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced(images<span class="operator">,</span> colorDistribution<span class="operator">,</span> <span class="type"><a href="../qtcore/qset.html">QSet</a></span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span><span class="operator">::</span>insert);

</pre>
<a name="using-function-objects"></a>
<h3 >Using Function Objects</h3>
<p>QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept function objects for the map function. These function objects can be used to add state to a function call. The result_type typedef must define the result type of the function call operator:</p>
<pre class="cpp">

  <span class="keyword">struct</span> Scaled
  {
      Scaled(<span class="type">int</span> size)
      : m_size(size) { }

      <span class="keyword">typedef</span> <span class="type">QImage</span> result_type;

      <span class="type">QImage</span> <span class="keyword">operator</span>()(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>image)
      {
          <span class="keyword">return</span> image<span class="operator">.</span>scaled(m_size<span class="operator">,</span> m_size);
      }

      <span class="type">int</span> m_size;
  };

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> Scaled(<span class="number">100</span>));

</pre>
<p>For the reduce function, function objects are not directly supported. Function objects can, however, be used when the type of the reduction result is explicitly specified:</p>
<pre class="cpp">

  <span class="keyword">struct</span> ImageTransform
  {
      <span class="type">void</span> <span class="keyword">operator</span>()(<span class="type">QImage</span> <span class="operator">&amp;</span>result<span class="operator">,</span> <span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>value);
  };

  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> thumbNails <span class="operator">=</span>
    <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mappedReduced<span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span>(images<span class="operator">,</span>
                                        Scaled(<span class="number">100</span>)<span class="operator">,</span>
                                        ImageTransform()<span class="operator">,</span>
                                        <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>SequentialReduce);

</pre>
<a name="wrapping-functions-that-take-multiple-arguments"></a>
<h3 >Wrapping Functions that Take Multiple Arguments</h3>
<p>If you want to use a map function that takes more than one argument you can use a lambda function or <code>std::bind()</code> to transform it onto a function that takes one argument.</p>
<p>As an example, we'll use QImage::scaledToWidth():</p>
<pre class="cpp">

  <span class="type">QImage</span> <span class="type">QImage</span><span class="operator">::</span>scaledToWidth(<span class="type">int</span> width<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>TransformationMode) <span class="keyword">const</span>;

</pre>
<p>scaledToWidth takes three arguments (including the &quot;this&quot; pointer) and can't be used with QtConcurrent::mapped() directly, because QtConcurrent::mapped() expects a function that takes one argument. To use QImage::scaledToWidth() with QtConcurrent::mapped() we have to provide a value for the <i>width</i> and the <i>transformation mode</i>:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> images <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
  std<span class="operator">::</span>function<span class="operator">&lt;</span><span class="type">QImage</span>(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>)<span class="operator">&gt;</span> scale <span class="operator">=</span> <span class="operator">[</span><span class="operator">]</span>(<span class="keyword">const</span> <span class="type">QImage</span> <span class="operator">&amp;</span>img) {
      <span class="keyword">return</span> img<span class="operator">.</span>scaledToWidth(<span class="number">100</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SmoothTransformation);
  };
  <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">QImage</span><span class="operator">&gt;</span> thumbnails <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>mapped(images<span class="operator">,</span> scale);

</pre>
</div>
<!-- @@@qtconcurrentmap.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>