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

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;/*
copy算法 將容器內指定范圍的元素拷貝到另一容器中
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param dest 目標起始迭代器
*/void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget;vTarget.resize(v.size());copy(v.begin(), v.end(), vTarget.begin());//for_each(vTarget.begin(), vTarget.end(), [](int val){ cout << val << " "; });copy(vTarget.begin(), vTarget.end(), ostream_iterator<int>(cout, " "));
}/*
replace算法 將容器內指定范圍的舊元素修改為新元素
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param oldvalue 舊元素
@param oldvalue 新元素replace_if算法 將容器內指定范圍滿足條件的元素替換為新元素
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param callback函數回調或者謂詞(返回Bool類型的函數對象)
@param oldvalue 新元素
*/
class MyCompare
{
public:bool operator()(int v){return v > 3;}
};
void test02()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//需求 把容器中的3  替換成300replace(v.begin(), v.end(), 3, 300);copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));cout << endl;//需求  把容器中所有大于3的數字 都替換成 30000replace_if(v.begin(), v.end(), MyCompare(), 30000);copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
}/*
swap算法 互換兩個容器的元素
@param c1容器1
@param c2容器2
*/void test03()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;v2.push_back(10);v2.push_back(30);v2.push_back(20);v2.push_back(40);cout << "交換前數據:" << endl;copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));cout << endl;copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));cout << endl;cout << "交換后的數據:" << endl;swap(v1, v2);copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));cout << endl;copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));cout << endl;}int main(){//test01();//test02();test03();system("pause");return EXIT_SUCCESS;
}

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

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

相關文章

防火墻的基礎知識入門

文章目錄防火墻基于實現方式&#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

Linux系統監控shell腳本

開源項目 https://github.com/atarallo/TECMINT_MONITOR #! /bin/bash # unset any variable which system may be usingunset tecreset os architecture kernelrelease internalip externalip nameserver loadaveragewhile getopts iv name docase $name ini)iopt1;;v)vopt1…

linux ubuntu 軟件安裝的三種方式

apt-get 自動安裝軟件&#xff0c;解決依賴關系 sudo apt-get update 更新源 源在 /etc/apt/sources.list 文件中sudo apt-get install softwarename sudo apt-get remove softwarenamedpkg 根據deb安裝包來安裝軟件 dpkg 是“Debian Packager ”的簡寫 sudo dpkg -i xxx.de…

linux 用戶管理以及其他命令

設置用戶組 sudo groupadd test 增加test用戶組創建用戶 選項&#xff1a; -s 指定shell -g 指定組 -d 用戶家目錄 -m 家目錄不在時&#xff0c;自動創建 sudo useradd -s /bin/bash -g test -d /home/newuser -m newuser設置密碼 sudo passwd newuser切換用戶 su xiaowan…

蒙特卡洛法求圓周率100億數據

代碼 import time import random hits0 pi0 DARTS100000*100000 starttime.perf_counter() for i in range(DARTS):x,yrandom.random(),random.random()distpow(x ** 2y**2,0.5)if dist < 1.0:hits1 pi4*(hits/DARTS) print("圓周率的值是{:.10f}".format(pi)) p…

linux gcc 簡單使用記錄01

大體編譯流程 gcc 參數&#xff1a; I 包含頭文件路徑 L 包含庫文件路徑 l 庫名 比如libxxx.so 對應著 -lxxx(掐頭去尾) O 優化選項 1&#xff0c;3 W 警告 all 顯示更多的 c 編譯成 .o 文件&#xff08;二進制&#xff09; E 輸出到標準輸出&#xff0c;宏替換&#xff0c…

No module named 'Tkinter'

sudo apt install python3-tk這一句就搞定了。網上長篇大論也真是醉了