2015-08-31 66 views
1

看着下面的代码片段。在所有平台上的所有情况下,输出是否始终为假?重新分配矢量是否会导致父矢量的重新分配?

std::vector<std::vector<int>> array(5); 
    array[0].resize(1); 
    std::vector<int>* arr_start = array.data(); 
    int* p_start = array[0].data(); 

    while(p_start == array[0].data()) 
    { 
    array[0].push_back(0); 
    } 

    std::cout << "Does a reallocation in a vector leads to a reallocation in the parent vector? " 
      << array.data() != arr_start; 
+4

您的标题问题的答案是否定的。 –

回答

5

只允许在vector上的某些操作导致重新分配。对vector元素的操作从不这样做。

元素本身是vector的事实不会改变这一点。