vector <Mat> redAd(adFileName.size());
fill(redAd.begin(), redAd.end(), NULL);
for (int i = 0; i < adFileName.size(); ++i) {
cout << adFileName[i].c_str() << endl;
Mat im = imread(adFileName[i].c_str(), 1);
cout << im.data << endl;
redAd.push_back(im);
cout << redAd[i].data << endl;
}
Here, adFileName
is the string vector containing the file paths of the images. The aths are printed correctly. im.data
gives a non-null value. But after pushing it into a vector <Mat>
and printing an element shows "Application has stopped working".
How should it be solved ?
Because, you set the vector size to adFileName.size() and then you push_back the new elements. You should either contruct the vector with 0 size and use push_back or else use insert.
redAd.insert(redAd.begin()+i,1,im);
Instead of fill
with NULLs, which is adding to the problem, you could just call the default vector constructor vector <Mat> redAd;
and then call redAd.reserve(adFileName.size())
and then leave the loop as is.
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.