zoukankan      html  css  js  c++  java
  • 问题解决——在结构体中使用set保存结构体数据

    =====================声明==========================

    本文原创,转载请明确的注明出处和作者,并保持文章的完整性(包括本声明部分)。

    本文链接:http://blog.csdn.net/wlsgzl/article/details/41722939

    ==================================================

    至于为什么会使用这么奇葩的东西……为了部落的荣耀

    ----------------------------------------------------------------------------------------

    在结构体中使用STL的set,比使用vector等要复杂一点,感觉是因为set的存储用到了树,所以要写“<”。

    上示例代码。

    #include <iostream>
    #include <set>
    
    using namespace std;
    
    struct AA 
    {
    	int a1;
    	int a2;
    
    	bool operator < (const AA& oDR) const
    	{
    		return a1<oDR.a1;
    	}
    };
    
    struct NN
    {
    	std::set<AA> stSet;
    };
    
    int main(int argc, char* argv[])
    {
    	NN stN1;
    
    	for(int i=6;i>0;i--)
    	{
    		AA stA;
    		stA.a1=i;
    		stA.a2=10*i;
    		stN1.stSet.insert(stA);
    	}
    
    	std::set<AA>::iterator it=stN1.stSet.begin();
    	std::set<AA>::iterator itEnd=stN1.stSet.end();
    
    	for(;it!=itEnd;it++)
    	{
    		printf("vector: a1=%d,a2=%d
    ",it->a1,it->a2);
    	}
    
    	printf("sizeof(NN)=%d
    ",sizeof(NN));
    	printf("sizeof(stN)=%d
    ",sizeof(stN1));
    
    	//////////////////////////////////////////////////////////////////////////
    	NN stN2;
    	stN2.stSet=stN1.stSet;
    	stN1.stSet.clear();
    
    	it=stN2.stSet.begin();
    	itEnd=stN2.stSet.end();
    
    	for(;it!=itEnd;it++)
    	{
    		printf("vector: a1=%d,a2=%d
    ",it->a1,it->a2);
    	}
    
    	printf("sizeof(NN)=%d
    ",sizeof(NN));
    	printf("sizeof(stN)=%d
    ",sizeof(stN2));
    
    	printf("http://blog.csdn.net/wlsgzl/article/details/41722939");
    
    	return 0;
    }

    =====================分割线跟你说再见=======================

  • 相关阅读:
    安装rocketmq并配置管理界面
    centos7搭建xl2tpd
    搭建fastfds+nginx
    申请SSL域名证书
    nginx返回状态码
    搭建v/2/ray和sserver
    第十一周课程总结
    第八周课程总结
    实验报告七&&课程总结
    第八周课程报告&&实验报告六
  • 原文地址:https://www.cnblogs.com/wlsandwho/p/4202072.html
Copyright © 2011-2022 走看看