C++ 使用用户定义大小的二维向量

C++ 使用用户定义大小的二维向量

本教程旨在解释具有用户定义大小的二维向量的概念。我们必须了解数组是二维的2D数组,可以将其视为矩阵。

在这里,向量的概念解决了固定大小集合的中心痛点,其中向量的概念是动态的。

二维向量只是一个向量的向量,并且是核心所在。二维向量的时间和空间复杂度为O(1)。

方法1

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include 
using namespace std;
// The main driver code functionality starts from here
int main()
{

/*  theory behind vector implementation

    1. vector variable_name (in case of a 2d vector)

    In the case of a 2D vector

    All we do is create

    A vector of datatype vector.

    We replace "datatype" with "vector":

    1. vector> variable_name (simple change to create a vector)

*/

    vector> vect;

    return 0;
// the main driver code functionality ends from here
}

方法2

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include 
#include 
using namespace std;
// The main driver code functionality starts from here
int main()
{

    // In the below code, we have tried to initialize a 2D vector
    // named "vect" on line 12 and then
    // we declare the values on
    // lines code respectively.


    vector> vect
    {
        {11, 2, 33},
        {74, 5, 66},
        {7, 98, 9}
    };

    // Now, from the below code, we are trying to print the values that
    // we just declared on lines
    // code using a simple
    // nested for loop.

    for (int i = 0; i < vect.size(); i++)
    {
        // this is inner for loop to implement the vector code 
        // functionality
        for (int j = 0; j < vect[i].size(); j++)
        {
            cout << vect[i][j] << " ";
        }   
        cout << endl;
    }

    return 0;
    // the main driver code functionality ends from here
}

输出:

11 2 33 
74 5 66 
7 98 9

方法3

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include 
#include 
using namespace std;
// The main driver code functionality starts from here
int main()
{
    int n = 4;
    int m = 5;


//  here, we are trying to create a vector that contains the variable "n."
//  and a vector of functionality each of size "m."

    vector> vec( n , vector (m));
// for loop to run through the variables n to m
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            vec[i][j] = j + i + 1;
        }
    }
// for loop to run through the variables m to n
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }

return 0;
// The main driver code functionality ends from here
}

输出:

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

方法4

C++ 代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include 
#include 
using namespace std;
// The main driver code functionality starts from here
int main()
{
    // below are the limits of variables n and m limits to loop
    int n = 3;
    int m = 4;

    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;
    // The main driver code functionality ends from here
}

输出:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程