【C++ 測試】

C++ 測試

  • 一、二維數組
  • 二、私有成員
  • 三、function用法
  • 四、類里面創建另一個類
  • 五、lambda
  • 六、Map動態申請

一、二維數組

#include <iostream>
#include<windows.h>
#include <map>
//	SetConsoleOutputCP ( CP_UTF8 ) ; 
using namespace std;void test1()
{map<int, map<string,float>> m;m[0]["sch"] = 1.1;m[1]["hsd"] = 2.2;for (map<int, map<string,float>>::iterator it = m.begin(); it != m.end();it++){for ( map<string, float>::iterator it2= it->second.begin(); it2 != it->second.end();it2++){cout << it2->first<<" : "<<it2->second<<" ";}cout << endl;}
}void test2()
{map<int, string> m;m[0]= "sch";m[1]= "hsd";for (map<int, string>::iterator it = m.begin(); it != m.end();it++){cout << it->first <<"  "<<it->second<< endl;}
}void test3()
{array<int ,6> a={1,2,3,4,5,6};cout<< a.size()<< endl;for(unsigned int i(0);i < a.size(); i++){cout<< a[i]<< endl;}
}int main() {SetConsoleOutputCP ( CP_UTF8 );test1();system("pause");return 0;
}

二、私有成員

#include <iostream>
#include<windows.h>
#include <map>
//	SetConsoleOutputCP ( CP_UTF8 ) ; 
using namespace std;class myclass{
public:struct Store{int id;int value;bool status;};void test6(int number);private:Store a;
};void my_print(const myclass::Store &store){cout<<"store.id == "<<store.id<<endl;cout<<"store.status == "<<store.status<<endl;cout<<"store.value == "<<store.value<<endl;cout<<"-------------------------------------------"<<endl;
}void myclass::test6(int number){if(number==0){a.id=0;a.value=0;a.status=false;}else{a.id =1;	a.status=true;a.value =number;}myclass::Store & s= a;cout<< "a的地址== "<<&a<<endl;my_print(s);
}int main() {SetConsoleOutputCP ( CP_UTF8 );myclass c;c.test6(0);c.test6(1);c.test6(0);system("pause");return 0;
}

三、function用法

#include <iostream>
#include<windows.h>
#include <functional>
//	SetConsoleOutputCP ( CP_UTF8 ) ; 
using namespace std;
class A
{std::function<void(void)> callback_;
public:A(const std::function<void(void)>& f):callback_(f){}void notify(){callback_();}
};class Foo
{
public:void operator()(void){std::cout << __FUNCTION__ << std::endl;}
};void call_when_even(int x, const std::function<void(int)>& f)
{if (x % 2 == 0) {f(x);}else {f(0);}
}void output(int x)
{std::cout<< x << ""<<endl;
}int main() {SetConsoleOutputCP ( CP_UTF8 );Foo foo;A aa(foo);aa.notify();    call_when_even(1,output);system("pause");return 0;
}

四、類里面創建另一個類

#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
//	SetConsoleOutputCP ( CP_UTF8 ) ; 
using namespace std;class A
{public: A(){cout<<"A 構造"<<endl;}A(int a,int b){cout<<a<<endl;cout<<b<<endl;}int a;~A(){cout<<"A 析構"<<endl;}
};class C
{public: C(){cout<<"C 構造"<<endl;}C(int a,int b){cout<<a<<endl;cout<<b<<endl;}int a;~C(){cout<<"C 析構"<<endl;}
};class B{
public:B(){cout<<"B 構造"<<endl;}~B(){cout<<"B 析構 "<<endl;}private:A a;
public:C c;
};void test()
{B b;
}int main() {SetConsoleOutputCP ( CP_UTF8 );if(-1)test();system("pause");return 0;
}

五、lambda

#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
//	SetConsoleOutputCP ( CP_UTF8 ) ; 
using namespace std;using fun_name=std::function<void(int b,int c)>;
void fun(int a, std::function<void(int b,int c)> f)
{cout <<__FUNCTION__ <<  ":   "<< a <<endl;fun_name d=f;cout <<__FUNCTION__ <<  " 內的 f 函數"<<endl;f(4,6);cout <<__FUNCTION__ <<  " 內的 d 函數"<<endl;d(90,9);
}void fun2(int b, int c)
{cout <<__FUNCTION__<<  ":   "<<"b+c = "<<b+c <<endl;
}void test()
{int g =100;fun( 1,[&](int d, int e){g++; fun2(d,e);cout <<__FUNCTION__<<  ":   "<<"g = "<<g <<endl;});
}int main() {SetConsoleOutputCP ( CP_UTF8 );test();system("pause");return 0;
}結果打印:fun:   1fun 內的 f 函數fun2:   b+c = 10operator():   g = 101fun 內的 d 函數fun2:   b+c = 99operator():   g = 102

六、Map動態申請

#include <iostream>
#include<windows.h>
#include <map>
#include <functional>
//	SetConsoleOutputCP ( CP_UTF8 ) ; 
using namespace std;enum mGroup{GROUP_1=0,GROUP_2=1,GROUP_3=2,GROUP_4=3,GROUP_5=4,GROUP_6=5,
};class A{
public:struct Store{int id;array<int,8>data;bool status;};A(){cout <<"A 構造"<<endl;}~A(){cout <<"A 析構"<<endl;}void initStore();private:map<mGroup,map<int, Store>> m_map;
};void A::initStore(){m_map[GROUP_1][0].id = 1;m_map[GROUP_1][1].data.fill(0x02);for(int i=0; i<5; i++){cout << m_map[GROUP_1].size() << "   ID ="<<m_map[GROUP_1][i].id <<"  :" ;for (int j = 0; j < 8  ; j++){	cout<<m_map[GROUP_1][i].data[j]<<"  ";}cout<<endl;}
}int main() {SetConsoleOutputCP ( CP_UTF8 );A a;a.initStore();system("pause");return 0;
}
運行結果:A 構造2   ID =1  :0  0  0  0  0  0  0  02   ID =0  :2  2  2  2  2  2  2  22   ID =0  :0  0  0  0  0  0  0  03   ID =0  :0  0  0  0  0  0  0  04   ID =0  :0  0  0  0  0  0  0  0

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

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

相關文章

求最短路徑之迪杰斯特拉算法

對fill用法的介紹 1.用鄰接矩陣實現 const int maxn100; const int INF100000000;//無窮大&#xff0c;用來初始化邊 int G[maxn][maxn];//用鄰接矩陣存儲圖的信息 int isin[maxn]{false};//記錄是否已被訪問 int minDis[maxn];//記錄到頂點的最小距離void Dijkstra(int s,in…

網格圖的搜索

來自靈神網格圖題單。 1. 網格圖 1.1. LC 200 島嶼數量 這題我一開始想繁了&#xff0c;想維護并查集&#xff0c;然后看等價類個數。其實完全沒有必要。因為連通分量深搜到頭就可以直接給答案計數1。利用vis數組維護訪問過的點&#xff0c;然后碰到新連通分量重新深搜即可。…

Pinia使用

官方地址&#xff1a;Pinia | The intuitive store for Vue.js (vuejs.org)https://pinia.vuejs.org/ 1.安裝 npm install pinia npm install pinia-plugin-persistedstate Pinia是一個基于Vue 3的狀態管理庫&#xff0c;它使得管理Vue的全局狀態變得更加容易和直觀。 而…

自定義el-dialog的樣式

實現效果&#xff1a; 樣式代碼如下&#xff1a;&#xff08;可以寫在common.scss文件夾中&#xff09; .el-dialog__header {padding: 16px 20px;border-bottom: 1px solid #DCDFE6;display: flex;align-items: center;.el-dialog__title {font-size: 16px;position: relativ…

utniy urp shinyssrr插件使用

文章目錄 前言步驟1首先在URP的配置文件里添加SSR后處理2 修改RenderingPath為延遲渲染3 啟用深度紋理4 為物體添加腳本 插件下載 前言 用來實現屏幕空間反射效果 unity 版本為2021.3.8LTS&#xff0c;低版本的untiy URP的參數設置位置z可能會不同 步驟 1首先在URP的配置文件…

記錄阿里云換源失敗的慘痛教訓

聲明 首先我不是一個云服務器小白&#xff0c;但是之前一直在使用騰訊云和火山引擎的云服務器。從未見過阿里云這樣如此**的運營商。 問題 服務器到手&#xff0c;第一步在我進行sudo apt update的時候&#xff0c;也就是更新軟件包的時候&#xff0c;我發現&#xff0c;一直…

1028. 從先序遍歷還原二叉樹(三種方法:棧+遞歸+集合)

文章目錄 1028. 從先序遍歷還原二叉樹&#xff08;三種方法&#xff1a;棧遞歸集合&#xff09;一、棧 while迭代1.思路2.代碼 二、遞歸法1.思路2.代碼 三、集合存儲1.思路2.代碼 1028. 從先序遍歷還原二叉樹&#xff08;三種方法&#xff1a;棧遞歸集合&#xff09; 一、棧 wh…

hive報錯:FAILED: NullPointerException null

發現問題 起因是我虛擬機的hive不管執行什么命令都報空指針異常的錯誤 我也在網上找了很多相關問題的資料&#xff0c;發現都不是我這個問題的解決方法&#xff0c;后來在hive官網上與hive 3.1.3版本相匹配的hadoop版本是3.x的版本&#xff0c;而我的hadoop版本還是2.7.2的版本…

HTTPS的加密過程

文章目錄 前言一、為什么需要加密&#xff1f;二、只用對稱加密可以嗎&#xff1f;三、只使用非對稱加密四、雙方都使用非對稱加密五、使用非對稱加密對稱加密六、引入證書1.如何放防止數字證書被篡改&#xff1f;2.中間人有可能篡改該證書嗎&#xff1f;3.中間人有可能掉包該證…

開窗函數rank() over,dense_rank() over,row_number() over的區別

1.rank() over 查詢出指定的條件進行排名&#xff0c;條件相同排名相同的話&#xff0c;排名之間是不連續的 例如排名如 1 2 3 3 5 6 7 等&#xff0c;相同的排名會自動跳過 2.dense_rank() over 查詢出指定的條件后進行排名&#xff0c;條件相同&#xff0c;排名相同的話&…

【YOLO系列】YOLOv9論文超詳細解讀(翻譯 +學習筆記)

前言 時隔一年&#xff0c;YOLOv8還沒捂熱&#xff0c;YOLO系列最新版本——YOLOv9 終于閃亮登場&#xff01; YOLOv9的一作和v7一樣。v4也有他。 他于2017年獲得臺灣省National Central University計算機科學與信息工程博士學位&#xff0c;現在就職于該省Academia Sinica的…

【大數據】Flink SQL 語法篇(六):Temporal Join

《Flink SQL 語法篇》系列&#xff0c;共包含以下 10 篇文章&#xff1a; Flink SQL 語法篇&#xff08;一&#xff09;&#xff1a;CREATEFlink SQL 語法篇&#xff08;二&#xff09;&#xff1a;WITH、SELECT & WHERE、SELECT DISTINCTFlink SQL 語法篇&#xff08;三&…

機器視覺——硬件選型

1、相機選型 在選擇機器視覺相機時&#xff0c;通常需要考慮以下幾個方面&#xff1a; 1、分辨率&#xff1a;相機的分辨率決定了其拍攝圖像的清晰度和細節程度。根據具體的應用需求&#xff0c;可以選擇適當的分辨率范圍。 2、幀率&#xff1a;幀率表示相機每秒鐘能夠拍攝的…

2023年營養保健品線上電商市場行業分析(2024年營養保健行業未來趨勢分析)

近年來&#xff0c;受人口老齡化、養生年輕化等因素驅動&#xff0c;保健品行業增長強勁&#xff0c;加之越來越多的年輕人也加入養生大軍&#xff0c;成為保健品市場上的一股新力量&#xff0c;進一步帶動市場擴容。 鯨參謀數據顯示&#xff0c;2023年度&#xff0c;京東平臺…

[pdf]《軟件方法》2024版部分公開-共196頁

DDD領域驅動設計批評文集 做強化自測題獲得“軟件方法建模師”稱號 《軟件方法》各章合集 潘加宇《軟件方法》2024版部分公開pdf文件&#xff0c;共196頁&#xff0c;已上傳CSDN資源。 也可到以下地址下載&#xff1a; http://www.umlchina.com/url/softmeth2024.html 如果…

Ubuntu20.04 ssh終端登錄后未自動執行.bashrc

sudo vim ~/.profile輸入以下內容 if [ -n "$BASH_VERSION" ]; then if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi 執行 source ~/.profile重新測試 其他答案 如果你的~/.bashrc文件在Ubuntu中沒有自動生效&#xff0c;…

3. 文檔概述(Documentation Overview)

3. 文檔概述&#xff08;Documentation Overview&#xff09; 本章節簡要介紹一下Spring Boot參考文檔。它包含本文檔其它部分的鏈接。 本文檔的最新版本可在 docs.spring.io/spring-boot/docs/current/reference/ 上獲取。 3.1 第一步&#xff08;First Steps&#xff09; …

解析電源模塊測試條件與測試步驟 快速完成測試

高溫高濕儲存測試是電源模塊環境適應性測試內容之一&#xff0c;在實際使用過程中由于應用場景不同電源所處的環境也是多樣的&#xff0c;因此需要測試電源對各種環境的適應能力&#xff0c;提高電源的性能和可靠性。 電源高溫高濕存儲測試的目的是為了測量環境對電源結構、元件…

C語言第三十三彈---動態內存管理(上)

?個人主頁&#xff1a; 熬夜學編程的小林 &#x1f497;系列專欄&#xff1a; 【C語言詳解】 【數據結構詳解】 動態內存管理 1、為什么要有動態內存分配 2、malloc和free 2.1、malloc 2.2、free 3、calloc和realloc 3.1、calloc 3.2、realloc 4、常見的動態內存的錯…

氣象數據收集

1、國家氣象科學數據中心 預報數據:需要定制,收費10萬+ 觀測數據:國家氣象信息中心-中國氣象數據網 (cma.cn)https://data.cma.cn/data/cdcdetail/dataCode/A.0012.0001.html 地面基本氣象觀測數據 滯后2天 滯后一天 路面數據同化系統,實時 國家氣象信息中心-中國氣象數…