C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

ユーザー定義のサイズのC++の2Dベクトル


ベクトルのベクトルは2Dベクトルと呼ばれます。

アルゴリズム

Begin
   Declare a variable v to the 2D vector type.
   Initialize values to the vector v.
   Print “the 2D vector is:”.
   for (int i = 0; i < v.size(); i++)
      for (int j = 0; j < v[i].size(); j++)
         print the value of 2D vector v[i][j].
End.

#include <iostream>
#include <vector> //header file for 2D vector in C++
using namespace std;
int main() {
   vector<vector<int> > v{ { 4,5, 3, 10 }, // initializing 2D vector with values.
   { 2, 7, 11 },
   { 3, 2, 1, 12 } };
   cout<<"the 2D vector is:"<<endl;
   for (int i = 0; i < v.size(); i++) { // printing the 2D vector.
      for (int j = 0; j < v[i].size(); j++)
      cout << v[i][j] << " ";
      cout << endl;
   }
   return 0;
}

出力

the 2D vector is: 
4 5 3 10
2 7 11 
3 2 1 12

  1. C++で3nスライスのピザ

    さまざまなサイズの3nスライスのピザがあるとすると、私と2人の友人は次のようにピザのスライスを取ります- ピザのスライスを選びます。 友達のアマルが私のピックの反時計回りに次のスライスをピックします。 友達のBimalが、私のピックの時計回りに次のスライスをピックします。 ピザのスライスがなくなるまで、これらの手順を繰り返します。 ピザスライスのサイズは、時計回りの円形配列スライスで表されます。可能な最大のスライスサイズの合計を見つける必要があります。 したがって、入力が[9,8,6,1,1,8]のような場合、 次に、各ターンでサイズ8のピザスライスを選

  2. C++でstd::vectorをシャッフルする方法

    ベクトルシャッフルは、Fisher-Yatesシャッフルアルゴリズムで実行できます。 このアルゴリズムでは、ベクトルの線形スキャンが実行され、各要素が、要素自体を含む残りのすべての要素の中でランダムな要素と交換されます。 アルゴリズム Begin   Declare a function show().       Pass a constructor of a vector as a parameter within show() function.       for (auto const& i: input