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;
}
原文地址:https://www.cnblogs.com/zkzzkz/p/6634696.html