• C++系统List类库常用demo


    //
    //  CStudent.hpp
    //  标准C++类库_list
    //
    //  Created by 张凯泽 on 17/3/28.
    //  Copyright © 2017年 rytong_zkz. All rights reserved.
    //
    
    #ifndef CStudent_hpp
    #define CStudent_hpp
    
    #include <stdio.h>
    #include <list>
    typedef struct StudentInfo{
        char name[20];
        int age;
    }DATA;
    typedef std::list<DATA> :: iterator POSITION;
    class CStudent{
        std::list<DATA>List;
        
    public:
        CStudent();
        ~CStudent();
        void AddHeader(DATA&data);
        void AddTailer(DATA&data);
        void ShowNote();
        POSITION GetHeadPosition();
        POSITION GetTailPosition();
        DATA GetNext(  POSITION &pos);
        DATA GetPrev(POSITION &pos);
        int GetCount();
        void RemoveAt(POSITION &pos);
        DATA& GetAt(POSITION &pos);
        void InsertData(POSITION & pos,DATA &data);
        
    };
    #endif /* CStudent_hpp */
    //
    //  CStudent.cpp
    //  标准C++类库_list
    //
    //  Created by 张凯泽 on 17/3/28.
    //  Copyright © 2017年 rytong_zkz. All rights reserved.
    //
    
    #include "CStudent.hpp"
    #include <iostream>
    using namespace std;
    CStudent ::CStudent()
    {
        
    }
    CStudent ::~CStudent()
    {
        List.clear();
    }
    POSITION CStudent :: GetHeadPosition()
    {
        return List.begin();
    }
    POSITION CStudent :: GetTailPosition()
    {
        return List.end();
    }
    int CStudent :: GetCount() 
    {
        return List.size();
    }
    DATA CStudent :: GetNext(POSITION &pos)
    {
            return *pos++;//先取值再++
    }
    DATA CStudent :: GetPrev(POSITION &pos)
    {
        return *(--pos);
    }
    void CStudent :: AddHeader(DATA&data)
    {
        List.push_front(data);
    }
    void CStudent:: AddTailer(DATA&data)
    {
        List.push_back(data);
    }
    void CStudent :: RemoveAt(POSITION &pos)
    {
        List.erase(pos);
    }
    DATA& CStudent :: GetAt(POSITION &pos)
    {
        return *pos;
    }
    void CStudent:: InsertData(POSITION & pos,DATA &data)
    {
        List.insert(pos, data);
    }
    void CStudent:: ShowNote()
    {
       POSITION pos = List.begin();
        while (pos != List.end()) {
            cout << (*pos) .name <<endl;
            pos++;
        }
    }
    //
    //  main.cpp
    //  标准C++类库_list
    //
    //  Created by 张凯泽 on 17/3/27.
    //  Copyright © 2017年 rytong_zkz. All rights reserved.
    //
    
    #include <iostream>
    #include <list>
    #include "CStudent.hpp"
    using namespace std;
    int main(int argc, const char * argv[]) {
        CStudent sc;
        DATA d1 = {"ios",23},d2={"python",32},d3={"C++",43},d4={"c",45},d5={"swift",3},d6={"java",4545};
        sc.AddHeader(d1);
        sc.AddHeader(d2);
        sc.AddHeader(d3);
        sc.AddTailer(d4);
        sc.AddTailer(d5);
        //sc.ShowNote();
        cout << "-------------" <<endl;
        POSITION pos = sc.GetTailPosition();
        while (pos != sc.GetHeadPosition()) {
            DATA & data = sc.GetAt(pos);
            if (data.age == 3) {
                //cout <<"姓名=" <<data.name<<endl;
               // sc.RemoveAt(pos);
                sc.InsertData(pos, d6);
            }
            //std:: cout << sc.GetPrev(pos).name <<std:: endl;
            sc.GetPrev(pos);
        }
        
        for (std::list<DATA> :: iterator i = sc.GetHeadPosition(); i != sc.GetTailPosition(); i++) {
            std:: cout << (*i).name << "=" << (*i).age <<"
    " <<std:: endl;
        }
        
        
        
        
        list<int>m_list1,m_list2;
        m_list1.push_front(12);
        m_list1.push_front(2);//头部插入
    //    m_list1.push_front(4);
    //    m_list1.push_back(82);
    //    m_list1.push_back(8);
    //    m_list1.push_back(44);
        m_list2.push_back(83);
        m_list2.push_back(28);//尾部插入
        //m_list.unique();
        //m_list.insert(<#const_iterator __p#>, <#value_type &&__x#>)
        m_list1.splice(m_list1.begin(), m_list2);//合并两个链表
        m_list1.sort();//对链表进行排序
        //cout << *(m_list.begin())<<endl;
        cout<<"listOne.begin()--- listOne.end():"<<endl;
        std::list<int> :: iterator i;
        for (i = m_list1.begin(); i != m_list1.end(); ++i)
            cout << *i << " ";
        cout << endl;
        
        return 0;
    }
  • 相关阅读:
    Android 智能问答机器人的实现
    hadoop分布式安装部署具体视频教程(网盘附配好环境的CentOS虚拟机文件/hadoop配置文件)
    淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成物理查询计划
    linux启动基本流程
    C#开发Unity游戏教程之Scene视图与脚本的使用
    软件项目量化管理(CMMI高成熟度)实践经验谈——之项目管理过程监督与控制篇
    Servlet+JSP 原理
    2 会计要素和会计科目
    Android系统优化
    [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/zkzzkz/p/6634696.html
Copyright © 2020-2023  润新知