泛型算法之unique

Xredman posted @ Jun 14, 2009 10:11:32 AM in 以前博文 , 1138 阅读

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;

const int N = 10;

int main()
{
        int str[8] = {0, 1, 1, 2, 3, 2, 1, 1};
        vector<int> vec(str, str + 8);
        vector<int>::iterator p, q;

        p = vec.begin();
        q = unique(vec.begin(), vec.end());

        while(p != q)
                cout<<*p++<<" ";
        cout<<endl;

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

        p = vec.begin();
        q = unique(vec.begin(), vec.end());

        while(p != q)
                cout<<*p++<<" ";
        cout<<endl;

        return 0;
}
     unique()虽然去掉容器中的重复值,但是只去掉相邻的重复值。
    为达到每次只出现单个字母一次的目的,需对其先排序。
    unique()操作容器的长度并没有改变,每个独一无二的元素被放到从头开始的下一个自由槽中。
    实际上结果为01231223,序列1223是算法的废弃部分,unique()返回一个iterator指向废弃部分的开始处,
    典型情况下,这个iterator被传递给相关的容器操作erase()来删除无效元素。

 

 






  • 无匹配
  • 无匹配
mi actividad en goog 说:
Jul 26, 2023 05:12:40 AM

Hoy en día, todos tienen múltiples dispositivos que funcionan a través de Android, Mac OS y otros dispositivos. mi actividad en google Google permitió ver y eliminar/eliminar o borrar su historial de búsqueda y otras actividades del usuario a través de la página Mi actividad de Google para todos los dispositivos vinculados a la cuenta de Google.

SEBA 10th important 说:
Sep 07, 2023 09:58:52 PM

Assam HSLC Students Interested aspirants Prepare to Participate to Download Printout Copy of Assam HSLC Important Question Paper 2024 Available for Assamese, English, Bengali Medium Pdf Paper, SEBA HSLC Guess Paper 2024 help Students to learn about the Marking Scheme, the Total Number of Questions From each topic and weight age of Various Topics. Last Year Exam Question Paper will give SEBA 10th important questions 2024 an Exact idea of Exam Pattern,Assam HSLC Question Paper 2024 All the Online PDF format Question Papers for Arabic, Assamese, Bengali, Hindi, Sanskrit, Social Science, English, Manipuri etc. Provided by Secondary Education Board of Assam Students can Refer the Assam HSLC Blueprint 2024 Download Prepare for Students.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter