c++面向對象高級編程 學習十三 數量不定的模板參數,auto,for

文章目錄

  • 數量不定的模板參數
  • auto
  • ranged-base for

數量不定的模板參數

void print() {}
//... 表示參數的數量不定
template<typename T, typename...Types>
void print(const T&firstArg, const Types&...args) {cout << firstArg << endl;cout<<"args參數的個數為:"<<sizeof...(args)<<endl;//sizeof...(args)計算參數的個數print(args...);//此處firstArg是一個參數,args參數不定,調用print(args...)將args分為兩組:一個參數和多個參數,進行遞歸//當args沒有參數時,即0個參數時,調用 print()
}
//測試函數如下
int main()
{print(7.5,"hello",42,"nihao");system("pause");return 0;
}

輸出結果為:

7.5
args參數的個數為:3
hello
args參數的個數為:2
42
args參數的個數為:1
nihao
args參數的個數為:0

…就是一個所謂的pack(包)
用于template parameters,就是template parameters pack(模板參數包)
用于function parameter types,就是function parameter types pack(函數參數類型包)
用于function parameters,就是function parameters pack(函數參數包)

auto

auto可以自動推導出數據的類型

list<string>c;
...
list<string>::iterator ite;
ite=find(c.begin(),c.end(),target);//auto版本如下:list<string>c;
...
auto ite=find(c.begin(),c.end(),target);

ranged-base for

語言規則:

for(decl:coll){statement
}
//decl是變量,coll是容器,將容器中的變量依次賦值給decl

示例1:

for(int i:{1,3,5,6}){cout<<i<<endl;
}
//輸出結果為:
1
3
5
6

示例2:

vector<double>vec;
...
//值傳遞,不改變容器中的值
for(auto elem:vec){cout<<elem<<endl;
}
//引用傳遞,可改變容器中的值
for(auto& elem:vec){elem*=3;
}

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

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

相關文章

數據結構 樹

定義 樹是節點的優先集合度&#xff1a;孩子的數量&#xff0c;度為0 就是終端節點&#xff0c;不為零就是根節點有序樹&#xff1a;有順序&#xff0c;不可以替換無序樹&#xff1a;無順序&#xff0c;可以替換深度 和 樹的深度相反&#xff0c;第一層深度為1 樹的深度為 3 二…

英語口語 Week15 TuesDay

英語文章 One day, when Bella was doing sports in the school yard, the squirrel fled out of her sleeve. Threading its way through a considerable number of people, the squirrel disappeared in the distance After a sequence of movements, it hopped onto the ar…

c++面向對象高級編程 學習十四 引用

文章目錄referencereference的常見用途reference 變量有三種形式&#xff1a;值&#xff0c;指針&#xff0c;引用 int x0; //值 int* p&x;//指向整型的指針&#xff0c;地址&#xff0c;指針在之后的程序中可以指向其他變量 int& rx;//引用&#xff0c;此處表示 r代…

google瀏覽器 隱藏功能開啟

網址 chrome://flags/ 1&#xff0c;多線程下載 2&#xff0c;暗黑模式3&#xff0c;標簽縮略圖4&#xff0c;PWA 漸進式web應用 網頁即應用5&#xff0c;閱讀模式&#xff0c;排除廣告&#xff0c;點擊閱讀模式去除干擾chrome://net-internals6&#xff0c;解決有問題的代理IP…

英語口語Week 15 Wednesday

英語文章 Accomplishing the task assigned by the teacher; Julia rushed out. Squatting at the gate and playing with the squirrel, Bella waved at the sight of Julia and yelled out here" . Julia ran quickly towards them, pointed at the squirrel and asked…

c++面向對象高級編程 學習十五 組合繼承關系下的構造和析構

文章目錄繼承關系組合關系繼承和組合繼承關系 構造由內而外&#xff0c;析構由外而內&#xff0c;內即是父類 組合關系 A擁有B&#xff0c; 構造由內而外&#xff0c;析構由外而內&#xff0c;內即是B 繼承和組合 構造和析構順序如圖&#xff1a;

英語口語Week16 Wednesday

英語文章 Recently my friend received a gift from her boyfriend - a very expensive bracelet. But the substance of her response left us in astonishment - she didn’t attend to the exquisiteness(of the gift and wanted to return it to him In terms of salary, …

C++ 查漏補缺

特性關系 C語言面向過程C面向過程 面向對象(封裝 繼承 多態)C具備C語言的全部特性的基礎上&#xff0c;并且支持更多新的特性 內存泄露 申請內存&#xff0c;沒有釋放申請 malloc new釋放 free deleteProcessExplorer查看內存是否釋放 代碼移植 將生成的exe運行在別的平臺&…

c++面向對象高級編程 學習十六 vptr和vtbl

當一個類中有一個或多個虛函數時&#xff0c;內存中會多一個虛指針&#xff08;vptr&#xff0c;virtual pointer&#xff09;&#xff0c;指向一個虛表&#xff08;vtbl&#xff0c;virtual table&#xff09; 父類有虛函數&#xff0c;則子類一定有虛函數 在下圖示意圖中&a…

英語口語Week16 Thursday

英語文章 It is an impossibility that everything runs smoothly in everyday life. Where there is trouble, there could be anxiety.Anxiety is a common phenomenon; you are not the only one carrying it. But, it could be somewhat poisonous if you don’t let it o…

c++面向對象高級編程 學習十七 const, new, delete

文章目錄常量成員函數new和delete常量成員函數 常量成員函數是不改變成員數據。 當成員函數的const和non-const版本同時存在時&#xff0c;const object只能調用const版本&#xff0c;non-const object只能調用non-const版本。因此&#xff0c;可以看出&#xff0c;const是函…

codeforces 467A-C語言解題報告

題目網址 題目解析 1.輸入n個房間,再每一行輸入現有的p個人,和一共可以容納的q人數,如果q-p>2則計數1 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h>int main() {int n0,p0,q0;int count0,i;scanf("%d",&n);for(i0;i&…

使用引用的方式交換數據的數值

#include <iostream>void swap(int &a,int &b){a ^ b;b ^ a;a ^ b; } int main(){int num1 10;int num2 20;swap(num1,num2);std::cout << num1 << std::endl;std::cout << num2 << std::endl; }

C++STL與泛型編程 侯捷 (1)

泛型編程&#xff08;Generic Programming&#xff0c;GP&#xff09;&#xff0c;就是使用template&#xff08;模板&#xff09;為主要工具來編寫程序。 重要網頁&#xff1a; http://www.cplusplus.com/ https://en.cppreference.com/w/ http://gcc.gnu.org/

codeforces 136A-C語言解題報告

題目網址 題目解析 1.輸入每個人給第幾個人發禮物(1–n), 輸出每個人收到第幾個人發的禮物 2.因為1–n,所以for循環也使用1–n output[input[j]]j; 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() {int n0;scanf("%d&…

static內容相關介紹學習

說一下static關鍵字的作用 當程序執行到函數內部定義的變量時&#xff0c;編譯器為它在棧上分配空間&#xff0c;函數在棧上分配的空間在此函數執行結束時會釋放掉&#xff0c;這樣就產生了一個問題: 如果想將函數中此變量的值保存至下一次調用時&#xff0c;如何實現&#xf…

C++STL與泛型編程(2) 第一個C++ STL Application

文章目錄STL六大部件STL六大部件代碼示例時間復雜度前閉后開區間auto關鍵字的用法STL六大部件 容器 分配器 算法 迭代器 適配器 仿函數 容器要放東西&#xff0c;東西要占用內存&#xff0c;分配器可支持容器解決內存問題。算法處理容器中的數據。迭代器是容器和算法之間的橋…

codeforces 344A-C語言解題報告

題目網址 題目解析 1.有10和01兩種,同性相斥,異性相吸 2.01是1,使用pre去記錄前一個,寫出所有情況 0110 1001 分為一組 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() {int n0;int i0;int c0;int pre-1;int count0;scanf(&…

JAVA 程序執行進行計時,用于驗證程序執行的時間

package com.example.algorithm.demo.class1;public class Hello {public static void main(String[] args) {long start System.nanoTime();//程序開始時間System.out.println(start);System.out.println("Hello");long end System.nanoTime();System.out.println…

C++STL與泛型編程__侯捷視頻_學習博客_總目錄

CSTL與泛型編程 侯捷 &#xff08;1&#xff09;: c重要網站相關 CSTL與泛型編程&#xff08;2&#xff09; 第一個C STL Application&#xff1a; STL六大部件代碼示例&#xff0c;容器前閉后開區間&#xff0c;auto關鍵字的用法示例 CSTL與泛型編程&#xff08;3&#xff0…