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

2017-04-14  本文已影响0人  雨打梨花闭门寒

今天讲的主要是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;
}
上一篇下一篇

猜你喜欢

热点阅读