Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A good way to go about this would be to use <code>std::transform</code>. Consult this <a href="http://www.cplusplus.com/reference/algorithm/transform/" rel="nofollow">link</a> for more details. Short example with how to do this for rows is below. The column part is a little tricky.</p> <hr> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; using namespace std; const int nRowCnt = 3, nColCnt = 3; int RowFunc(int i) { return ++i; } int ColFunc(int i) { return --i; } void PrintArray(vector&lt;vector&lt;int&gt;&gt;&amp; vecArray, int nRowCnt, int nColCnt) { for (int nOuter = 0; nOuter &lt; nRowCnt; nOuter++) { for (int nInner = 0; nInner &lt; nColCnt; nInner++) { cout&lt;&lt;vecArray[nOuter][nInner]&lt;&lt;" "; } cout&lt;&lt;endl; } } int main() { vector&lt; vector&lt;int&gt; &gt; vecVals(nRowCnt, vector&lt;int&gt;(nColCnt,0)); vector&lt; int &gt; rowOut(nColCnt*nRowCnt,0), colOut(nColCnt*nRowCnt,0); vector&lt;int&gt;::iterator itrOut; for (int nRow = 0; nRow &lt; nRowCnt; nRow++) { for (int nCol = 0; nCol &lt; nColCnt; nCol++) { vecVals[nRow][nCol] = nRow * (10+nCol) ; } } PrintArray(vecVals,nRowCnt,nColCnt); itrOut = rowOut.begin(); for (int nOuter = 0; nOuter &lt; nRowCnt; nOuter++) { std::transform(vecVals[nOuter].begin(),vecVals[nOuter].end(),itrOut,RowFunc); itrOut += nColCnt; } itrOut = colOut.begin(); for (int nOuter = 0; nOuter &lt; nRowCnt; nOuter++) { for (int nInner = 0; nInner &lt; nColCnt; nInner++) { std::transform( vecVals[nInner].begin() + nOuter, vecVals[nInner].begin() + nOuter +1, itrOut,ColFunc); itrOut++; } } cout&lt;&lt;endl&lt;&lt;"Row Transformed"&lt;&lt;endl; for (itrOut = rowOut.begin(); itrOut != rowOut.end(); itrOut++) cout&lt;&lt;*itrOut&lt;&lt;" "; cout&lt;&lt;endl&lt;&lt;"Col Transformed"&lt;&lt;endl; for (itrOut = colOut.begin(); itrOut != colOut.end(); itrOut++) cout&lt;&lt;*itrOut&lt;&lt;" "; cout&lt;&lt;endl; return 0; } </code></pre> <hr> <p>There is a catch though, the column part won't work for non-square 2D arrays (i.e for it to work row and column counts must be the same). I guess this can be worked around with a little more thought.</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. This table or related slice is empty.
    1. VO
      singulars
      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