亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么第十次輸出就成了空串了?不理解啊, 還有就是red,兩個red成了一個了,這是怎么回事?

為什么第十次輸出就成了空串了?不理解啊, 還有就是red,兩個red成了一個了,這是怎么回事?

C++
慕桂英546537 2023-03-01 18:18:22
#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;void a(vector<string> &words);int main(){string word[] = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};vector<string> vec_str(begin(word),end(word));a(vec_str);/*for(auto i : vec_str){cout<<i<<ends;}*/int ii = 1 ;for(vector<string>::const_iterator i = vec_str.cbegin() ; i != vec_str.cend() ; ++i){cout<<*i<<ends<<ii<<"次"<<endl;++ii;}}void a(vector<string> &words){sort(words.begin(),words.end()) ;auto end_unique = unique(words.begin(),words.end());//words.erase(end_unique,words.end());}
查看完整描述

2 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

你定義的a函數的形參是1個對vector的引用,所以函數內對形參作的所有改動會影響到實參。
sort(words.begin(),words.end()) ; 首先是對形參容器進行排序,根據首字母。auto end_unique = unique(words.begin(),words.end()); 執行unique函數后,容器中重復元素被放到了最后面。(不知道你知不知道unique算法的作用,這是1個泛型算法,返回1個迭代器,他對傳入的元素進行排序,同時把重復的元素放到最后面,返回的迭代器指向的重復的第1個元素),那這樣實參的容器就發生變化了,10個元素順序是這樣的:。。。。。前面7個就懶得打了,turtle,the,turtle,此時的end_unique執行the,因為這是第1個重復的元素。a函數調用結束,打印vector里面的元素,現在vector已經拍過序了,有效的迭代器范圍就是指向the,所以打印的時候只打印到這,最后的turtle因為不在迭代器范圍內,所以不打印出來,懂了嗎?

查看完整回答
反對 回復 2023-03-06
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

一.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
注意,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;
}

 


查看完整回答
反對 回復 2023-03-06
  • 2 回答
  • 0 關注
  • 84 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號