栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++容器和函数对象(仿函数)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++容器和函数对象(仿函数)

vector、set、map容器 vector和set的声明和遍历

vector的嵌套,大vector中嵌套多个小vector

#include 
#include 

using namespace std;

void test1() {
    vector > vec;
    vector v1;
    vector v2;
    vector v3;
    for (int i = 0; i < 4; i++) {
        v1.push_back(i + 1);
        v2.push_back(i + 2);
        v3.push_back(i + 3);
    }
    vec.push_back(v1); // 将三个vector容器放入一个大vector容器中
    vec.push_back(v2);
    vec.push_back(v3);
    // 遍历嵌套容器
//    for (vector >::iterator it = vec.begin(); it < vec.end(); it++) {
//        for (vector::iterator vit = (*it).begin(); vit < (*it).end(); vit++) { // *it只解引用出的小容器
//            cout << (*vit) << " ";
//        }
//        cout << endl;
//    }
    // 使用auto也可实现遍历
    for (auto it = vec.begin(); it < vec.end(); it++) {
        for (auto vit = (*it).begin(); vit < (*it).end(); vit++) { // *it只解引用出的小容器
            cout << (*vit) << " ";
        }
        cout << endl;
    }
}

int main() {
    test1();
    return 0;
}

map的声明和遍历
#include 
#include 
#include 

using namespace std;

//class MyCompare {
//public:
//    bool operator()(int v1, int v2) {
//        return v1>v2;
//    }
//};

// 传入指针也可!打印map下的数据
void printMap(map *map1) {
    for (map::iterator it = map1->begin(); it != map1->end(); it++) {
        cout << "key:" << (*it).first << " value:" << (*it).second << endl;
    }
}

//void printMap2(map *mapn) {
//    for (map::iterator it = mapn->begin(); it != mapn->end(); it++) {
//        cout << "key:" << (*it).first << " value:" << (*it).second << endl;
//    }
//}

int main() {
    // set
    set s;
    s.insert(20);
    s.insert(10);
    s.insert(40);
    s.insert(30);

    pair::iterator, bool> ret = s.insert(10);
    if (ret.second) {
        cout << "successful!" << endl;
        return 0;
    }
    cout << "fail" << endl;

    // map
    map map1;
    map1.insert(pair(1, 666));
    map1.insert(make_pair(2, 777)); // 这样也可制造对组
    map1.insert(make_pair(3, 888));

    printf("------------n");
    printMap(&map1);
    printf("------------n");
    map::iterator pos = map1.find(1);
    if (pos != map1.end()) {
        cout << "key为" << pos->first << " value为" << pos->second << endl;
//        return 0;
    } else {
        cout << "未找到该key对应的value" << endl;
    }
    printf("------------");

//    map map2;
//    map2.insert(pair(1, 666));
//    map2.insert(make_pair(2, 777)); // 这样也可制造对组
//    map2.insert(make_pair(3, 888));
    printMap2(&map2);
//    for (map::iterator it = map2.begin(); it != map2.end(); it++) {
//        cout << "key:" << (*it).first << " value:" << (*it).second << endl;
//    }
    return 0;
}

map仿函数(函数对象)

当给对象排序时,必须使用仿函数(即类)
bool operator()(int k1, int k2) const { // 仅根据key排序 return k1 > k2; }

#include 
#include 

using namespace std;

class MyCompare {
public:
    // 警告:这里不加const将运行报错!!!
    bool operator()(int k1, int k2) const { // 仅根据key排序
        return k1 > k2;
    }
};

int main() {

    map map2;
    map2.insert(pair(1, 666));
    map2.insert(make_pair(2, 999)); // 这样也可制造对组
    map2.insert(make_pair(3, 888));
//    printMap2(&map2);
    for (map::iterator it = map2.begin(); it != map2.end(); it++) {
        cout << "key:" << (*it).first << " value:" << (*it).second << endl;
    }
    return 0;
}

函数对象的三种使用方式
#include 

using namespace std;


class MyAdd {
public:
    int operator()(int a, int b) {
        return a + b;
    }
};

class MyPrint {
public:
    int count;

    MyPrint() {
        this->count = 0;
    }

    void operator()(string text) {
//        this->count++; // 阔以~
        count++;
        cout << text << endl;
    }
};

void test1() {
    MyAdd myAdd;
    int ret = myAdd(1, 2);
    cout << "ret:" << ret << endl;
}

void test2() {
    MyPrint myPrint;
    myPrint("lwt1");
    myPrint("lwt2");
    myPrint("lwt3");
    cout << "调用仿函数的次数:" << myPrint.count << endl;
}

void doPrint(MyPrint &mp,string text){
    mp(text);
}
void test3(){
    MyPrint myPrint;
    doPrint(myPrint,"void doPrint(MyPrint &mp,string text) lwt666");
}
int main() {
    test1();
    test2();
    test3();
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号