结构体用于map,set时要重载运算符<
#include<iostream>
#include<set>
using namespace std;
struct P
{
int entry;
int time;
bool operator<(const P &b)const {
return (this->entry<b.entry);
} };
int main()
{
while(!cin.eof())
{
int n;
cin>>n;
set<P> s;
P tmp; for(int i = 0;i<n;i++)
{
tmp.time = 1;
cin>>tmp.entry;
if(s.find(tmp)==s.end())s.insert(tmp);
else
{
set<P>::iterator it; it = s.find(tmp);
tmp=*it;
tmp.time++;
s.erase(it);
s.insert(tmp);
}
}
set<P>::iterator itr;
for(itr = s.begin();itr!=s.end()&&!cin.eof();itr++)
if(itr->time % 2)
cout<<itr->entry<<endl;
}
}
举例: #include <map>
#include <iostream>
#include <string> using namespace std; //学生信息
typedef struct tagStudentInfo
{
int nID;
string strName;
bool operator <(const tagStudentInfo &A) const
{
if (nID < A.nID) return true; //先比较nID
if (nID == A.nID) return strName.compare(A.strName) < ; //nID相同时,再比较strName
return false;
} }StudentInfo,*pstudentInfo;
结构体用于map,set时要重载运算符<的更多相关文章
- 结构体作为map的key或放入set中,需要重载<运算符
结构体作为map的key或放入set中,需要重载<运算符,如下: typedef struct tagRoadKey{ int m_i32Type; int m_i32Scale; ...
- GO学习-(38) Go语言结构体转map[string]interface{}的若干方法
结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...
- Golang操作结构体、Map转化为JSON
结构体生成Json package main import ( "encoding/json" "fmt" ) type IT struct { Company ...
- 初次见识结构体与map的区别
题目 http://vjudge.net/contest/view.action?cid=51142#problem/G 自己做的结构体 #include <iostream>#incl ...
- Go 结构体和map等数据结构转json字符串
Go语言中使用json包中的 Marshal() 函数将数据结构转成json字符串,源代码: func Marshal(v interface{}) ([]byte, error) { e := ne ...
- C++ 结构体、模板、类、重载初使用
目的:需要几个缓存用的数组900*750 首先定义一个模板<参数数据类型,参数1,参数2> 定义一个class类 名字自己取ap_uint0 下面是公用的数组模板[lrow][lcol] ...
- C++中,用类和重载运算符写高精模板
先放代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; s ...
- 用set、map等存储自定义结构体时容器内部判别各元素是否相同的注意事项
STL作为通用模板极大地方便了C++使用者的编程,因为它可以存储任意数据类型的元素 如果我们想用set与map来存储自定义结构体时,如下 struct pp { double xx; double y ...
- STL map、set中key为结构体的用法
下面是map定义的结构: // TEMPLATE CLASS map template<class _Kty, class _Ty, class _Pr = less<_Kty>, ...
随机推荐
- 数组作为hash元素的时候如何push
####################################################################### # Copyright (C) 2015 All rig ...
- 第四章 CSS基础
1.CSS是cascading style sheets 层叠样式表.样式定义如何显示html元素,通常存储在样式表中,将样式添加到html中,是为了解决内容与表现分离的问题. 2.外部样式表可以极大 ...
- Redis persistence demystified - part 1
关于Redis我的一部分工作是阅读博客,论坛以及twitter时间线(time line).对于开发者来说,能够了解用户社区,非用户社区如果理解他正在开发的产品是非常重要的.据我所知,持久化特性是最易 ...
- Thinking in java之正则表达式小例子
public static final String POEM= "Twas brilling, and the slithy toves\n" + "Did gyre ...
- HDU 1560 DNA sequence A* 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=1560 仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串 ...
- tarjan求强联通分量 模板
void tarjan(int u) { dfn[u]=low[u]=++dfs_clock; stack_push(u); for (int c=head[u];c;c=nxt[c]) { int ...
- Memcached 及 Redis 架构分析和比较
Memcached和Redis作为两种Inmemory的key-value数据库,在设计和思想方面有着很多共通的地方,功能和应用方面在很多场合下(作为分布式缓存服务器使用等) 也很相似,在这里把两者放 ...
- 飞天诚信usb-key登录windows+远程桌面
最近在尝试用智能卡做身份验证,以飞天诚信的ePass3000为例. 1.网络环境搭建: 用3台虚机+1台实体机搭一个单独的测试网段:172.16.188.x,如下: 机器名 IP 操作系统 作用 do ...
- 2、IValueConverter应用
1.C#代码如下: public class logotoimgConverter:IValueConverter { //将logo转换为URI public object Convert(obje ...
- C语言:文件操作
以附加方式打开文件,输入数据,关闭文件. #include<stdio.h> #include<stdlib.h> int main() { FILE *fp = NULL; ...