C++ 常用算法之遍歷

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;/*
遍歷算法 遍歷容器元素
@param beg 開始迭代器
@param end 結束迭代器
@param _callback  函數回調或者函數對象
@return 函數對象
*///void myPrint(int v)
//{
//	cout << v << endl;
//}struct myPrint01
{void operator()(int v){cout << v << endl;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), myPrint01());
}struct myPrint02
{void operator()(int v){cout << v << endl;m_Count++;}int m_Count;
};
//2 for_each有返回值
void test02()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}myPrint02 print2 = for_each(v.begin(), v.end(), myPrint02());cout << print2.m_Count << endl;
}//3 for_each可以綁定參數進行輸出
struct myPrint03 :public binary_function<int, int, void>
{void operator()(int v, int start) const{cout << v + start << endl;}
};void test03()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), bind2nd(myPrint03(), 10000));
}/*
transform算法 將指定容器區間元素搬運到另一容器中
注意 : transform 不會給目標容器分配內存,所以需要我們提前分配好內存
@param beg1 源容器開始迭代器
@param end1 源容器結束迭代器
@param beg2 目標容器開始迭代器
@param _cakkback 回調函數或者函數對象
@return 返回目標容器迭代器
*/class TransForm
{
public:int operator()(int val){return val + 10;}
};
void test04()
{vector<int>v; //原容器for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget; //目標容器vTarget.resize(v.size());transform(v.begin(), v.end(), vTarget.begin(), TransForm());for_each(vTarget.begin(), vTarget.end(), [](int val) { cout << val << " "; });}//transform 第二種用法 將兩個容器數據相加搬運到目標容器
class TransForm2
{
public:int operator()(int val, int val2){return val + val2;}
};void test05()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(100 + i);v2.push_back(200 + i);}vector<int>vTarget; //目標容器vTarget.resize(v1.size());transform(v1.begin(), v1.end(), v2.begin(), vTarget.begin(), TransForm2());// 300 302...for_each(vTarget.begin(), vTarget.end(), [](int val) { cout << val << " "; });cout << "----" << endl;for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) {cout << *it << endl;}
}int main() {//test01();//test02();//test03();//test04();test05();system("pause");return EXIT_SUCCESS;
}

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

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

相關文章

網絡之NAT協議

由來&#xff1a; 2011年2月3日中國農歷新年&#xff0c; IANA對外宣布&#xff1a;IPv4地址空間最后5個地址塊已經被分配給下屬的5個地區委員會。2011年4月15日&#xff0c;亞太區委員會APNIC對外宣布&#xff0c;除了個別保留地址外&#xff0c;本區域所有的IPv4地址基本耗盡…

C++ 常用查找算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> using namespace std; #include <vector> #include <string> #include <functional> /* find算法 查找元素 param beg 容器開始迭代器 param end 容器結束迭代器 para…

CentOS7卸載并安裝mysql教程

MySQL安裝 先卸載其他 刪除Mysql yum remove mysql mysql-server mysql-libs mysql-server;find / -name mysql 將找到的相關東西delete掉(rm -rf /var/lib/mysql)&#xff1b;rpm -qa|grep mysql(查詢出來的東東yum remove掉) rm /etc/my.cnf查看是否還有mysql軟件&#x…

C++ 常用排序算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include <algorithm> #include <vector> #include <functional> #include <ctime> /* merge算法 容器元素合并&#xff0c;并存儲到另一容器中 這兩個容器 必須也是…

排序穩定性的意義

首先&#xff0c;為什么會有排序算法穩定性的說法&#xff1f;只要能排好不就可以了嗎&#xff1f; 看例子 第1行是數字2 記作 1 2 第2行是數字4 記作 2 4 第3行是數字2 記作 3 2 排序后的結果&#xff08;如果看不懂命令的意思&#xff0c;參照這個博客&#xff09; 那么引入…

C++ 常用拷貝和替換算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std;/* copy算法 將容器內指定范圍的元素拷貝到另一容器中 param beg 容器開始迭代器 param end 容器結束迭代器 p…

防火墻的基礎知識入門

文章目錄防火墻基于實現方式&#xff0c;防火墻的發展分為四個階段:Linux 環境中主要的防火墻形式TCP wrappers~~詳解~~ 粗解Tcp wrappers的認識它的基本過程是這樣的&#xff1a;iptable攻擊和防御DDOS 攻擊常見的可能受到 DDOS 攻擊體現的癥狀有&#xff1a;而常見的 DDOS 攻…

C++ 常用算數生成算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> using namespace std; #include <algorithm> //不好使 #include <numeric> //好使 #include <iterator> /* accumulate算法 計算容器元素累計總和 param beg 容器開始迭代…

fork()請問下面的程序一共輸出多少個“A”?多少個-?

題目&#xff1a;請問下面的程序一共輸出多少個“-”&#xff1f; #include #include #include int main(void) { int i; for(i0; i<2; i){ fork(); printf("-"); } return 0; } 解析&#xff1a;一共輸出8個。 首先程序一開始&am…

C++ 常用集合算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std;/* set_intersection算法 求兩個set集合的交集 注意:兩個集合必須是有序序列 param beg1 容器1開始迭代器 par…

本能富可敵國,最后卻選擇拯救世界!Bram的Vim和烏干達兒童

他本能富可敵國&#xff0c;最后卻選擇拯救世界 在命令行界面輸入vim會出現一堆文件&#xff0c;但是一直有這么一句話 Help poor children in Uganda! “幫助可憐的烏干達兒童” 查詢了一下這里面相關的歷史背景和知識 在Vim許可證文件結束后的部分翻譯 &#xff0d;如果…

linux 常用命令01

/bin/bash 就是linux默認的shell ls命令 ls -a 顯示所有文件 包含隱藏文件 ls -R 遞歸顯示子目錄 ls -l 顯示詳細信息 ls -lrt 按照時間排序&#xff0c;顯示文件信息 配合通配符使用 ls *.c *匹配任意多個字符 ls xx.? 匹配任意一個字符 cd 命令 cd - 為切換到上次目錄 cd 回…

Linux基礎查漏補缺

文章目錄第二遍重新回顧Linux基礎查看主機名修改主機名查看IP地址Linux的 “--”和“-”根目錄文件的意義和作用alias直接在命令行界面輸入firefox數組越界發生什么命令行光標移動的幾個操作重定向第二遍重新回顧Linux基礎 1.查找忽略的知識點 2.再次記憶一些基礎知識 3.鞏固基…

linux 常用命令02--文件屬性 以及軟硬鏈接

文件屬性和用戶用戶組 通過ls-l 顯示文件詳細信息 drwxrwxr-x 2 user usergroup 4096 10月 30 20:55 stu1drwxrwxr-x d代表目錄文件&#xff0c; -代表普通文件 rwx rwx r-x 歸屬用戶的權限 歸屬組的權限 其他用戶的權限 權限位數字表示法(8進制數…

linux查漏補缺之常用命令

wc命令 -c, --bytes, --chars輸出字節統計數。-l, --lines輸出換行符統計數。-L, --max-line-length輸出最長的行的長度。-w, --words輸出單詞統計數。grep命令 圖解

linux 常用命令03--修改文件的權限與歸屬

chmod 命令 改變文件權限 第一種&#xff1a; chmod [u|g|o|a] [|-] [r|w|x] filename 比如&#xff1a; chmod ux filename 給所屬用戶增加執行的權限第二種&#xff1a; 給a.out 文件&#xff0c;所屬用戶可讀可寫&#xff0c;所屬組可讀可寫&#xff0c;其他的讀 chmod 06…

思維導圖:面試小結

文件&#xff1a;思維導圖

linux 常用命令04 查找和檢索

先說一下 文件的基本類型 文件類型 l 符號鏈接文件&#xff08;軟連接&#xff09; b 塊設備 &#xff08;磁盤文件&#xff09;c 字符設備p 管道設備&#xff08;pipe&#xff09;s 本地套接字&#xff08;網絡編程&#xff09;- 普通文件 用find命令的時候&…

linux 常用命令05 常用的壓縮與解壓縮文件

zip/unzip ----zip格式 使用方式&#xff1a;zip -r 壓縮包名 原材料 -r代表遞歸子目錄 原材料可以有多個 例如&#xff1a;zip -r bb.zip bb hello 對應的解壓縮&#xff1a;unzip bb.zip .gz格式的壓縮包 gzip和gunzip tar 最常用打包工具 .tar.gz tar相應參數介紹 -c 壓縮…

apt-howto

https://www.debian.org/doc/manuals/apt-howto/index.zh-cn.html#contents