Skip to content

Instantly share code, notes, and snippets.

@gidden
Last active December 26, 2015 13:49
Show Gist options
  • Save gidden/7161374 to your computer and use it in GitHub Desktop.
Save gidden/7161374 to your computer and use it in GitHub Desktop.
no container of pointers to objects in other containers
struct request {
string commod;
double pref;
}
class context {
public:
// adding this in the constructor will fix it until resizing occurs
// context() {
// int reserve_size = 16 // a magic number
// requests_.reserve(reserve_size)
// }
void AddRequest(const request& r) {
requests_.push_back(r); // copy r, refer only to our copy in the future
const request& r = requests_.back();
requests_by_commod_[r.commod].push_back(&r)
}
private:
vector<request> requests_;
map<string, vector<request*> > requests_by_commod_;
}
@gidden
Copy link
Author

gidden commented Oct 25, 2013

this fails because the address becomes invalidated when the vector resizes itself. see http://stackoverflow.com/questions/19560064/segmentation-fault-when-accessing-second-element-in-obj-vector-using-pointer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment