zoukankan      html  css  js  c++  java
  • 如何快速方便的输出向量vector容器中不重复的内容

    在vector容器中,存入的内容难免会出现重复,那么如何快速输出或提前非重复的那些数据呢,即重复的数据只输出一次,直观的方法是每次输出都要通过循环比较是否已经输出过,这种方法还是比较费时的,可以利用unique函数简化代码,例子:

    #include "stdafx.h"

    #include <iostream>

    #include <vector>

    #include <algorithm>

    #include <string>

    using namespace std;

    int_tmain(int argc, _TCHAR* argv[])

    {

        strings;

        unsigned int size =0;

        vector<string>v;

        cout<<"输入个字符串:"<<endl;

        while(size<5)

        {

           cin>>s;

           v.push_back(s);

           ++size;

        }

     

        cout<<"输入的字符串为:"<<endl;

        copy(v.begin(),v.end(),ostream_iterator<string>(cout," "));

        cout<<"排序后的结果为:"<<endl;

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

        copy(v.begin(),v.end(),ostream_iterator<string>(cout," "));

        cout<<"不输出重复的结果:"<<endl;

        vector<string>::iteratore = unique(v.begin(),v.end());

        for(vector<string>::iterator b=v.begin(); b!=e;b++)

        {

           cout<<*b<<endl;

        }

        //再次输出排序后的结果

        cout<<"再次输出排序后的结果:"<<endl;

        copy(v.begin(),v.end(),ostream_iterator<string>(cout," "));

     

        return 0;

    }

    输出结果为:

  • 相关阅读:
    JavaEE(9)
    微信公众平台自定义菜单接口API指南
    企业微信公众平台建设指南
    微信公众平台消息接口开发(13)多语种互译
    PHP 挖掘 XML 和 HTML 数据
    微信公众平台消息接口开发(12)消息接口Bug
    微信公众平台通用接口API指南
    微信公众平台消息接口API指南
    JavaEE(8)
    JavaEE(7)
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3324910.html
Copyright © 2011-2022 走看看