I'm trying to copy a 3D array into a 3D vector that has the same dimensioning as the array. There's no problem if I use a nested loop to make the copy, and the program compiles OK if I try to use std::copy, but the program throws an exception when it runs (access violation in memcpy.asm at CopyUpDword Loop). Obviously I'm going out of bounds somewhere, but why?
Using a nested loop to copy the array into the vector and examining the vector before and after the copy shows that the vector is sized properly.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<string> vsCols;
typedef vector<vsCols> vsRows;
typedef vector<vsRows> vsPage;
string Array[2][3][2];
int main()
{
// dimension the vector same as the array
vsPage pages(2);
for (size_t page = 0; page < pages.size(); page++) {
pages[page].resize(3);
for (size_t row = 0; row < pages[page].size(); row++) {
pages[page][row].resize(2);
}
}
// fill Array
string s;
for (int page = 0; page < 2; page++) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 2; col++) {
s = "Item" + to_string(((page + 1) + (row + 2)) * (col + 1));
Array[page][row][col] = s;
}
}
}
// throws the exception described above
copy(&Array[0][0][0], &Array[0][0][0] + 2 * 3 * 2, &pages[0][0][0]);
The syntax is similar to an example I saw here that used std::copy to copy one 3D array to another 3D array, so I expected it to also work with a vector as the copy destination.