Question

I have a map<int, vector<int>> M. In some functions I want to use M[someInt] multiple times. Instead of having M[someInt].size(), M[someInt].push_back(123) etc all over the place I want to use a local variable instead.

I'm currently using vector<int> v = M[someInt]; however I don't want to copy the contents of the vector. I think I can do vector<int> *v = &M[someInt];

How can I create a local variable that refers to M[someInt] w/o copying the vector's contents and, ideally, w/o using a pointer?

Was it helpful?

Solution

Do vector<int>& v = M[someInt];.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top