I think I see where you're going, but you've fallen into a common trap for people who learned Java or C# before C++:
int a=1, b=1, c=1;
std::vector <int*> edges;
edges.resize(3);
edges[0] = new int; // this line is unnecessary
edges[0] = &a;
The call to new int
allocates memory for an int
, and stores the address in edges[0]
. But the very next line decides to point edges[0]
to the address of a
. The memory allocated by new int
is leaked.
What's more, the call to delete edges[0]
is effectively a call to delete &a
, which is not what you want at all (you can only delete
nullptr
or things you got from new
; and you did not get a
from new
).
Unlike Java or C#, new
actually means something in C++.
The calls to edges.erase(edges.begin())
are all good, though.
So, edited, this would work:
std::vector <int*> edges;
edges.resize(3);
edges[0] = new int(1);
...
delete edges[0];
edges.erase(edges.begin());
But I have to wonder why you aren't using std::vector<std::unique_ptr<int>>
, which will take care of the memory management for you:
std::vector<std::unique_ptr<int>> edges;
edges.resize(3);
edges[0] = std::unique_ptr<int>(new int(1)); // or edges[0] = std::make_unique(1); in C++14
...
edges.erase(edges.begin());
...
In fact, if int
isn't a stand-in for some other type (i.e., you actually want a container of int
s), you should just use std::vector<int>
:
std::vector<int> edges;
edges.resize(3);
edges[0] = 1;
edges[1] = 2;
edges[2] = 3;
edges.erase(edges.begin());
edges.erase(edges.begin());
edges.erase(edges.begin());