hi,
I was wondering if there is a better way to do the following.
Let say I have a recursive function rec():
Code:
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
void rec(int s, vector<int>& j, unordered_map <int, int>& m){
if (m[s]>0)
rec(m[s], j, m);
j.push_back(s);
}
int main (){
vector<int> a(10);
unordered_map <int, int> map;
map[1] = 2;
map[2] = 3;
map[3] = 4;
map[5] = 5;
rec(1,a, map);
for(int s = 0; s< a.size(); s++){
cout << a[s] << "\n";
}
return 0;
}
and in each cycle i need to store the visited map() value. I think the code is clear what I am trying to achieve. however as a result i get:
Code:
0
0
0
0
0
0
0
0
0
0
4
3
2
1
as you can see my numbers are at the end of the vector , which is exactly a behaviour one would expect . but is there a clever way to get those numbers at the begining of the array ?? (remapping as post processing is one way to go but is there a slicker solution ??)
thnx
ps
array needs to be filled with 0 apriori