Last active
December 26, 2015 13:49
-
-
Save gidden/7161374 to your computer and use it in GitHub Desktop.
no container of pointers to objects in other containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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