導航:首頁 > 源碼編譯 > std泛型演算法

std泛型演算法

發布時間:2022-12-17 21:21:53

⑴ 泛型演算法unique_的問題

#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
using namespace std;
int main()
{
�0�2int a[]={1,2,3,4,3,5};
�0�2list<int> ilst(a,a+6);
ilst.sort(); �0�2 // 這里需要排序,不排序,unique_不起作用�0�2vector<int> ivec;
�0�2unique_(ilst.begin(),ilst.end(),back_inserter(ivec));
�0�2for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();++iter)
�0�2 cout<<*iter<<" ";
�0�2cout<<endl;
�0�2return 0;
}

⑵ c++的程序

其實排序,在C++的泛型演算法里有一個 sort() ,直接利用它就不用自己寫代碼,又簡潔,又省力。
我把1 2的代碼入在一個main()里

#include <iostream>
#include <cstdlib> //隨機數產生頭文件
#include <ctime> //包含time()的頭文件
#include <vector>
#include <algorithm> //泛型演算法頭文件

using std::cout;
using std::endl;
using std::cin;
using std::vector;

int main(void)
{

/* 1小題部分 */

int rezult = 1;
int temp = 0;

cout << "please input the number of the end:" << endl;
cin >> temp; //交互部分,輸入階乘上限
while(temp)
{
rezult *= temp--;
}

cout << "rezult:" << rezult << endl << '\n'; // 結果

/* 2小題部分 */

int num = 0;
int rd = 0;
vector<int> TR;
srand(time(0));

cout << "input the number :" << endl;
cin >> num; //輸入需要產生隨機數的個數,10則輸入10

while(num != 0)
{
rd = rand() % 101;
if(rd % 2 != 0 )
TR.push_back(rd);
else
continue;
--num;
}
vector<int>::iterator first = TR.begin();
sort(first, TR.end()); //利用泛型演算法對數排序

while(first != TR.end())
{
cout << *first << " "; // 輸出排序後的數
++first;
}
cout << endl;

return 0;
}

⑶ c++泛型演算法中replace的問題

能學到STL的演算法已經恨牛逼了。
應該改成
replace(sevc.begin(),sevc.end(),string("hello"),string("haha"));
因為演算法中使用模板做形參,所以需要明確類型信息。直接寫"hello"編譯器不知道該處理成什麼類型,所以出錯。

閱讀全文

與std泛型演算法相關的資料

熱點內容
中央文獻研究室和中央編譯局 瀏覽:967
濟南圖紙加密 瀏覽:372
雲伺服器如何擴展 瀏覽:944
ug裝配體存多個文件夾 瀏覽:936
對數運演算法則題目及解析 瀏覽:104
rar解壓軟體怎麼變成中文顯示 瀏覽:800
為什麼我蘋果手機app上沒有微信 瀏覽:361
用python寫一個三角形 瀏覽:748
足球俱樂部網站源碼 瀏覽:147
為什麼蘋果耳機連安卓沒有聲音 瀏覽:513
解壓文件一直載入不出來 瀏覽:437
蘋果伺服器能看到什麼 瀏覽:519
單片機7段數碼管顯示 瀏覽:792
python接入紅包 瀏覽:156
少女絕對命令 瀏覽:7
我的世界伺服器怎麼造房子不被挖 瀏覽:329
美國有什麼購物app好用的 瀏覽:442
庫圖片默認文件夾 瀏覽:411
微信加密能退出嗎 瀏覽:394
程序員看劇不用買會員 瀏覽:592