📜  用户定义大小的C++中的2D矢量

📅  最后修改于: 2021-05-30 05:09:45             🧑  作者: Mango

2D向量是该向量的向量。像2D数组一样,我们可以声明2D向量并将其赋值给2D向量!
假设您熟悉C++中的法向矢量,并借助示例,我们将说明2D向量与以下法向矢量的不同之处:

C++
/* Vectors belong to a C++ library
   called STL so we need to import
   it first! */
#include 
using namespace std;
int main()
{
    /*
    In the case of a normal vector we initialize it as:
     
    1. vector variable_name
     
    Now in the case of a 2D vector all we do is create
    a vector of datatype vector.
     
    We simply replace "datatype" with "vector":
     
    1. vector> variable_name
     
    That's literally it! We just created a 2D vector!
    On line 23 below we declare an actual 2D vector
    named "vect".
    */
     
    vector> vect;
 
    return 0;
}


C++
/* C++ code to demonstrate a 2D vector
   with elements(vectors) inside it. */
#include 
#include 
using namespace std;
 
int main()
{
    /*
    Below we initialize a 2D vector
    named "vect" on line 12 and then
    we declare the values on
    line 14, 15 and 16 respectively.
    */
     
    vector> vect
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
     
    /*
    Now we print the values that
    we just declared on lines
    14, 15 and 16 using a simple
    nested for loop.
    */
     
    for (int i = 0; i < vect.size(); i++)
    {
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }   
        cout << endl;
    }
 
    return 0;
}


C++
/*
C++ program to demonstrate a 2D vector where
each of its elements is of different size.
*/
#include 
#include 
using namespace std;
int main()
{
    /*
    We initialize a 2D vector
    named "vect" on line 16 with
    different number of values
    in each element.
    */
     
    vector> vect
    {
        /* Element one with 2 values in it. */
        {1, 2},
       
        /* Element two with 3 values in it. */
        {4, 5, 6},
       
         /* Element three with 4 values in it. */
        {7, 8, 9, 10}
    };
 
    /*
    Now we print the vector that we
    just defined using a simple
    nested for loop.
    */
     
    for (int i = 0; i < vect.size(); i++)
    {
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }   
        cout << endl;
    }
    return 0;
}


C++
/*
C++ program to create a 2D vector where
every row has a certain number of values
as defined by the user.(On line 13)
*/  
 
#include 
#include 
using namespace std;
int main()
{
     
    /* Here we tell how many rows
    the 2D vector is going to have. */
    int row = 5;
   
    /* We define the number of values
    each row is supposed to have. */
    int colom[] = {5, 3, 4, 2, 1};
 
    /*
    We now create a vector of vector with size
    equal to row.
    */
     
    vector> vec(row);
    /*
    On line 21 we created a 2D vector and assigned
    it a capacity of "row"(in this case 5) units.
    */
     
    /*
    Now we will proceed to create the structure of
    our 2D vector by assigning the value of rows and
    columns through a nested for loop.
    */
 
    for(int i = 0; i < row; i++)
    {  
        /* Declaring the size of the column. */
        int col = colom[i];
 
        /*
        On the 43rd line we declare the
        i-th row to the size of the column.
        We create a normal vector of capacity "col" which
        in every iteration of the for loop will define the
        values inside of each row.
        */
        vec[i] = vector(col);
        for(int j = 0; j < col; j++)
        {
            vec[i][j] = j + 1;
        }   
    }
     
    /*
    We now finally use a simple nested for loop
    to print the 2D vector that we just created above.
    */
 
    for(int i = 0; i < row; i++)
    {
        for (int j = 0; j < vec[i].size(); j++)
        {
            cout << vec[i][j] << " ";
        }   
        cout << endl;
    }
    return 0;
}


C++
// CPP program
#include 
#include 
using namespace std;
int main()
{
    int n = 3;
    int m = 4;
 
    /*
    We create a 2D vector containing "n"
    elements each having the value "vector (m, 0)".
    "vector (m, 0)" means a vector having "m"
    elements each of value "0".
    Here these elements are vectors.
    */
    vector> vec( n , vector (m, 0));
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout<< endl;
    }
     
    return 0;
}


C++
// CPP program
#include 
#include 
using namespace std;
int main()
{
    int n = 4;
    int m = 5;
 
    /*
    Create a vector containing "n"
    vectors each of size "m".
    */
    vector> vec( n , vector (m));
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            vec[i][j] = j + i + 1;
        }
    }
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
     
   return 0;
}


在2D向量中,每个元素都是一个向量。

C++

/* C++ code to demonstrate a 2D vector
   with elements(vectors) inside it. */
#include 
#include 
using namespace std;
 
int main()
{
    /*
    Below we initialize a 2D vector
    named "vect" on line 12 and then
    we declare the values on
    line 14, 15 and 16 respectively.
    */
     
    vector> vect
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
     
    /*
    Now we print the values that
    we just declared on lines
    14, 15 and 16 using a simple
    nested for loop.
    */
     
    for (int i = 0; i < vect.size(); i++)
    {
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }   
        cout << endl;
    }
 
    return 0;
}

输出 :

1 2 3 
4 5 6 
7 8 9 

像Java的锯齿状数组一样,二维向量的每个元素可以包含不同数量的值。

C++

/*
C++ program to demonstrate a 2D vector where
each of its elements is of different size.
*/
#include 
#include 
using namespace std;
int main()
{
    /*
    We initialize a 2D vector
    named "vect" on line 16 with
    different number of values
    in each element.
    */
     
    vector> vect
    {
        /* Element one with 2 values in it. */
        {1, 2},
       
        /* Element two with 3 values in it. */
        {4, 5, 6},
       
         /* Element three with 4 values in it. */
        {7, 8, 9, 10}
    };
 
    /*
    Now we print the vector that we
    just defined using a simple
    nested for loop.
    */
     
    for (int i = 0; i < vect.size(); i++)
    {
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }   
        cout << endl;
    }
    return 0;
}   

输出 :

1 2 
4 5 6 
7 8 9 10 

练习题:用不同大小的列定义2D向量。
例子:

Input : Number of rows : 5 
        Number of columns in rows : 
        2 3 4 5 1
Output : 1 2
         1 2 3
         1 2 3 4
         1 2 3 4 5 
         1

Input : Number of rows : 3
        Number of columns in rows : 
        3 2 1

Output : 1 2 3
         1 2
         1

2D向量通常被视为内部带有“行”和“列”的矩阵。实际上,它们实际上是2D矢量的元素。
我们首先声明一个名为“ row”的整数变量,然后声明一个名为“ column”的数组,该数组将保存每行大小的值。

之后,我们将根据列的大小来初始化每一行的内存。

C++

/*
C++ program to create a 2D vector where
every row has a certain number of values
as defined by the user.(On line 13)
*/  
 
#include 
#include 
using namespace std;
int main()
{
     
    /* Here we tell how many rows
    the 2D vector is going to have. */
    int row = 5;
   
    /* We define the number of values
    each row is supposed to have. */
    int colom[] = {5, 3, 4, 2, 1};
 
    /*
    We now create a vector of vector with size
    equal to row.
    */
     
    vector> vec(row);
    /*
    On line 21 we created a 2D vector and assigned
    it a capacity of "row"(in this case 5) units.
    */
     
    /*
    Now we will proceed to create the structure of
    our 2D vector by assigning the value of rows and
    columns through a nested for loop.
    */
 
    for(int i = 0; i < row; i++)
    {  
        /* Declaring the size of the column. */
        int col = colom[i];
 
        /*
        On the 43rd line we declare the
        i-th row to the size of the column.
        We create a normal vector of capacity "col" which
        in every iteration of the for loop will define the
        values inside of each row.
        */
        vec[i] = vector(col);
        for(int j = 0; j < col; j++)
        {
            vec[i][j] = j + 1;
        }   
    }
     
    /*
    We now finally use a simple nested for loop
    to print the 2D vector that we just created above.
    */
 
    for(int i = 0; i < row; i++)
    {
        for (int j = 0; j < vec[i].size(); j++)
        {
            cout << vec[i][j] << " ";
        }   
        cout << endl;
    }
    return 0;
}

输出:

1 2 3 4 5
1 2 3 
1 2 3 4
1 2
1

另一种方法
假设我们要初始化一个二维矢量,其中包含“ n”行和“ m”列,其值为0。

C++

// CPP program
#include 
#include 
using namespace std;
int main()
{
    int n = 3;
    int m = 4;
 
    /*
    We create a 2D vector containing "n"
    elements each having the value "vector (m, 0)".
    "vector (m, 0)" means a vector having "m"
    elements each of value "0".
    Here these elements are vectors.
    */
    vector> vec( n , vector (m, 0));
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout<< endl;
    }
     
    return 0;
}

输出:

0 0 0 0 
0 0 0 0
0 0 0 0

另一种方法:
假设我们要创建一个由“ n”行, “ m”列和输入值组成的2D向量。

C++

// CPP program
#include 
#include 
using namespace std;
int main()
{
    int n = 4;
    int m = 5;
 
    /*
    Create a vector containing "n"
    vectors each of size "m".
    */
    vector> vec( n , vector (m));
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            vec[i][j] = j + i + 1;
        }
    }
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
     
   return 0;
}

输出:

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

希望您对本文有所了解,并有足够的信心自行使用它们。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”