### Recursive function and vector.

Is there a way to get the value of the elements through each pass of the recursive function rather than just one value?

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; // selectContain picks the 1 of the 4 vector element in data. srchContain used for iterator arithmetics. double getData(vector> da, const int selectContain, int srchContain = 0) { vector::iterator beg = da.at(selectContain).begin(); //Sets a begin iterator within the selected 1 of 4 vector elements in data. beg += srchContain; //Iterators through the selected vector element. if (beg == da.at(selectEle).end() - 1) //Condition to break recursion when iteration reaches the end of selected vector element. { return *beg; } return *beg, getData(da, selectEle, srchContain + 1); } int main() { vector> data{ {1,2,3,4}, {12,33,44,55}, {6,7,8}, {12,3,4,5} }; cout << getData(data, 1, 0); //Passes data, selecting {12,33,44,55}, starting from 0. return 0; }``````
Last edited on
I find it hard to understand what this function is doing and the parameter names doesn't help much. If you want to return many values you can put the values in a vector and return the vector.
You set getData to return double and returning pointe? wtf?
*beg is not a pointer.
Added comments, I hope that manages to clarify what it's doing. I want it to return

12 33 44 55

instead of just 55. This is meant to get data from a vector defined in a class. Unless I'm going about it horribly wrong.

Also if you find it hard to understand does that mean the code is written badly?
It's dereferencing beg which is an iterator to get the value. Unless I'm not understanding this right either.
Add vector returned from recursive call to current vector.
Sorry MiiNiPaa but I don't understand. So I should be making another vector to collect the value returned from the function?
If I've understood the intent correctly:
 ``1234567`` ``````vector get_slice( const vector>& da, std::size_t row, std::size_t from_col = 0 ) { return row < da.size() && from_col < da[row].size() ? vector( da[row].begin() + from_col, da[row].end() ) : vector() ; }``````
I was intending that the function would return the value like:

 ``123456`` ``````vector store {1,2,3,4,5,6,7}; for (auto x : store) { cout << x << endl; }``````

 ``1234567891011121314151617181920`` ``````//I thought this would do the same as the above. int getData(vector da, std::size_t val = 0) { if (val == da.size() - 1) { return da[val]; } return da[val], getData(da, val + 1); } int main() { vector store {1,2,3,4,5,6,7}; cout << getData(store) << endl; }``````

But I forgot the fact that functions can only return one value. Unless there's a way aside for creating a new vector and copying the contents over; or rather initializing a vector with the contents of another? (Am I understanding your code right JLBorges?)
Last edited on
By logic this statement

cout << getData(data, 1, 0); //Passes data, selecting {12,33,44,55}, starting from 0

is equivalent to

for ( int x : data[1] ) std::cout << x << ' ';

I do not see any necessarity to write a special function. If you need some range of elements of a nested vector<int> it can be done very simple

Let assume that i - is the target vector and j - is the target starting element in the vector. Then you can write

 ``12`` ``````std::vector v( std::next( data[i].begin(), j ), std[i].end() ); for ( int x : v ) std::cout << x << ' ';``````
Last edited on
> or rather initializing a vector with the contents of another?
> (Am I understanding your code right JLBorges?)

Yes. Initializing a vector with a slice of the contents of another.

The slice can also be a slice of references instead of copied values:

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using std::vector ; vector get_slice( const vector>& da, std::size_t row, std::size_t from_col = 0 ) { return row < da.size() && from_col < da[row].size() ? vector( da[row].begin() + from_col, da[row].end() ) : vector() ; } #include vector< std::reference_wrapper> get_slice( vector>& da, std::size_t row, std::size_t from_col = 0 ) { return row < da.size() && from_col < da[row].size() ? vector< std::reference_wrapper>( da[row].begin() + from_col, da[row].end() ) : vector< std::reference_wrapper>() ; } #include int main() { vector> data{ {1,2,3,4}, {12,33,44,55}, {6,7,8}, {12,3,4,5} }; for( int i : get_slice( data, 1, 1 ) ) // {33,44,55} (starting from 1) std::cout << i << ' ' ; std::cout << '\n' ; for( auto& ref : get_slice( data, 1, 1 ) ) ref += 100 ; for( int i : get_slice( data, 1, 1 ) ) std::cout << i << ' ' ; std::cout << '\n' ; }``````

http://liveworkspace.org/code/351scV\$0
Here is a demonstrative example

 ``1234567891011121314151617`` ``````#include #include #include int main() { std::vector> data{ {1,2,3,4}, {12,33,44,55}, {6,7,8}, {12,3,4,5} }; for ( int x : std::vector( std::next( data[1].begin(), 0 ), data[1].end() ) ) { std::cout << x << ' '; } std::cout << std::endl; return 0; }``````

where i == 0 because in your original code you are trying to output a vector starting from the first element.

Or a more compound example

 ``1234567891011121314151617181920`` ``````#include #include #include int main() { std::vector> data{ {1,2,3,4}, {12,33,44,55}, {6,7,8}, {12,3,4,5} }; for ( std::vector::size_type i = 0; i < data[1].size(); i++ ) { for ( int x : std::vector( std::next( data[1].begin(), i ), data[1].end() ) ) { std::cout << x << ' '; } std::cout << std::endl; } return 0; }``````

Output is

 ```12 33 44 55 33 44 55 44 55 55```
Last edited on
@JLBorges - Thanks for the second example. Admittedly I don't quite understand it, but I'll look up the terms I'm not familiar with and figure it out. Appreciate the help.

@Vlad - Thank you for the explanation of what I was actually doing with cout << getData(data, 1, 0); and also for other ways to print out a vectors within a vector.

Case closed!
Topic archived. No new replies allowed.