如何从双精度向量的向量访问数据



我有一个变量顶点;如何分配数据并从顶点获取数据?我知道一种通过创建一个临时变量然后Vertex.push_back(temp)来分配值的方法。是否有任何定向方法来分配和获取数据?

vector<vector<double>> Vertex;
vector<double> temp;
Vertex.push_back(temp); //Any other direct method?

鉴于您在>>之间没有空格,我认为使用 C++11 可以吗?如果是这样,您可以初始化列表,例如

#include <iostream>
#include <vector>
int main()
{
    std::vector<std::vector<double>> Vertex;
    Vertex.push_back({1.0,2.0,3.0});
    Vertex.push_back({4.5,2.5,0.5});
    std::cout << Vertex[0][1] << 'n'    // prints '2'
              << Vertex[1][2] << 'n';   // prints '0.5'
}

你在找emplace_back吗?

vector<vector<double>> Vertex;
Vertex.emplace_back();
//or
Vertex.emplace_back(100, 1.0); //Creates new vector of 100 doubles initialized with 1.0
vector<vector<double>> Vertex;
    for(int i=1; i<n; i++)
    {   
      Vertex.resize(i);
      Vertex[i-1].push_back(11);
    Vertex[i-1].push_back(112);
    std::cout<<Vertex[i-1][0]<<Vertex[i-1][1];
    }

最新更新