Say I have a problem statement where my function receives a string (all alphabets) and I need to store the indices of all the alphabets. For example
If input is aabbacd
then I need something like
a -> 0, 1, 4
b -> 2, 3
c -> 5
d -> 6
Now, I wanted to create a map of char to vector, but ended up creating a map of char to vector*.
function(String s)
{
map<char, vector<int> > m;
for(i = 0; i < s.size(); i++)
{
if(m.find(s[i]) == m.end())
{
//create new vector and push 'i'
vector<int>* v = new vector<int>(); // If I create a vector here, then that will only be live till the closing brace of the if
.... // rest of the code
Given c++ stl already handles all pointer management, I understand creating a vector* is not the best thing to do. But what can I do in this case, so that the vector created inside the if block remain live outside as well?