日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ STL unique , unique_copy函数

發布時間:2025/4/16 c/c++ 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ STL unique , unique_copy函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.unique函數

類屬性算法unique的作用是從輸入序列中“刪除”全部相鄰的反復元素

該算法刪除相鄰的反復元素。然后又一次排列輸入范圍內的元素,而且返回一個迭代器(容器的長度沒變,僅僅是元素順序改變了),表示無反復的值范圍得結束。

// sort words alphabetically so we can find the duplicates sort(words.begin(), words.end()); /* eliminate duplicate words: * unique reorders words so that each word appears once in the * front portion of words and returns an iterator one past the unique range; * erase uses a vector operation to remove the nonunique elements */ vector<string>::iterator end_unique = unique(words.begin(), words.end()); words.erase(end_unique, words.end());

在STL中unique函數是一個去重函數, unique的功能是去除相鄰的反復元素(僅僅保留一個),事實上它并不真正把反復的元素刪除,是把反復的元素移到后面去了。然后依舊保存到了原數組中,然后 返回去重后最后一個元素的地址,由于unique去除的是相鄰的反復元素,所以一般用之前都會要排一下序。


若調用sort后。vector的對象的元素按次序排列例如以下:

sort jumps over quick red red slow the the turtle

則調用unique后,vector中存儲的內容是:


注意,words的大小并沒有改變,依舊保存著10個元素;僅僅是這些元素的順序改變了。調用unique“刪除”了相鄰的反復值。給“刪除”加上引號是由于unique實際上并沒有刪除不論什么元素,而是將無反復的元素拷貝到序列的前段。從而覆蓋相鄰的反復元素。unique返回的迭代器指向超出無反復的元素范圍末端的下一個位置。

注意:算法不直接改動容器的大小。假設須要加入或刪除元素。則必須使用容器操作。

example:

?

#include <iostream> #include <cassert> #include <algorithm> #include <vector> #include <string> #include <iterator>using namespace std;int main() {//cout<<"Illustrating the generic unique algorithm."<<endl;const int N=11;int array1[N]={1,2,0,3,3,0,7,7,7,0,8};vector<int> vector1;for (int i=0;i<N;++i)vector1.push_back(array1[i]);vector<int>::iterator new_end;new_end=unique(vector1.begin(),vector1.end()); //"刪除"相鄰的反復元素assert(vector1.size()==N);vector1.erase(new_end,vector1.end()); //刪除(真正的刪除)反復的元素copy(vector1.begin(),vector1.end(),ostream_iterator<int>(cout," "));cout<<endl;return 0; }

執行結果為:


二、unique_copy函數

算法標準庫定義了一個名為unique_copy的函數,其操作類似于unique。

唯一的差別在于:前者接受第三個迭代器實參,用于指定復制不反復元素的目標序列。

unique_copy依據字面意思就是去除反復元素再運行copy運算。

編敲代碼使用unique_copy將一個list對象中不反復的元素賦值到一個空的vector對象中。

//使用unique_copy算法 //將一個list對象中不反復的元素賦值到一個空的vector對象中 #include<iostream> #include<list> #include<vector> #include<algorithm> using namespace std;int main() {int ia[7] = {5 , 2 , 2 , 2 , 100 , 5 , 2};list<int> ilst(ia , ia + 7);vector<int> ivec;//將list對象ilst中不反復的元素拷貝到空的vector對象ivec中//sort(ilst.begin() , ilst.end()); //不能用此種排序。會報錯ilst.sort(); //在進行復制之前要先排序,切記unique_copy(ilst.begin() , ilst.end() , back_inserter(ivec));//輸出vector容器cout<<"vector: "<<endl;for(vector<int>::iterator iter = ivec.begin() ; iter != ivec.end() ; ++iter)cout<<*iter<<" ";cout<<endl;return 0; }

假如

list<int> ilst(ia , ia + 7); 改為:vector<int> ilst(ia , ia + 7);

則排序時可用:

sort(ilst.begin() , ilst.end());

?這里要注意list和vector的排序用什么方法。

《Effective STL》里這些話可能實用處: item 31      “我們總結一下你的排序選擇:    ● 假設你須要在vector、string、deque或數組上進行全然排序。你能夠使用sort或stable_sort。    ● 假設你有一個vector、string、deque或數組,你僅僅須要排序前n個元素,應該用partial_sort    ● 假設你有一個vector、string、deque或數組。你須要鑒別出第n個元素或你須要鑒別出最前的n個元素,而不用知道它們的順序,nth_element是你應該注意和調用的。    ● 假設你須要把標準序列容器的元素或數組分隔為滿足和不滿足某個標準,你大概就要找partition或stable_partition。

   ● 假設你的數據是在list中,你能夠直接使用partition和stable_partition,你能夠使用list的sort來取代sort和stable_sort。

假設你須要partial_sort或nth_element提供的效果,你就必須間接完畢這個任務,但正如我在上面勾畫的。會有非常多選擇。

     另外,你能夠通過把數據放在標準關聯容器中的方法以保持在不論什么時候東西都有序。你也可能會考慮標準非STL容器priority_queue,它也能夠總是保持它的元素有序。

總結

以上是生活随笔為你收集整理的c++ STL unique , unique_copy函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。