stl向量_用戶定義大小的C ++ STL中的2D向量

stl向量

C ++ STL中的2D矢量 (2D Vector in C++ STL)

In C++ STL, a 2D vector is a vector of vector.

在C ++ STL中,二維向量是向量的向量。

Syntax to declare a 2D vector:

聲明2D向量的語法:

    vector<vector<T>> vector_name{ {elements}, {elements}, ...};

1) C++ STL code to declare and print a 2D Vector (with same number of elements)

1)使用C ++ STL代碼聲明和打印2D向量(具有相同數量的元素)

// C++ STL code to declare and print a 2D Vector
#include <iostream>
#include <vector> // for vectors
using namespace std;
int main()
{
// Initializing 2D vector "v1" with
// same number of vector elements
vector<vector<int> > v1{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Printing the 2D vector's elements
for (int i = 0; i < v1.size(); i++) {
for (int j = 0; j < v1[i].size(); j++)
cout << v1[i][j] << " ";
cout << endl;
}
return 0;
}

Output

輸出量

1 2 3
4 5 6
7 8 9

2) C++ STL code to declare and print a 2D Vector (with different number of elements)

2)使用C ++ STL代碼聲明和打印2D向量(具有不同數量的元素)

// C++ STL code to declare and print a 2D Vector
#include <iostream>
#include <vector> // for vectors
using namespace std;
int main()
{
// Initializing 2D vector "v1" with
// different number of vector elements
vector<vector<int> > v1{ { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };
// Printing the 2D vector's elements
for (int i = 0; i < v1.size(); i++) {
for (int j = 0; j < v1[i].size(); j++)
cout << v1[i][j] << " ";
cout << endl;
}
return 0;
}

Output

輸出量

1 2 3
4 5
6 7 8 9

3) C++ STL code to declare and print a 2D Vector (Numbers of rows, columns and elements input by the user)

3)使用C ++ STL代碼聲明和打印2D向量(用戶輸入的行數,列數和元素數)

// C++ STL code to declare and print a 2D Vector
#include <iostream>
#include <vector> // for vectors
using namespace std;
int main()
{
int row;
int col;
// Input rows & columns
cout << "Enter number of rows: ";
cin >> row;
cout << "Enter number of columns: ";
cin >> col;
// Declaring 2D vector "v1" with
// given number of rows and columns
// and initialized with 0
vector<vector<int> > v1(row, vector<int>(col, 0));
// Input vector's elements
for (int i = 0; i < v1.size(); i++) {
for (int j = 0; j < v1[i].size(); j++) {
cout << "Enter element: ";
cin >> v1[i][j];
}
}
// Printing the 2D vector's elements
cout << "2D vector elements..." << endl;
for (int i = 0; i < v1.size(); i++) {
for (int j = 0; j < v1[i].size(); j++)
cout << v1[i][j] << " ";
cout << endl;
}
return 0;
}

Output

輸出量

Enter number of rows: 3
Enter number of columns: 4
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 5
Enter element: 6
Enter element: 7
Enter element: 8
Enter element: 9
Enter element: 10
Enter element: 11
Enter element: 12
2D vector elements...
1 2 3 4
5 6 7 8
9 10 11 12

翻譯自: https://www.includehelp.com/stl/2d-vector-in-cpp-stl-with-user-defined-size.aspx

stl向量

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378951.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378951.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378951.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

librtmp分析(接收數據包處理)

RTMP詳細分析&#xff08;三次握手&#xff09; RTMP詳細分析(Message 消息&#xff0c;Chunk分塊) librtmp分析&#xff08;發送數據包處理&#xff09; rtmp協議中的message的接收涉及有message 組合多個chunk、相對時間戳計算絕對值。 分析一下librtmp庫中的int RTMP_ReadP…

動態可緩存的內容管理系統(CMS)(轉)

摘要&#xff1a;內容管理系統(CMS)在各大商業站點和門戶站點中扮演著重要的角色&#xff0c;是內容有效組織和快速發布極為重要的基礎平臺。目前主流的內容發布系統都使用靜態頁面進行內容發布&#xff0c;在我們的實際使用過程中我們深切的感受到靜態內容發布存在著很多弊端&…

反轉字符串中的元音字符_C程序消除字符串中的所有元音

反轉字符串中的元音字符Given a string and we have to eliminate/ remove all vowels from the string using C program. 給定一個字符串&#xff0c;我們必須使用C程序從字符串中消除/刪除所有元音。 To eliminate/remove the vowels 消除/刪除元音 We will traverse (reac…

mysql 自動化 安裝_mysql自動化安裝

MySQL安裝一般使用RPM或者源碼安裝的方式。RPM安裝的優點是快速,方便.缺點是不能自定義安裝目錄.如果需要調整數據文件和日志文件的存放位置,還需要進行一些手動調整。源碼安裝的優點是可以自定義安裝目錄,缺點是編譯時間長,過程復雜其實還有一種方式,定制RPM包.它相當于用源碼…

hls協議分析

目錄1、簡介1.1、 綜述1.2 、HLS 協議編碼格式要求1.3 、HLS 協議優勢1.4 、HLS 協議劣勢1.5 、框架圖2、m3u8文件2.1 、單碼率適配流m3u8文件2.2 、多碼率適配流m3u8文件2.3 、Playlist file2.4 、Tags3、ts文件3.1 、ts文件結構3.2、ts文件結構部分截圖3.3、ts層&#xff08;…

OpenGL 學習筆記(1)初始化窗體

前言 學習OpenGL只是興趣愛好&#xff0c;因為對圖形比較感興趣.將以OpenGl的紅寶書(7)和藍寶石書(4)為基礎,雖然手頭有紅寶書書&#xff0c;但感覺沒藍寶石書寫的好 準備工作 首先要下載一個工具庫(GLUT) http://www.opengl.org/resources/libraries/glut/ 只要把相應文件放在…

基于云平臺的家居綜合監測管理系統的設計與實現

時間過得飛快&#xff0c;轉眼間大四即將畢業&#xff0c;有點留戀和不舍。可能是越是到了離別的時候&#xff0c;越開始珍惜吧。大一開始&#xff0c;通過考核進入了學校院系實驗室開始學習&#xff0c;這期間自學了很多東西&#xff0c;很充實&#xff0c;也參加過很多比賽&a…

小白學數據分析-----留存率分析_I[次日留存率突然下降了50%?]

最近在做留存分析時&#xff0c;遇到了不少的情況&#xff0c;也經常會有人問我&#xff0c;為什么我的游戲突然次日留存率降了一半。如果留存率是單單作為一個簡單的指標的話&#xff0c;那對你價值還是蠻有限的&#xff0c;今天就和大家說說一個case&#xff0c;這是不久前解…

mysql映射mapper_SQL映射器Mapper接口(MyBatis)

SQL映射器Mapper接口MyBatis基于代理機制&#xff0c;可以讓我們無需再寫Dao的實現。直接把以前的dao接口定義成符合規則的Mapper。注意事項&#xff1a;1&#xff0e;接口必須以Mapper結尾,名字是DomainMapper2&#xff0e;mapper.xml文件要和Mapper接口建立關系,通過namespac…

計算機圖形學畫線_在計算機圖形學中直接使用線方程

計算機圖形學畫線計算機圖形學| 直接使用線方程 (Computer Graphics | Direct Use of Line Equation) The standard line equation, as we all know is used for drawing a line. It is given by: y mx c. 眾所周知&#xff0c;標準線方程式用于繪制線。 由下式給出&#xff…

Request.ServerVariables (server environment variable)

參數 服務器環境變量指定要檢索的服務器環境變量名。可以使用下面列出的值。 變量說明ALL_HTTP客戶端發送的所有 HTTP 標題文件。ALL_RAW檢索未處理表格中所有的標題。ALL_RAW 和 ALL_HTTP 不同&#xff0c;ALL_HTTP 在標題文件名前面放置 HTTP_ prefix&#xff0c;并且標題名稱…

c/c++ 編程試題

c/c 編程試題 帶*號為選作題&#xff0c;給出代碼截屏和編譯運算結果截屏 1.編程:選取M個最大的數 編程實現從N個無序數中選取M個最大的數(0 < M < N ) 思路&#xff1a;通過冒泡排序或者選擇排序對N個數進行遞減排序&#xff0c;然后輸入前M個數即可。這里我想到的是通…

Java String startsWith()方法與示例

字符串startsWith()方法 (String startsWith() Method) startsWith() method is a String class method, it is used to check whether a given string starts with specific character sequences or not. startsWith()方法是一個String類方法&#xff0c;用于檢查給定的字符串…

mysql inception web_基于Inception搭建MySQL SQL審核平臺Yearing

Inception1. Inceptionj簡介Inception是一款針對MySQL的SQL語句審核自動化運維工具。使用Inception&#xff0c;將會給DBA帶來更大的便利性&#xff0c;將DBA從繁冗的工作中解放出來&#xff0c;做更多的自動化工作&#xff0c;或者從架構方面研究如何更大程度地保證數據庫的高…

C---日常練習

若有以下定義語句&#xff1a;int a5;printf("%d\n",a);則輸出結果是&#xff08;&#xff09; 解析&#xff1a;a 即先使用再自增&#xff0c;a的初始值即為5&#xff0c;則先使用&#xff0c;輸出結果為5 舉個例子&#xff1a; int a5,b; ba;//等價于 ba;aa1 prin…

VS2010 運行庫設置

如下圖所示&#xff0c;當在一個EXE工程中調用lib或dll時&#xff0c;2個工程的下面選項一定要一致&#xff0c;否則會導致exe工程編譯不過。 原則&#xff1a; Debug下&#xff0c;默認是MTd&#xff1b; Release下&#xff0c;默認是MT。 轉載于:https://www.cnblogs.com/lgh…

算法中的Strassen矩陣乘法

Introduction 介紹 Strassen in 1969 which gives an overview that how we can find the multiplication of two 2*2 dimension matrix by the brute-force algorithm. But by using divide and conquer technique the overall complexity for multiplication two matrices i…

零拷貝、mmap、sendfile

目錄零拷貝mmapsendFile總結零拷貝 要了解零拷貝&#xff0c;首先得先了解一下傳統 IO 的執行流程&#xff0c;這里舉個例子&#xff0c;通過傳統的 IO 進行網絡傳輸來傳輸一個文件。 先上一張圖&#xff0c;這張圖就代表了傳統 IO 傳輸文件的流程。 讀取文件的時候&#xf…

網頁服務器和mysql服務器_實現Web服務器之間使用同一個MYSQL和相同的網頁配置文件的方法...

實現Web服務器之間使用同一個MYSQL和相同的網頁配置文件的方法發布時間&#xff1a;2020-04-15 16:42:41來源&#xff1a;億速云閱讀&#xff1a;133作者&#xff1a;三月欄目&#xff1a;數據庫億速云負載均衡(Cloud Load Balancer)是對多臺云服務器進行流量分發的服務。億速云…

傳128GB版iPad4售價為799/929美元

外媒9to5mac報道&#xff0c;蘋果將推出一款升級版iPad4&#xff0c;外觀和iPad 4相同&#xff0c;還是黑白兩色的&#xff0c;只加入了新的SKU。 據報道&#xff0c;這款升級版iPad4還有128GB版&#xff0c;隨著這條消息傳出&#xff0c;不久關于128GB版iPad4的售價信息也傳出…