【C++ 面试 - 新特性】每日 3 题(五)

发布于:2024-09-18 ⋅ 阅读:(16) ⋅ 点赞:(0)

✍个人博客:Pandaconda-CSDN博客
📣专栏地址:http://t.csdnimg.cn/fYaBd
📚专栏简介:在这个专栏中,我将会分享 C++ 面试中常见的面试题给大家~
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

13. STL 中的绑定器是什么

  • bind1st:operator() 的第一个形参变量绑定成一个确定的值。
  • bind2nd:operator() 的第二个形参变量绑定成一个确定的值。
    应用实例:
#include <iostream>
#include <memory>
#include <thread>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

template<typename Container>
void showContainer(Container& con)
{
    //类型还没有实例化 编译器不知道后面套的是类型还是变量
    //加上typename 告诉编译器后面的是类型
    typename Container::iterator it = con.begin(); 
    for (; it != con.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;
}

int main()
{
    vector<int>vec;
    srand(time(nullptr));
    for (int i = 0; i < 20; i++) {
        vec.push_back(rand() % 100 + 1);
    }

    sort(vec.begin(), vec.end());
    showContainer(vec);

    sort(vec.begin(), vec.end(), greater<int>());
    showContainer(vec);

    //把70按序插入
    //库里只有二元的, 但是我们需要一元的
    //绑定器+二元函数对象 => 一元函数对象
    auto it1 = find_if(vec.begin(), vec.end(),
        bind1st(greater<int>(), 70));
    if (it1 != vec.end())
    {
        vec.insert(it1, 70);
    }
    else
        vec.push_back(70);
    showContainer(vec);

    return 0;
}

14. bind1st 和 bind2nd 的底层实现原理

封装了下,底层填充,变成二元。

#include <iostream>
#include <memory>
#include <thread>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

template<typename Container>
void showContainer(Container& con)
{
    //类型还没有实例化 编译器不知道后面套的是类型还是变量
    //加上typename 告诉编译器后面的是类型
    typename Container::iterator it = con.begin(); 
    for (; it != con.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;
}

template <typename Compare, typename T>
class _mybind1st
{
public:
    _mybind1st(Compare comp, T val)
        :_comp(comp), _val(val)
    { }
    bool operator()(const T& second)
    {
        return _comp(_val, second); //底层是二元函数对象
    }
private:
    Compare _comp;
    T _val;
};

template <typename Compare, typename T>
_mybind1st<Compare, T> mybind1st (Compare comp, const T& val)
{
    return _mybind1st<Compare, T>(comp, val); 
}

template<typename Iterator, typename Compare>
Iterator my_find_if(Iterator first, Iterator last, Compare comp)
{
    for (; first != last; ++first)
    {
        if (comp(*first))
        {
            return first;
        }
    }
    return last;
}

int main()
{
    vector<int>vec;
    srand(time(nullptr));
    for (int i = 0; i < 20; i++) {
        vec.push_back(rand() % 100 + 1);
    }

    sort(vec.begin(), vec.end());
    showContainer(vec);

    sort(vec.begin(), vec.end(), greater<int>());
    showContainer(vec);

    //把70按序插入
    //库里只有二元的, 但是我们需要一元的
    //绑定器+二元函数对象 => 一元函数对象
    auto it1 = my_find_if(vec.begin(), vec.end(),
            bind1st(greater<int>(), 70));
    if (it1 != vec.end())
    {
        vec.insert(it1, 70);
    }
    else
            vec.push_back(70);
    showContainer(vec);

    return 0;
}

15. bind 函数要如何使用?

#include <iostream>
#include <typeinfo>
#include <string>
#include <functional>

using namespace std;

void hello(string str) { cout << str << endl; }
int sum(int a, int b) { return a + b; }
class Test
{
public:
    int sum(int a, int b) { return a + b; }
};

int main()
{
    //bind 是函数模板,可以自动推演模板类型参数
    bind(hello, "hello")();
    cout << bind(sum, 10, 20)() << endl;
    cout << bind(&Test::sum, Test(), 20, 30)() << endl;

    //参数占位符 绑定器出了语句无法继续使用
    bind(hello, placeholders::_1)("测试占位符");
    cout << bind(sum, placeholders::_1, placeholders::_2)(10, 20) << endl;

    //此处把bind返回的绑定器复用起来了
    function<void(string)> func1 = bind(hello, placeholders::_1);
    func1("hello t1");
    func1("hello t2");

    return 0;
}
/*
hello
30
50
测试占位符
30
hello t1
hello t2
*/