实现从vector中过滤重复的数据

0、前言

相信有很有种情况下面要在vector下过滤掉重复的数据就比如在数组中需要过滤重复数据一样重要一般常用的方法,好像还是在学校中教的,进行匹配一遍,然后再进行插入既然有了STL容器,那么我们可以完全抛弃上面提到的一般方法,把效率提高至少1倍

1、思路

map也是我们常用的一种容器,他主要的保存方式是,也就是通过key来进行索引,而且这个key是不重复的,那么在我们过滤的前提下,map就是成败的关键了,看看如何把它用好

2、容易陷入的错误

a、一想到容器,很容易想到使用迭代器(iterator),但是对于map,加入数据量大的时候,进行iterator的操作很慢很慢!

b、容易想到,先把数据都插入到map中,再最后直接在写入vector中,其实这样又回到了a中

3、流程分析

假设我们要存放N多的数据(假设是海量的),那么我们需要避免对map进行iterator的操作(很慢很慢!)

于是在数据插入的时候,我先去从map中find一下,加入map中并不存在这个key,那么我们放心的插入到map中,同时插入到vector中。总之,每一次想到要插入vector我都想到map.find、map.insert,那么最终的vector就是不存在重复的,也同时避免了在map中使用迭代器(iterator)

4、一个简单的实例(class)

play_list.h:

#include "iostream"
#include "string"
#include "vector"
#include "unordered_map"
using namespace std;

class PlayList
{
public:
	PlayList();
	~PlayList();

	bool ReadFile(const string &path);
	bool WriteFile(const string &path);

	void AddPlayVideoPath(string play_video_path);

private:
	bool ReadFile(const string &path,  vector<string> &vec);
	bool WriteFile(const string &path, vector<string> &vec);

private:
	vector<string> all_video_path_vec_;
	unordered_map<string, string> all_video_path_map_;
};

play_list.cpp:

#include "stdafx.h"

#include "fstream"
#include "play_list.h"

PlayList::PlayList()
{

}

PlayList::~PlayList()
{

}

bool PlayList::ReadFile(const string &path)
{
	return ReadFile(path, all_video_path_vec_);
}

bool PlayList::WriteFile(const string &path)
{
	return WriteFile(path, all_video_path_vec_);
}

bool PlayList::ReadFile(const string &path, vector<string> &vec)
{
	ifstream fin;
	fin.open(path);
	if (!fin.is_open())
	{
		return false;
	}

	// 取出数据
	string tmp;
	while (fin >> tmp)
	{
		cout << tmp << endl;
//		vec.push_back(tmp);
		AddPlayVideoPath(tmp);
	}

	fin.close();

	return true;
}

bool PlayList::WriteFile(const string &path, vector<string> &vec)
{
	ofstream fout;
	fout.open(path);

	vector<string>::iterator it;
	for (it = vec.begin(); it != vec.end(); it++)
	{
		fout << *it << endl;
	}
	fout.close();

	return true;
}

void PlayList::AddPlayVideoPath(string play_video_path)
{
	unordered_map<string, string>::iterator it;
	it = all_video_path_map_.find(play_video_path);
	if (it != all_video_path_map_.end())
	{
		return;
	}

	all_video_path_map_.insert(pair<string, string>(play_video_path, ""));
	all_video_path_vec_.push_back(play_video_path);
}

5、整个实例代码下载

下载传送门:http://download.csdn.net/detail/zengraoli/7868149

时间: 2024-07-30 04:38:30

实现从vector中过滤重复的数据的相关文章

通过双重for循环来找到JSON中不重复的数据

//通过双重for循环来找到JSON中不重复的数据 var count = 0; for ( i=0; i<json.length; i++) { for ( j=0; j<i; j++) { if ( json[j].api_id == json[i].api_id ) { count++; } } if ( count == 0 ) { kong.getApiName( json[i].api_id ); } }

使用aggregate在MongoDB中查找重复的数据记录

我们知道,MongoDB属于文档型数据库,其存储的文档类型都是JSON对象.正是由于这一特性,我们在Node.js中会经常使用MongoDB进行数据的存取.但由于Node.js是异步执行的,这就导致我们无法保证每一次的数据库save操作都是原子型的.也就是说,如果客户端连续两次发起同一事件将数据存入数据库,很可能会导致数据被重复保存.高并发的情况下,哪怕是你在代码中已经做了非常严格的校验,例如插入数据前判断要保存的数据是否已经存在,但仍然有可能会出现数据被重复保存的风险.因为在异步执行中,你没有

python删除列表中得重复得数据

解决思想:将列表转换为 集合,利用集合删除重复数据得特性删除重复数据,然后将集合转换为列表 #删除列表中得重复元素 def delect_1 (lt): s = set(lt) lt = list(s) print(lt)delect_1([1,2,3,4,1,3,4,5]) 原文地址:https://www.cnblogs.com/chaojiyingxiong/p/9174791.html

删除vector中的重复元素

排序 删除 重新赋值 例: vector<int> ivec = {-1,2,0,0,-1,2,3}; //sort sort(ivec.begin(),ivec.end()); //delete auto it = unique(ivec.begin(),ivec.end()); //resize ivec.resize(distance(ivec.begin(),it));

C++中删除重复的数据并且输出(相当与shell脚本里面的sort -u)

//问题: //给你一个数组,a[]={1,1,1,1,1,2,2,2,2,3,3,3,4,5,6} //要输出的结果是1,2,3,4,5,6.(去除重复的数字,要求时间空间的考虑). #include <iostream> using namespace std; struct Node { int data; Node *next; Node():data(-1),next(NULL){} }; //时间复杂度大幅度减少,但是增加了一定的空间复杂度. class Hash { public

mysql 中删除重复字段数据的方式

1.创建一张表 CREATE TABLE `user` ( `id` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `address` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 2.插入对应数据 INSERT INTO `user` VALUES ('1', 'zhangsan', '20'

从数据表中筛选重复的数据

If you want to find duplicate data (by one or several criterias) and select the actualrows. This should also work, maybe give it try. SELECT id, COUNT(id) FROM table1 GROUP BY id HAVING COUNT(id)>1; I think this will work properly to search repeated

手把手教你如何用java8新特性将List中按指定属性排序,过滤重复数据

在java中常常会遇到这样一个问题,在实际应用中,总会碰到对List排序并过滤重复的问题,如果List中放的只是简单的String类型过滤so easy,但是实际应用中并不会这么easy,往往List中放的是一个类,类中有多个属性,要过滤重复数据,而且这个重复数据要按自己指定的属性过滤,但是要想按照其它属性排序顺序过滤,所以要先排序一下,然后按照某个属性过滤. 实体类如下所示,大家只要创建下面的实体类,无需继承父类,大家不会注解式风格的话,请自行加上getter/setter方法. 首先看看gr

MySQL 处理重复数据:防止表中出现重复数据、统计、过滤、删除重复数据

MySQL 处理重复数据 有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 本章节我们将为大家介绍如何防止数据表出现重复数据及如何删除数据表中的重复数据. 防止表中出现重复数据 你可以在 MySQL 数据表中设置指定的字段为 PRIMARY KEY(主键) 或者 UNIQUE(唯一) 索引来保证数据的唯一性. 让我们尝试一个实例:下表中无索引及主键,所以该表允许出现多条重复记录. CREATE TABLE person_tbl