Any idea why this throws that exception? Compiler: Visual C++ 2013 Update 2
1 2 3
auto arrPtr = boost::any_cast<std::vector<uint16_t>>(&it->second);
m_someArray.reserve(arrPtr->size());
std::copy(arrPtr->begin(), arrPtr->end(), m_usIgnoredDropList.begin());
Where
m_someArray is std::vector<uint16_t>
This works:
std::copy(arrPtr->begin(), arrPtr->end(), std::back_inserter(m_someArray));
Also
It doesn't matter how many items I reserve, it still throws.
Fixed. Sorry for stupid question. I used reserve + back_inserter for strings so I didn't know I had to use resize in this case.
It doesn't matter how many items I reserve, it still throws.
reserve has to do with the capacity of the vector. It does not make the size of the array bigger. Rather, it has to do with the number of elements that may be added to the vector without causing a reallocation.
If, for instance, we do the following:
1 2 3 4 5
std::vector<int> v ;
v.reserve(64) ;
// v is empty here so the following is undefined behavior:
v[0] = 0 ;
And this sounds like the issue you're having. The code with std::back_inserter will actually insert elements into the vector. The code without it will only try to copy over existing elements (and, again, that number of elements is not at all affected by reserve.)