Note that there are some explanatory texts on larger screens.

plurals
  1. POStrategy of passing and returning values (any kind, including array or more complex data structure)?
    primarykey
    data
    text
    <p>I used to use PHP, and I like the way PHP passes and returns values, very straightforward. </p> <p>Now I've turned to C++ to gain more performance, but find it difficult to pass and return 2d array, not to mention some more complex data structures.</p> <ul> <li>I once thought JSON may be a choice, but this way you have to encode and decode every time, not practical.</li> <li>Currently I am using pointers, but it's not very convenient. </li> <li>Then I tried STL vector, still not satisfied,the STL vector is a little safer and comfortable to use, but still, you have to manually iterate the array, what I want is a more flexible strategy to deal with these kind of tasks</li> <li>I tried to use class/struct to hold the data, then pass and return, but I have to define a new struct whenever to pass a different kind of data structures. </li> </ul> <p>So, what's the best choice for data exchanging between functions? Professionals, any suggestions?</p> <p>edit:</p> <p>As an example:</p> <pre><code>string function(string data){ //do something return result; } </code></pre> <p>If this result is like:</p> <pre><code>{ "key1"=&gt;{"key11"=&gt;"value1"}, "key2"=&gt;{"key21"=&gt;"value2"}, "key3"=&gt;{"key31"=&gt;"value3","key32"=&gt;"value32"} } </code></pre> <p>it would be too complicated to pass this with pointers, wouldn't it? With php, you can use several foreach statements to process the nest array easily.</p> <p>edit2:</p> <p>To put it simply, how to exchange a nested array with unknown length and dimension?</p> <p>edit3:</p> <p>I am trying to find the cpp way of dealing with common programming tasks. And exchange data between functions is very important. </p> <ul> <li>In php, I can easily process the nested array with several foreach and return array easily. </li> <li>But with cpp <ul> <li>I have to return the result array pointer or directly operate the referred vector</li> <li>i need to pass the array length to functions</li> <li>when data structure is complicated, get values from the array is difficult.</li> </ul></li> </ul>
    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