#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
vector<pair<int, int>*> *v;
pair<int, int> *x = new pair<int, int>();
cin >> x->first >> x->second;
v->push_back(x);
cout << v->size() << endl;
return 0;
}
Why does this code get run-time error? I dont understand. All I do is appending a pair pointer to a vector of pair pointer.
First of all: Vector definition should be (this resolves the run-time error):
vector<pair<int, int>*> v;
Second, you don't release the memory of the new pair. This will solve that issue too:
vector<pair<int, int>> v;
pair<int, int> x{ 0,0 };
A vector of pairs, rather than a vector of pair-pointers. On the other hand: If it necessary to use a vector of pointers, smart pointers is a better choice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.