《認清C++語言》のrandom_shuffle()和transform()算法

1STL中的函數random_shuffle()用來對一個元素序列進行重新排序(隨機的),函數原型如下:

?

template<class RandomAccessIterator>

?? void random_shuffle(

????? RandomAccessIterator _First, //指向序列首元素的迭代器

????? RandomAccessIterator _Last? //指向序列最后一個元素的下一個位置的迭代器

?? );

template<class RandomAccessIterator, class RandomNumberGenerator>

?? void random_shuffle(

????? RandomAccessIterator _First,

????? RandomAccessIterator _Last,

????? RandomNumberGenerator& _Rand //調用隨機數產生器的函數

?? );

?

random_shuffle()是一個完全通用的算法,適用于內置數據類型和用戶自定義類型。同時,由于STL算法不僅適用于容器,也適用于序列,因此,random_shuffle()算法可用于內置數組。

?

實例代碼如下:

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

?

int main()

{

??? //用于內置數據類型

??? std::vector<int> vi;

??? for(int i=0; i<100; i++)

??? {

??????? vi.push_back(i); ???????

??? }

???

??? std::random_shuffle(vi.begin(), vi.end());

???

??? std::vector<int>::iterator it;

??? for(it=vi.begin(); it!=vi.end(); it++)

??? {

??????? std::cout<<*it<<std::endl;??????????????????

??? }

???

??? //用于用戶自定義類型

??? std::vector<std::string> vs;

??? vs.push_back(std::string("Sunday"));

??? vs.push_back(std::string("Monday"));

??? vs.push_back(std::string("Tuesday"));

??? vs.push_back(std::string("Wednesday"));

??? vs.push_back(std::string("Thursday"));

??? vs.push_back(std::string("Friday"));

??? vs.push_back(std::string("Saturday"));

???

??? std::random_shuffle(vs.begin(), vs.end());

???

??? for(int i=0; i<7; i++)

??? {

??????? std::cout<<vs[i]<<std::endl;???????

??? }

???

??? //用于數組

??? char arr[6] = {'a', 'b', 'c', 'd', 'e', 'f'};

??? std::random_shuffle(arr, arr+6);

??? for(int i=0; i<6; i++)

??? {

??????? std::cout<<arr[i]<<" ";???????

??? }

??? std::cout<<std::endl;

??? system("pause");

??? return 0;

}

?

2STL中的函數transform()用來遍歷一個容器里面指定范圍的元素,并對這些元素執行指定的操作,函數原型如下:

template<class InputIterator, class OutputIterator, class UnaryFunction>

?? OutputIterator transform(

????? InputIterator _First1, //元素起始位置的輸入迭代器

????? InputIterator _Last1, //元素結束位置的輸入迭代器

????? OutputIterator _Result, //執行指定操作的元素的起始位置的輸出迭代器

????? UnaryFunction _Func //執行的操作(函數)

?? );

template<class InputIterator1, class InputIterator2, class OutputIterator,

?? class BinaryFunction>

?? OutputIterator transform(

????? InputIterator1 _First1, //第一個操作范圍的元素起始位置的輸入迭代器

????? InputIterator1 _Last1, //第一個操作范圍的元素結束位置的輸入迭代器

????? InputIterator2 _First2, //第二個操作范圍的元素起始位置的輸入迭代器

????? OutputIterator _Result, //最終范圍的元素的起始位置的輸出迭代器

????? BinaryFunction _Func //執行的操作(函數)

?? );

?

上面第一個版本的算法對區間[_First1, _Last1]中的每個元素應用函數_Func,并將每次_Func返回的結果存儲到_Result中;

第二個版本的算法以類似的方式運行,但它期望獲得兩個序列并逐次調用一個處理成對元素的二元函數。

?

實例代碼如下:

#include <vector>

#include <algorithm>

#include <functional>

#include <iostream>

?

// The function object multiplies an element by a Factor

template <typename T>

class MultiValue

{

private:

??? T Factor;?? //The value to multiply by

public:

??? //Constructor initializes the value to multiply by

??? MultiValue(const T& _val) : Factor(_val)

??? {

??? }

??? //The function call for the element to be multiplied

??? T operator()(T& elem) const

??? {

??????? return elem*Factor;???????????????

??? }

};

?

int main()

{

??? using namespace std;

??? vector<int> v1, v2(7), v3(7);

??? vector<int>::iterator it1, it2, it3;

???

??? //Constructing vector v1;

??? for(int i=-4; i<=2; i++)

??? {

??????? v1.push_back(i);???????

??? }???

???

??? cout<<"Original vector v1=(";

??? for(it1=v1.begin(); it1!= v1.end(); it1++)

??? {

??????? cout<<*it1<<" ";???????????????????

??? }

??? cout<<")."<<endl;

???

??? //Modifying the vector v1 in place

??? transform(v1.begin(), v1.end(), v1.begin(), MultiValue<int>(2));

??? cout<<"The elements of the vector v1 multiplied by 2 in place gives:"

??????? <<"/n v1mod=(";

??? for(it1=v1.begin(); it1!=v1.end(); it1++)

??? {

? ??????cout<<*it1<<" ";???????????????????

??? }

??? cout<<")."<<endl;

???

??? //using transform to multiply each element by a factor of 5

??? transform(v1.begin(), v1.end(), v2.begin(), MultiValue<int>(5));

???

??? cout<<"Multiplying the elements of the vector v1mod/n"

??????? <<"by the factor 5 & copying to v2 gives:/n v2=(";

??? for(it2=v2.begin(); it2!=v2.end(); it2++)

??? {

??????? cout<<*it2<<" ";???????????????????

??? }

??? cout<<")."<<endl;

???

??? //The second version of transform used to multiply the

??? //elements of the vectors v1mod & v2 pairwise

??? transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),

????????????????????????? multiplies<int>());

??? cout<<"Multiplying elements of the vectors v1mod and v2 pairwise "

??????? <<"gives:/n v3=( ";

??? for(it3=v3.begin(); it3!=v3.end(); it3++)

??? {

??????? cout<<*it3<<" ";

??? }

??? cout<<")."<<endl;

???

??? system("pause");

??? return 0;

}

?

程序運行后輸出如下:

Original vector? v1 = ( -4 -3 -2 -1 0 1 2 ).
The elements of the vector v1 multiplied by 2 in place gives:
 v1mod = ( -8 -6 -4 -2 0 2 4 ).
Multiplying the elements of the vector v1mod
 by the factor 5 & copying to v2 gives:
 v2 = ( -40 -30 -20 -10 0 10 20 ).
Multiplying elements of the vectors v1mod and v2 pairwise gives:
 v3 = ( 320 180 80 20 0 20 80 ).

?

?

?

?

?

轉載于:https://www.cnblogs.com/android-html5/archive/2010/09/07/2533985.html

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

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

相關文章

作為前端開發,如何高效學習 TypeScript

大家好&#xff0c;我是若川。有朋友跟我說最近面試前端候選人&#xff0c;問到關于 JavaScript 的一些少見誤區問題&#xff0c;候選人很多都沒回答上來&#xff0c;他很詫異&#xff0c;一個從國際大廠出來的面試者&#xff0c;竟然對 JavaScript 的一些誤區問題都不了解。他…

figma下載_對于這10家公司,Figma是邁向新高度的起點

figma下載Hey everyone! In this post, we are highlighting 10 companies for which the use of Figma has become the starting point on the path to new heights. These are the use cases of problems and their solutions, where Figma played a decisive role.嘿大家&am…

mysql查詢條件為or_使用mysql查詢where條件里的or和and

為什么要著重講這塊內容呢?因為好多小伙伴都會混淆&#xff0c;要不就是不知道怎么組合使用&#xff0c;今天就給大家講這部分內容干貨&#xff0c;讓大家半分鐘看懂。AND、OR運算符的組合使用在WHERE子句中&#xff0c;通過AND、OR運算符可以同時連接多個條件&#xff0c;當然…

sql server(常用)

普通用法 //生成 uuid 并轉為小寫 select LOWER(SUBSTRING(uuid,1,8)-SUBSTRING(uuid,10,4)-SUBSTRING(uuid,15,4)-SUBSTRING(uuid,20,4)-SUBSTRING(uuid,25,12)) from (select cast(NEWID() as varchar(36)) as uuid) s //ea52a7bb-a2aa-44b8-be28-5ebc64defcf9//獲取時分秒…

代碼編寫中會遇到的安全性問題

一、常用的攻擊手段 1&#xff0e;腳本注入 漏洞描述&#xff1a; 腳本注入攻擊在通過瀏覽器使用用戶輸入框插入惡意標記或腳本代碼時發生。 如&#xff1a;某個輸入框允許用戶向數據存儲中插入內容&#xff0c;如果將一段js腳本插入其中&#xff0c;則當其他用戶使用或瀏覽此數…

TypeScript 原來可以這么香?!

先問一個問題&#xff0c;JavaScript有幾種數據類型&#xff1f;number、string、boolean、null、undefined、symbol、bigint、object其中 bigint 是 ES2020 新增的數據類型&#xff0c;而早在 TS3.2 時便成為 TS 的標準&#xff0c;其實還有好多 ES 標準是 TS 率先提出的&…

java8新特性stream深入解析

2019獨角獸企業重金招聘Python工程師標準>>> 繼續java8源碼的發燒熱&#xff0c;越看越是有充實的感覺。 數據時代下的產物 Java順應時代的發展推出的高效處理大量數據能力的api&#xff0c;它專注于對集合對象進行各種非常便利、高效的聚合操作&#xff0c;借助于同…

mysql內連接的自連接_mysql 內連接、外連接、自連接

一)內連接(等值連接)&#xff1a;查詢客戶姓名&#xff0c;訂單編號&#xff0c;訂單價格---------------------------------------------------select c.name,o.isbn,o.pricefrom customers c inner join orders owhere c.id o.customers_id;-------------------------------…

關于ASP.NET MVC

我是否要學習一下ASP.NET MVC呢&#xff1f;因爲從它剛發布的時候就已經初步的學習了一下&#xff0c;可是一直沒有堅持下來。不過心里對于這份惦記&#xff0c;又讓我始終放不下&#xff0c;看來應該抽個時間來系統的學習一下。 就這樣吧&#xff0c;把自己的博客當成微博來使…

版式設計與創意 pdf_戀愛與版式

版式設計與創意 pdfSince its beginnings, Libe?ration has been characterized by a very distinctive use of typeface, to such an extent that Libe? has put its mark on fonts from across different eras, appropriating these in a certain way.小號因斯它的起點&…

移動網站開發——標記語言

移動互聯網被稱為“第五次科技革命”&#xff0c;而隨著iPhone和Android等智能手機的日漸流行和iPad等平板電腦的出現&#xff0c;移動互聯網的潛力和趨勢也愈發顯現&#xff0c;針對移動設備的網站開發越來越受到關注&#xff0c;國內很多公司也開始重視面向所有移動設備的網站…

mysql適配器_MySQL適配器PyMySQL詳解

import pymysqlimport datainfoimport time#獲取參數host datainfo.hostusername datainfo.usernamepassword datainfo.passworddatabase datainfo.dbprint()#測試數據庫連接def testconnect():#打開數據庫鏈接db pymysql.connect(host,username,password,database)#使用c…

獲取當前Tomcat實例的端口

有時需要在當前代碼中獲取當前Server實例的端口號, 通過HttpServletRequest請求可以, 但有時也需要在沒有請求的情況下獲取到端口號. 用以下方法是可以獲取到的: public int getHttpPort() {try {MBeanServer server;if (MBeanServerFactory.findMBeanServer(null).size() >…

前端技術未來三年前瞻性思考

大家好&#xff0c;我是若川。今天推薦云謙大佬的一篇好文章&#xff0c;值得收藏。點擊下方卡片關注我、加個星標&#xff0c;或者查看源碼等系列文章。學習源碼整體架構系列、年度總結、JS基礎系列習慣從業務場景、用戶體驗、研發速度、維護成本四個維度來看框架等前端技術&a…

微信臨時素材接口_在接口中表達臨時性

微信臨時素材接口When interacting with today’s graphic user interfaces (GUI), we experience a sense of realism. As of now, certain aspects of realism (for example animations) create the appearance that user interface graphics behave in accordance with the …

程序員,當你寫程序寫累了怎么辦。

記得泡泡網的CEO李想說過這樣一句話&#xff0c;大體就是&#xff1a;做一件事情&#xff0c;一開始是興趣使然&#xff0c;然而當三分鐘熱度過去之后&#xff0c;就要靠毅力支撐自己來完成它。至少我到現在是能非常深刻的體會這句話。一開始再怎么喜歡做一件事&#xff0c;要想…

mysql 導致iis 假死_解決IIS無響應假死狀態

1 查看服務器iis的w3wp.exe對應的應用程序池在IIS6下&#xff0c;經常出現w3wp的內存占用不能及時釋放&#xff0c;從而導致服務器響應速度很慢。今天研究了一下&#xff0c;可以做以下配置&#xff1a;1、在IIS中對每個網站進行單獨的應用程序池配置。即互相之間不影響。2、設…

Swift 5將強制執行內存獨占訪問

Swift 5將帶來改進的Swift程序內存安全性&#xff0c;在程序的其他部分修改變量時&#xff0c;不允許通過其他變量名來訪問這些變量。這個變更對現有應用程序的行為和Swift編譯器本身都有重要影響。Swift 5將帶來改進的Swift程序內存安全性&#xff0c;在程序的其他部分修改變量…

GitHub 支持上傳視頻文件啦!

大家好&#xff0c;我是若川。今天周六&#xff0c;分享一篇熱點新聞。文章較短&#xff0c;預計5分鐘可看完。近日 Github 宣布支持了視頻上傳功能&#xff0c;意味著&#xff0c;大家在提 issue 時可以攜帶視頻了&#xff0c;這極大地提高了開發者和維護者的效率&#xff0c;…

ui設計 網絡錯誤_UI設計人員常犯的10個錯誤

ui設計 網絡錯誤重點 (Top highlight)1.不考慮范圍 (1. Disregarding scope)It’s not uncommon for designers to introduce features that will overcomplicate the development process while bringing no additional value to the application. Focusing on the business o…