美文网首页
14.C++ 其他容器

14.C++ 其他容器

作者: 任振铭 | 来源:发表于2019-08-13 08:03 被阅读0次
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include "CSocketProtocol.h"
#include "CSocketFactoryImpl.h"
#include "CEncDesProtocol.h"
#include "HwEncode.h"
#include <algorithm>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <list>

using namespace std;


typedef int (*pFuc)(int a, int b);
int test(int a, int b) {
    cout << "test" << endl;
    return a + b;

}

int test2(int a, int b) {
    cout <<"test2" << endl;
    return a + b;

}
int test3(int a, int b) {
    cout << "test3"<< endl;
    return a + b;

}


int add(int (*p)(int a, int b)) {
    return p(5, 6);
}

void print(deque<int>& de) {
    for (deque<int>::iterator it = de.begin(); it < de.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}


void main() {
    //最大值优先队列
    priority_queue<int> p;
    priority_queue<int, vector<int>, less<int>> p1;
    //最小值优先队列
    priority_queue<int, vector<int>, greater<int>> p2;



    p1.push(12);
    p1.push(23);
    p1.push(1);
    p1.push(546);

    while (p1.size() > 0) {
        cout << p1.top() << " ";
        p1.pop();
    }

    p2.push(12);
    p2.push(23);
    p2.push(1);
    p2.push(546);
    while (p2.size() > 0) {
        cout << p2.top() << " ";
        p2.pop();
    }

    printf("-------------------------");
    list<int> l;
    for (int i = 0; i < 10; i++) {
        l.push_back(i);
    }

    for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    l.insert(l.begin(), 28);

    for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    l.remove(4);

    for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    printf("-------------------------");
    queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);

    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;



    printf("-------------------------");
    stack<int> s;
    s.push(1);
    s.push(2);
    s.push(3);
    while (!s.empty()) {
        cout<<s.top()<<" ";
        s.pop();
    }
    cout << endl;




    printf("-------------------------");



    deque<int> de;
    de.push_back(1);
    de.push_back(2);
    de.push_back(3);
    de.push_front(10);
    de.push_front(20);
    de.push_front(30);
    print(de);
    de.pop_back();
    de.pop_front();
    print(de);
    deque<int>::iterator it = find(de.begin(), de.end(), 10);
    int index = distance(de.begin(), it);
    cout << "index:" << index << endl;

    printf("-------------------------");
    

    
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    v.push_back(6);
    v.push_back(7);
    for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
        if (*it == 2) {
            it = v.erase(it);
        }
    }
    for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    v.insert(v.begin(), 8);
    for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include <algorithm>
#include <set>

using namespace std;

class Man
{
public:

    Man(int age) {
        this->age = age;
    }

public:
    int age;
};

struct BijiaoMan {
    bool operator()(const Man& left,const Man& right) {
        return left.age < right.age;
    }
};

void main() {

    //从小到达排序
    set<int> s;
    //set<int, less<int>> s;
    s.insert(2);
    s.insert(1);
    
    s.insert(3);
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    //从大到小排序
    set<int, greater<int>> s1;
    s1.insert(1);
    s1.insert(2);
    s1.insert(3);
    for (set<int, greater<int>>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    //functor 函数对象,仿函数(伪函数)作用就像Java的比较器
    Man m1(12), m2(4), m3(45),m4(23),s5(12);
    set<Man, BijiaoMan> sM;
    sM.insert(m1);
    sM.insert(m2);
    sM.insert(m3);
    sM.insert(m4);
    //s5存在相同的m1,所以无法插入set,怎么判断是否插入成功呢
    pair<set<Man, BijiaoMan>::iterator, bool> p = sM.insert(s5);
    if (p.second == true) {
        cout<<"s5插入成功"<<endl;
    }
    else {
        cout << "s5插入失败" << endl;
    }

    for (set<Man, BijiaoMan>::iterator it = sM.begin(); it != sM.end(); it++) {
        cout << it->age << " ";
    }
    cout << endl;
    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include <algorithm>
#include <map>

using namespace std;


void main() {
    map<int, string> m;

    //这三种方式重复插入,第二个插入报错
    m.insert(pair<int, string>(1, "aaa"));
    pair<map<int,string>::iterator,bool> p1 = m.insert(pair<int, string>(1, "bbb"));
    if (p1.second != true) {
        cout<<"p1插入失败"<<endl;
    }
    m.insert(make_pair(2, "bbb"));
    pair<map<int,string>::iterator,bool> p2 = m.insert(make_pair(2, "ccc"));
    if (p2.second != true) {
        cout<<"p2插入失败"<<endl;
    }
    m.insert(map<int, string>::value_type(3, "ccc"));
    pair<map<int,string>::iterator,bool> p3 = m.insert(map<int, string>::value_type(3, "ddd"));
    if (p3.second != true) {
        cout << "p3插入失败" << endl;
    }

    //这种方式重复插入,后者替换前者
    m[4] = "ddd";
    m[4] = "eee";

    for (map<int, string>::iterator it = m.begin(); it != m.end(); it++) {
        cout << it->first << " " << it->second << endl;
    }

    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include <algorithm>
#include <map>

using namespace std;


void main() {
    map<int, string> m;
    m.insert(pair<int, string>(1, "hello"));
    m.insert(make_pair(2, "world"));
    m.insert(map<int, string>::value_type(3, "haha"));
    m[4] = "byebye";

    for (map<int, string>::iterator it = m.begin(); it != m.end(); it++) {
        cout << it->first << " " << it->second << endl;
    }

    map<int,string>::iterator it = m.find(5);
    if (it == m.end()) {
        cout << "查找失败" << endl;
    }
    else {
        cout << "查找成功" << it->first << " " << it->second << endl;
    }

    pair<map<int,string>::iterator,map<int,string>::iterator> p = m.equal_range(3);
    if (p.first == m.end()) {
        cout << "第二次失败" << endl;
    }
    else {
        cout << "第二次成功 left=" << p.first->first << " " << p.first->second << " right=" << p.second->first << " " << p.second->second << endl;
    }

    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include <algorithm>
#include <map>

using namespace std;

class Person {
public:
    int age;
    string name;
    int salary;
};

void main() {
    Person p1, p2, p3, p4,p5;
    p1.age = 12;
    p1.name = "赵";
    p2.age = 13;
    p2.name = "钱";
    p3.age = 14;
    p3.name = "孙";
    p4.age = 15;
    p4.name = "李";
    p5.age = 16;
    p5.name = "周";
    multimap<string, Person> m;
    m.insert(pair<string, Person>("sale", p1));
    m.insert(pair<string, Person>("sale", p2));
    m.insert(make_pair("develop",p3));
    m.insert(map<string, Person>::value_type("develop", p4));
    m.insert(pair<string, Person>("manager", p5));

    for (map<string, Person>::iterator it = m.begin(); it != m.end(); it++) {
        cout << it->first << " " << it->second.name << endl;
    }

    int count  = m.count("develop");
    cout << "develop共有" <<count<<"人" << endl;


    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include <algorithm>
#include <vector>

using namespace std;

class Person {
public:
    Person(int age, const char* name) {
        this->name = new char[strlen(name) + 1];
        strcpy(this->name, name);
        this->age = age;
    }

    Person(const Person& p) {
        name = new char[strlen(p.name) + 1];
        strcpy(name, p.name);
        age = p.age;
    }
    ~Person() {
        if (this->name != NULL) {
            delete[]this->name;
            this->name = NULL;
        }
        this->age = 0;
    }
    
    int age;
    char* name;
};

void method() {
    Person p(12,"粉红色的积分" );
    vector<Person> v;
    v.push_back(p);

    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
        cout<<it->name<<" "<<it->age<<endl;
    }
}

void main() {
    
    method();
    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include <algorithm>
#include <vector>

using namespace std;

template<typename T>
class IsDivide {
public:
    IsDivide(const T& t) {
        this->t = t;
    }
    bool operator()(T& t1) {
        return (t1 % t == 0);
    }
private:
    T t;
};

void method() {
    vector<int> v;
    for (int i = 1; i < 20; i++) {
        v.push_back(i);
    }

    vector<int>::iterator it = find_if(v.begin(), v.end(), IsDivide<int>(4));
    if (it == v.end()) {
        cout << "未找到合适的值" << endl;
    }
    else {
        cout << "第一个可以被4整除的数是" << *it << endl;
    }
}
void main() {
    
    method();
    system("pause");
}

相关文章

网友评论

      本文标题:14.C++ 其他容器

      本文链接:https://www.haomeiwen.com/subject/submjctx.html