C++ 整数数组到向量

C++ 整数数组到向量,c++,arrays,vector,C++,Arrays,Vector,我正在尝试将int数组添加到向量中。无论我做什么,调试器都会指示新的vector元素只是“0” std::vectorlevel\u集合; 用于(自动和输入:级别) { 自动大小=标准::获取(i).size(); int level_数据[大小]; 对于(大小x=0;x LealOracle Cuths?@ USE3684240 LealOffice数据被添加到LealIsCopyDela.是代码>大小< /COD>常量表达式?如果不是, int LoopyDATA [大小];< /C> >

我正在尝试将int数组添加到向量中。无论我做什么,调试器都会指示新的vector元素只是“0”

std::vectorlevel\u集合;
用于(自动和输入:级别)
{
自动大小=标准::获取<1>(i).size();
int level_数据[大小];
对于(大小x=0;x(i)[x];
}
用于(自动x:级别_数据)
{

std::cout当你
向后推一个指向向量的指针时,你实际上并没有保留这个指针指向的内存。因此,这在这里不起作用

相反,您应该使用拥有
int
数组的对象的向量,例如另一个
std::vector
。您只需更改两行:

std::vector< std::vector<int> > level_collection; // CHANGED
for( auto & i : levels )
{
     auto size = std::get< 1 >(i).size();
     std::vector<int> level_data{size}; // CHANGED
     for( size_t x = 0; x < size; x ++ )
     {
          level_data[x] = std::get< 1 >(i)[x];
     }

     for( auto x : level_data)
     {
          std::cout << x << std::endl; // This works. All the values print correctly. So it did store the information as it should.
     }

     level_collection.push_back( level_data );
 }
 for( auto & i : level_collection)
 {
     std::cout << i[1] << std::endl; // This prints ALL 0s. Despite the new element not having that value.
 }
std::vectorlevel_集合;//已更改
用于(自动和输入:级别)
{
自动大小=标准::获取<1>(i).size();
std::向量级_数据{size};//已更改
对于(大小x=0;x(i)[x];
}
用于(自动x:级别_数据)
{

STD::你想添加哪个数组?我假设“代码> LoopyDATA < /C++ > <代码> LealOracle Cuths<代码>?@ USE3684240 LealOffice数据被添加到LealIsCopyDela.是代码>大小< /COD>常量表达式?如果不是,<代码> int LoopyDATA [大小];< /C> >不是标准C++。您输入的向量在声明< <代码> LeavyDATA < /C> >的范围之外是无效的。什么是<代码> VyLeaSysOuts<代码>?考虑使用<代码> STD::数组< /COD>。每个数组,即<代码> PuthSubBuff<代码> ED在每个循环迭代结束时被销毁。因此,每个指针都是一个悬空指针,取消引用,导致UNN。定义的行为。
std::vector< std::vector<int> > level_collection; // CHANGED
for( auto & i : levels )
{
     auto size = std::get< 1 >(i).size();
     std::vector<int> level_data{size}; // CHANGED
     for( size_t x = 0; x < size; x ++ )
     {
          level_data[x] = std::get< 1 >(i)[x];
     }

     for( auto x : level_data)
     {
          std::cout << x << std::endl; // This works. All the values print correctly. So it did store the information as it should.
     }

     level_collection.push_back( level_data );
 }
 for( auto & i : level_collection)
 {
     std::cout << i[1] << std::endl; // This prints ALL 0s. Despite the new element not having that value.
 }