14 C++ map与手写多元谓词

103 阅读1分钟

Android Ndk 学习笔记(目录)

void useMap(){
    map<int ,string> mapVar ;
    mapVar.insert(pair<int,string>(1,"一"));
    mapVar.insert(make_pair(2,"er"));
    mapVar.insert(map<int,string>::value_type(3,"san"));
//    mapVar.insert(pair<int,string>(4,"si"));
    // 前三种写法无法覆盖

    mapVar[4] = "si";
    mapVar[4] = "asd"; // 第四种写法 ,可以覆盖

    for (map<int,string>::iterator it = mapVar.begin(); it!=mapVar.end() ; it++) {

        cout << it->first << "," << it->second.c_str() <<endl ;
    }

    std::pair<map<int,string>::iterator , bool> result = mapVar.insert(make_pair(5,"5"));

    if (result.second){
        cout << "success" << endl;
    }else{
        cout << "faile" << endl;
    }

    map<int, string> ::iterator findResult = mapVar.find(3);
    if (findResult != mapVar.end()) {
        cout << "找到了" << findResult->first << "," << findResult->second.c_str() << endl;
    } else {
        cout << "没找到" << endl;
    }
}
void useMultimap(){
    // 1.key可以重复, 2.key重复的数据可以分组,  3.key会排序,  4.value不会排序
    multimap<int, string> multimapVar;

    multimapVar.insert(make_pair(10, "十个1"));
    multimapVar.insert(make_pair(10, "十个2"));
    multimapVar.insert(make_pair(10, "十个3"));

    multimapVar.insert(make_pair(30, "三十1"));
    multimapVar.insert(make_pair(30, "三十3"));
    multimapVar.insert(make_pair(30, "三十2"));


    multimapVar.insert(make_pair(20, "二十1"));
    multimapVar.insert(make_pair(20, "二十2"));
    multimapVar.insert(make_pair(20, "二十3"));

    for (auto iteratorVar = multimapVar.begin(); iteratorVar != multimapVar.end() ; iteratorVar ++) {
        cout << iteratorVar->first << "," << iteratorVar->second << endl;
    }
    cout << endl;

    // TODO 核心功能是分组
    int result;
    cout << "请输入你要查询的key,为int类型:" << endl;
    cin >> result;

    multimap<int, string>::iterator iteratorVar = multimapVar.find(result);
    while (iteratorVar != multimapVar.end()) {
        cout << iteratorVar->first << "," << iteratorVar->second << endl;

        // 需要自己做逻辑控制,不然有问题
        iteratorVar++;

        if (iteratorVar->first != result) {
            break;; // 循环结束
        }

        // 严谨性
        if (iteratorVar == multimapVar.end()) {
            break;; // 循环结束
        }
    }
}
// 谓词  == 仿函数

class ComPareObject{

public:
    void operator()(){
        cout << "仿函数" << endl;
    }
};
// 普通函数
void fun2() {
    cout << "普通函数" << endl;
}
void showAction(int content) {
    cout << "自定义 一元谓词" << content << endl;
}
class showActionObj {
public:
    void operator()(int content) {
        cout << "自定义仿函数" << content << endl;
    }
};
class showActionObj2 {
public:
    int count = 0;
    void _count() { cout << "本次输出次数是:" << this->count << endl; }

    void operator() (int __first) {
        cout << "仿函数" << __first << endl;
        count++;
    }
};
void useForeach(){

    set<int> setVar ;
    setVar.insert(10);
    setVar.insert(10);
    setVar.insert(10);
    setVar.insert(10);
    setVar.insert(10);


    for_each(setVar.begin(),setVar.end(),showAction);
    for_each(setVar.begin(),setVar.end(),showActionObj());

    showActionObj2 s2 ;
    s2 = for_each(setVar.begin(),setVar.end(),s2);
    s2._count();
}
// C++ 预定义函数(C++ 内置函数)

void usePlus(){
    plus<int> add_func;

    int r = add_func(1, 1);
    cout << r << endl;

    plus<string> add_func2;
    string r2 = add_func2("AAAA", "BBB");
    cout << r2 << endl;

    plus<float> add_func3;
    float r3 = add_func3(4354.45f, 34.3f);
    cout << r3 << endl;
}


// 自定义函数
template<typename T>
struct plus_d{
    T operator()(const T & t1 ,const T & t2){
        return t1+t2;
    }
};

void usePlusd(){
    plus_d<int> add_func;
    int r = add_func(1, 1);
    cout << r << endl;

    plus_d<string> add_func2;
    string r2 = add_func2("AAAA", "BBB");
    cout << r2 << endl;

    plus_d<float> add_func3;
    float r3 = add_func3(4354.45f, 34.3f);
    cout << r3 << endl;
}