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

ベクトルはC++でどのように機能しますか?


ベクトルには、要素が挿入または削除されたときに動的配列のように自動的にサイズを変更する機能があり、コンテナはストレージを自動的に処理します。ベクトル要素は、イテレータを使用してアクセスおよびトラバースできるように、連続したストレージに配置されます。データは、ベクトルの最初、中間、または最後で挿入または消去できます。

これは、ベクトルのさまざまな機能を実装するためのC++プログラムです。

アルゴリズム

Begin
   Declare a variable v of vector type.
   Declare another variable it as iterator of vector type.
   Declare another two variable c and i to the ineger datatype.
   while (1) do
      print “1.Size of the Vector”.
      print “2.Insert Element into the Vector”.
      print “3.Delete Last Element of the Vector”.
      print “4.Resize the vector”.
      print “5.Reserve the vector”.
      print “6.Display the capacity of vector”.
      print “7.Display by Iterator”.
      print “8.Clear the Vector”.
      print “9.Exit”.
      print “Enter your Choice:”.
      Enter the value of variable c.
      Switch(c)
         Case 1.
            Print “Size of Vector:”.
            Call size() function to print the size of the
            vector.
            Break.
         Case 2.
            Print “Enter value to be inserted:”.
            Enter the value of variable i.
            Call push_back() function to input the values in the
            vector.
            Break.
         Case 3.
            Print “Delete Last Element Inserted:”.
            Call pop_back() function to delete the values in
            the vector.
            Break.
         Case 4.
            Print “Resize the vector elements:”.
            Call resize() function to resize the vector.
            Break.
         Case 5.
            Print “Reserve the vector elements:”.
            Call reserve() function to reserve the size of
            the vector.
            Break.
         Case 6.
            Print “Displaying capacity of vector:”.
            Call capacity() function to display the capacity
            of the vector.
            Break.
         Case 7.
            Print “Displaying Vector by Iterator:”.
            for (it = v.begin(); it != v.end(); it++)
               print the value of iterator it.
            Break.
         Case 8.
            Call clear() function to clear the vector.
            Print “Vector Cleared”.
            break
         case 9.  
            call exit() function to take exit.  
            break.
         Default.
            Print “Wrong choice”.
End.

サンプルコード

#include <iostream>
#include <vector>
using namespace std;
int main() {
   vector<int> v;
   vector<int>::iterator it;
   int c, i;
   while (1) {
      cout<<"1.Size of the Vector"<<endl;
      cout<<"2.Insert Element into the Vector"<<endl;
      cout<<"3.Delete Last Element of the Vector"<<endl;
      cout<<"4.Resize the vector"<<endl;
      cout<<"5.Reserve vector"<<endl;
      cout<<"6.Display the capacity of vector"<<endl;
      cout<<"7.Display by Iterator"<<endl;
      cout<<"8.Clear the Vector"<<endl;
      cout<<"9.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of Vector: ";
            cout<<v.size()<<endl;
         break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            v.push_back(i);
         break;
         case 3:
            cout<<"Delete Last Element Inserted:"<<endl;
            v.pop_back();
         break;
         case 4:
            cout<<"Resize the vector elements:"<<endl;
            v.resize(10);
         break;
         case 5:
            cout<<"Reserve the vector elements:"<<endl;
            v.reserve(100);
         break;
         case 6:
            cout<<"Displaying capacity of vector: ";
            cout<<v.capacity()<<endl;
         break;
         case 7:
            cout<<"Displaying Vector by Iterator: ";
            for (it = v.begin(); it != v.end(); it++) {
               cout<<*it<<" ";
            }
            cout<<endl;
         break;
         case 8:
            v.clear();
            cout<<"Vector Cleared"<<endl;
         break;
         case 9:
            exit(1);
         break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

出力

1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 1
Size of Vector: 0
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 3
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 5
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 6
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 7
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 8
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 9
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 10
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 11
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 2
Enter value to be inserted: 12
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 1
Size of Vector: 12
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 7
Displaying Vector by Iterator: 1 3 2 4 5 6 7 8 9 10 11 12
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 4
Resize the vector elements:
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 7
Displaying Vector by Iterator: 1 3 2 4 5 6 7 8 9 10
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 3
Delete Last Element Inserted:
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 7
Displaying Vector by Iterator: 1 3 2 4 5 6 7 8 9
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 8
Vector Cleared
1.Size of the Vector
2.Insert Element into the Vector
3.Delete Last Element of the Vector
4.Resize the vector
5.Reserve vector
6.Display the capacity of vector
7.Display by Iterator
8.Clear the Vector
9.Exit
Enter your Choice: 9

  1. C++で例外はどのように機能しますか

    C ++では、例外処理はランタイムエラーを処理するプロセスです。例外は、C++で実行時にスローされるイベントです。すべての例外は、std::exceptionクラスから派生します。処理可能なランタイムエラーです。例外を処理しない場合は、例外メッセージを出力してプログラムを終了します。 例外は、C ++標準では、プログラム内で使用できるクラスとして定義されています。親子クラス階層の配置を以下に示します- C++の一般的な例外クラスは-です。 Sr.No。 例外と説明 1 std ::exception これは、すべての標準C++例外の例外および親クラスです。

  2. Snapchat の仕組み

    Snapchat がモバイル メッセージング アプリの世界に革命をもたらしたと言っても過言ではありません。 Snapchat が登場する前は、友人とのコミュニケーションにこれほどユニークな方法があるとは誰も夢にも思いませんでした。 Snapchat がもたらす自己破壊的な画像や動画という概念は、通常のメッセージやソーシャル ネットワーキング アプリとはまったく異なります。そしてこれが、2011 年の開始以来、Snapshot が現在 1 億 8000 万人を超える毎日のアクティブ ユーザーを持つ最も人気のあるアプリの 1 つになっている理由です。また、報告によると、ファンのフォローはティーンエ