C++ 如何在初始化后为数组赋值

C++ 如何在初始化后为数组赋值,c++,c++11,c++14,C++,C++11,C++14,我正在初始化一个数组 float verticesRect[] = { // Positions // Normal Coords // Texture Coords width, height, 0.0f, 0.0 , 0.0, 1.0 , 1.0f, 0.0f, // Top Right width, -height, 0.0f, 0.0 , 0.0, 1.0 , 1.0f, 1

我正在初始化一个数组

float verticesRect[] = {
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };
初始化后,我可以再次使用数组索引运算符[]将值写入数组

verticesRect[0] = 3;
但是要改变所有的值,我必须通过所有的索引,我可以这样做吗

verticesRect = {
            // Positions        // Normal Coords          // Texture Coords
            0.0,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
            0.0, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
           -10.0, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
           -10.0, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
        };

您可以使用std::vector进行此操作

std::vector<float> verticesRect =  {
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };


    verticesRect = {
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };
std::vector verticestrect={
//位置//法线坐标//纹理坐标
宽度、高度、0.0f、0.0、0.0、1.0、1.0f、0.0f、//右上角
宽度,-高度,0.0f,0.0,0.0,1.0,1.0f,1.0f,//右下角
-宽度,-高度,0.0f,0.0,0.0,1.0,0.0f,1.0f,//左下角
-宽度、高度、0.0f、0.0、0.0、1.0、0.0f、0.0f//左上角
};
垂直直线={
//位置//法线坐标//纹理坐标
宽度、高度、0.0f、0.0、0.0、1.0、1.0f、0.0f、//右上角
宽度,-高度,0.0f,0.0,0.0,1.0,1.0f,1.0f,//右下角
-宽度,-高度,0.0f,0.0,0.0,1.0,0.0f,1.0f,//左下角
-宽度、高度、0.0f、0.0、0.0、1.0、0.0f、0.0f//左上角
};

您可以使用std::vector进行此操作

std::vector<float> verticesRect =  {
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };


    verticesRect = {
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };
std::vector verticestrect={
//位置//法线坐标//纹理坐标
宽度、高度、0.0f、0.0、0.0、1.0、1.0f、0.0f、//右上角
宽度,-高度,0.0f,0.0,0.0,1.0,1.0f,1.0f,//右下角
-宽度,-高度,0.0f,0.0,0.0,1.0,0.0f,1.0f,//左下角
-宽度、高度、0.0f、0.0、0.0、1.0、0.0f、0.0f//左上角
};
垂直直线={
//位置//法线坐标//纹理坐标
宽度、高度、0.0f、0.0、0.0、1.0、1.0f、0.0f、//右上角
宽度,-高度,0.0f,0.0,0.0,1.0,1.0f,1.0f,//右下角
-宽度,-高度,0.0f,0.0,0.0,1.0,0.0f,1.0f,//左下角
-宽度、高度、0.0f、0.0、0.0、1.0、0.0f、0.0f//左上角
};

<代码> > p>在C++的情况下,100%的时间使用<代码> STD::vector < /C> >或者更好的是<代码> STD::数组< /C>。但如果您想坚持使用c样式数组,可以使用指针和动态分配:

    float* verticesRect = new float[32]{
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };
    delete verticesRect;
    verticesRect = new float[32]{
            // Positions        // Normal Coords          // Texture Coords
            0.0,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
            0.0, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
           -10.0, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
           -10.0, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
        };

<> P>在C++的情况下,100%的时间使用<代码> STD::vector < /代码>,或者更好的是<代码> STD::数组< /C>。但如果您想坚持使用c样式数组,可以使用指针和动态分配:

    float* verticesRect = new float[32]{
        // Positions        // Normal Coords          // Texture Coords
        width,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
        width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
       -width, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
       -width, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
    };
    delete verticesRect;
    verticesRect = new float[32]{
            // Positions        // Normal Coords          // Texture Coords
            0.0,  height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 0.0f,   // Top Right
            0.0, -height, 0.0f,    0.0 , 0.0, 1.0 ,     1.0f, 1.0f,   // Bottom Right
           -10.0, -height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 1.0f,   // Bottom Left
           -10.0, height, 0.0f,    0.0 , 0.0, 1.0 ,     0.0f, 0.0f    // Top Left 
        };