电商专业学习嵌入式软件开发第六十二天

  • C++第八天

今天讲的主要是list和map容器。将涉及到的函数也都讲到了,都有案例。今天讲的内容已经尽量添加备注了,方便以后查看。明天最后一天C++了,今天老师就将项目资料给我们要求我们做准备了,看了一眼,感觉难度不小,比第一个项目难度要大一些。

map查找速度快,会自动排序
map:也是类模板,里面放一对数据,第一个是键,第二个是值,内部实现是通过二叉树实现的
红黑树是一种特殊的二叉树,插入的时候会自动排序
树的遍历有前序遍历(顺序是中左右)、后序遍历(顺序是左右中)、中序遍历(顺序是左中右,结果是从小到大),前、后、中指的是根节点的位置

#include <iostream>
#include <string>
#include <map>
using namespace std;
//map:存放键值对数据,键唯一,不能重复
//若已存在键,则插入数据失败
typedef map<int, string> PEOMAP;
void print(const PEOMAP &peo)
{
    PEOMAP::const_iterator iter;
    iter = peo.begin();
    for (; iter != peo.end(); iter++)
    {
        cout << iter->first << ' ' << iter->second<< '\n';
    }
}
int main(void)
{
    map<int, string> people;
    people.insert(pair<int,string>(1001, "zhangsan"));//pair是用来保留一对数据的类模板
    people.insert(pair<int,string>(1009, "lisi"));
    people.insert(pair<int,string>(1006, "wangwu"));
    people.insert(pair<int,string>(1003, "zhaoliu"));
    people.insert(pair<int,string>(1007, "xiaoqi"));
    print(people);
#if 0  //上面写法相当于:
    pair<int,string> p(1001,"zhangsan");
    people.insert(p);
#endif
    return 0;
}
#include <iostream>
#include <string>
#include <map>
using namespace std;
//map:存放键值对数据,键唯一,不能重复
//若已存在键,则插入数据失败
typedef map<int, string> PEOMAP;
void print(const PEOMAP &peo)
{
    PEOMAP::const_iterator iter;
    iter = peo.begin();
    for (; iter != peo.end(); iter++)
    {
        cout << iter->first << ' ' << iter->second<< '\n';
    }
}
int main(void)
{
    map<int, string> people;
    people.insert(pair<int,string>(1001, "zhangsan"));
    //下标使用方法
    //string str = people[1009];//没有的会自动创建
    people[1001] = "hhashsha";//有的话会覆盖原值
    people[1009] = "$$$$$";//没有的话会创建
    //cout << people.at(1010) << endl;
    print(people);

    return 0;
}
#include <iostream>
#include <list>
using namespace std;

template<typename U>
void print(const list<U> &mylist)
{
    typename list<U>::const_iterator iter;
    iter = mylist.begin();
    for (; iter != mylist.end(); iter++)
    {
        cout << *iter << endl;
    }
}
bool predicate(int &a)
{
    if (0 == a%2)
    {
        return true;
    }
    return false;
}
bool mycompare(int &a, int &b)
{
    return a > b;
}
int main(void)
{
    list<int> mylist;
    mylist.push_back(15);
    mylist.push_back(17);
    mylist.push_back(25);
    mylist.push_back(77);
    mylist.push_front(16);
    mylist.push_front(86);
    mylist.push_front(26);
//  mylist.push_front(16);
//  mylist.push_front(16);
    print(mylist);
//  mylist.remove(17);//移除指定的内容
//  mylist.remove_if(predicate);//删除满足条件的内容
//  mylist.unique();//删除连续的内容,保留唯一的内容
//  mylist.sort();//默认从小到大排序
    mylist.sort(mycompare);//定义条件从大到小排序
    cout << "-----------------\n";
    print(mylist);
    return 0;
}
#include <iostream>
#include <list>
using namespace std;
template<typename U>
void print(const list<U> &mylist)
{
    typename list<U>::const_iterator iter;//迭代器也是一个类模板
    iter = mylist.begin();
    for (; iter != mylist.end(); iter++)
    {
        cout << *iter << ' ';
    }
    cout << '\n';
}
int main(void)
{
    int arr1[7] = {18, 67, 34, 6, 15, 2, 9};
    list<int> l1(arr1, arr1+7);
    l1.sort();
    print(l1);
    
    int arr2[5] = {33, 67, 22, 17, 32};
    list<int> l2(arr2, arr2+5);
    l2.sort();
    print(l2);

    l1.merge(l2);//合并
    cout << "l1:";
    print(l1);
    cout << "l2:";
    print(l2);
    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ostream &operator<<(ostream &out, const Student *stu);
    friend ostream &operator<<(ostream &out, const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out, const Student *stu)
{
    out << stu->m_caName << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out, const Student &stu)
{
    out << stu.m_caName << ' ' << stu.m_fScore;
    return out;
}
template<typename U>
void print(const list<U> &mylist)
{
    typename list<U>::const_iterator iter;
    iter = mylist.begin();
    for (; iter != mylist.end(); iter++)
    {
        cout << *iter << endl;
    }
}
bool compare(Student* &s1, Student* &s2)
{
    return s1->getScore() > s2->getScore();
}
int main(void)
{
    list<Student *> stulist;
    stulist.push_back(new Student("aa", 89));
    stulist.push_back(new Student("bb", 79));
    stulist.push_back(new Student("cc", 99));
    stulist.push_back(new Student("dd", 69));
    stulist.push_back(new Student("ee", 88));
    print(stulist);
    cout << "-------------------\n";
    stulist.sort(compare);//排序
    print(stulist);

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ostream &operator<<(ostream &out, const Student *stu);
    friend ostream &operator<<(ostream &out, const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out, const Student *stu)
{
    out << stu->m_caName << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out, const Student &stu)
{
    out << stu.m_caName << ' ' << stu.m_fScore;
    return out;
}

template<typename U>
void print(const list<U> &mylist)
{
    typename list<U>::const_iterator iter;
    iter = mylist.begin();
    for (; iter != mylist.end(); iter++)
    {
        cout << *iter << endl;
    }
}

bool compare(Student &s1, Student &s2)
{
    return s1.getScore() > s2.getScore();
}

int main(void)
{
    list<Student> stulist;
    stulist.push_back(Student("aa", 89));
    stulist.push_back(Student("bb", 79));
    stulist.push_back(Student("cc", 99));
    stulist.push_back(Student("dd", 69));
    stulist.push_back(Student("ee", 88));
    print(stulist);
    cout << "-------------------\n";
    stulist.sort(compare);//排序
    print(stulist);

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    
    bool operator <(const Student &other)
    {
        return m_fScore < other.m_fScore;
    }

    friend ostream &operator<<(ostream &out, const Student *stu);
    friend ostream &operator<<(ostream &out, const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out, const Student *stu)
{
    out << stu->m_caName << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out, const Student &stu)
{
    out << stu.m_caName << ' ' << stu.m_fScore;
    return out;
}

template<typename U>
void print(const list<U> &mylist)
{
    typename list<U>::const_iterator iter;
    iter = mylist.begin();
    for (; iter != mylist.end(); iter++)
    {
        cout << *iter << endl;
    }
}

int main(void)
{
    list<Student> stulist;
    stulist.push_back(Student("aa", 89));
    stulist.push_back(Student("bb", 79));
    stulist.push_back(Student("cc", 99));
    stulist.push_back(Student("dd", 69));
    stulist.push_back(Student("ee", 88));
    print(stulist);
    cout << "-------------------\n";
    stulist.sort();
    print(stulist);

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;
//map:存放键值对数据,键唯一,不能重复
//若已存在键,则插入数据失败

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    bool operator<(const Student &other) const 
    {
        return m_fScore <other.m_fScore;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ostream &operator<<(ostream &out, const Student *stu);
    friend ostream &operator<<(ostream &out, const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out, const Student *stu)
{
    out << stu->m_caName << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out, const Student &stu)
{
    out << stu.m_caName << ' ' << stu.m_fScore;
    return out;
}
typedef map<int, Student*> PEOMAP;
typedef pair<int, Student*> PAIR;
void print(const PEOMAP &peo)
{
    PEOMAP::const_iterator iter;
    iter = peo.begin();
    for (; iter != peo.end(); iter++)
    {
        cout << iter->first << ' ' << iter->second<< '\n';
    }
}
int main(void)
{
    map<int, Student*> people;
    people.insert(PAIR(1001, new Student("zhangsan", 90)));
    people.insert(PAIR(1009, new Student("lisi", 89)));
    people.insert(PAIR(1006, new Student("wangwu", 99)));
    people.insert(PAIR(1003, new Student("zhaoliu", 78)));
    people.insert(PAIR(1007, new Student("xiaoqi", 62)));
    print(people);
#if 0
    pair<int,string> p(1001,"zhangsan");
    people.insert(p);
#endif

    return 0;
}
#include <iostream>
#include <string>
#include <map>
#include <list>
#include "student.h"
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    bool operator<(const Student &other) const 
    {
        return m_fScore <other.m_fScore;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ostream &operator<<(ostream &out, const Student *stu);
    friend ostream &operator<<(ostream &out, const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out, const Student *stu)
{
    out << stu->m_caName << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out, const Student &stu)
{
    out << stu.m_caName << ' ' << stu.m_fScore;
    return out;
}

typedef map<int, Student*> PEOMAP;
typedef pair<int, Student*> PAIR;

bool compare(PAIR &p1, PAIR &p2)
{
    return p1.second->getScore() > p2.second->getScore();
}

void print(const PEOMAP &peo)
{
    PEOMAP::const_iterator iter;
    iter = peo.begin();
    for (; iter != peo.end(); iter++)
    {
        cout << iter->first << ' ' << iter->second<< '\n';
    }
}
int main(void)
{
    map<int, Student*> people;
    people.insert(PAIR(1001, new Student("zhangsan", 90)));
    people.insert(PAIR(1009, new Student("lisi", 89)));
    people.insert(PAIR(1006, new Student("wangwu", 99)));
    people.insert(PAIR(1003, new Student("zhaoliu", 78)));
    people.insert(PAIR(1007, new Student("xiaoqi", 62)));
    print(people);
    cout << "---------------\n";

    list<PAIR> test;
    PEOMAP::iterator iter;
    iter = people.begin();
    for (; iter != people.end(); iter++)
    {
        test.push_back(*iter);  
    }
    test.sort(compare);
    list<PAIR>::iterator iter2;
    iter2 = test.begin();
    for (; iter2 != test.end(); iter2++)
    {
        cout << iter2->first<< ' ' << iter2->second<< '\n';
    }
    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>"
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    bool operator<(const Student &other) const 
    {
        return m_fScore <other.m_fScore;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ostream &operator<<(ostream &out
            , const Student *stu);
    friend ostream &operator<<(ostream &out
            , const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out
            , const Student *stu)
{
    out << stu->m_caName 
        << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out
            , const Student &stu)
{
    out << stu.m_caName 
        << ' ' << stu.m_fScore;
    return out;
}

typedef map<Student, int> PEOMAP;
typedef pair<Student, int> PAIR;

void print(const PEOMAP &peo)
{
    PEOMAP::const_iterator iter;
    iter = peo.begin();
    for (; iter != peo.end(); iter++)
    {
        cout << iter->first 
             << ' ' << iter->second
             << '\n';
    }
}

int main(void)
{
    map<Student, int> people;
    people.insert(PAIR(Student("zha", 90), 1001));
    people.insert(PAIR(Student("as", 80), 1009));
    people.insert(PAIR(Student("bs", 79), 1005));
    people.insert(PAIR(Student("usb", 99), 1008));
    people.insert(PAIR(Student("sca", 77), 1002));
    print(people);

    return 0;
}
#include <iostream>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    bool operator<(const Student &other) const 
    {
        return m_fScore <other.m_fScore;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ostream &operator<<(ostream &out, const Student *stu);
    friend ostream &operator<<(ostream &out, const Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ostream &operator<<(ostream &out, const Student *stu)
{
    out << stu->m_caName << ' ' << stu->m_fScore;
    return out;
}
ostream &operator<<(ostream &out, const Student &stu)
{
    out << stu.m_caName << ' ' << stu.m_fScore;
    return out;
}
class Compare
{
public:
    bool operator()(Student *s1, Student *s2)
    {
        return s1->getScore()>s2->getScore();
    }
};

typedef map<Student*, int, Compare> PEOMAP;
typedef pair<Student*, int> PAIR;

void print(const PEOMAP &peo)
{
    PEOMAP::const_iterator iter;
    iter = peo.begin();
    for (; iter != peo.end(); iter++)
    {
        cout << iter->first << ' ' << iter->second<< '\n';
    }
}
int main(void)
{
    PEOMAP people;
    people.insert(PAIR(new Student("zha", 90), 1001));
    people.insert(PAIR(new Student("as", 80), 1009));
    people.insert(PAIR(new Student("bs", 79), 1005));
    people.insert(PAIR(new Student("usb", 99), 1008));
    people.insert(PAIR(new Student("sca", 77), 1002));
    print(people);

    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,227评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,755评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,899评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,257评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,617评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,757评论 1 221
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,982评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,715评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,454评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,666评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,148评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,512评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,156评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,112评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,896评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,809评论 2 279
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,691评论 2 272

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,296评论 18 399
  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 4,373评论 1 31
  • 课程介绍 先修课:概率统计,程序设计实习,集合论与图论 后续课:算法分析与设计,编译原理,操作系统,数据库概论,人...
    ShellyWhen阅读 2,162评论 0 3
  • 基于树实现的数据结构,具有两个核心特征: 逻辑结构:数据元素之间具有层次关系; 数据运算:操作方法具有Log级的平...
    yhthu阅读 3,948评论 1 5
  • 高一那年,同样是班上填体放电影的时候,我在纸上写到:社会磨平大部分人的棱角,使众生泯然。 由我自己来想,这已经颇为...
    青禾木成阅读 282评论 0 0