C of NDK, 2021Android interview summary

IntSequence seq(1); //Sequence starting from 1
//Starting from 1, insert 9 elements into container col1
generate_n(back_inserter(col1), 9, seq);
//Still insert 9 elements into the container col1 starting from 1
generate_n(back_inserter(col1), 9, seq); 

generate function

#include <iostream>
#include <algorithm>
#include <array>
#include <functional>
using namespace std;
int main(){
    array<int,4> t1;
    //Generate 4 random numbers within 100
    generate(t1.begin(),t1.end(),[](){return rand()%100;});
    for_each(t1.begin(),t1.end(),[](int i){cout<<i<<"\t";});
    cout<<endl;
    //Generate 5 random numbers within 1000
    array<int,5> t2;
    generate_n(t2.begin(),5,[](){return rand()%1000;});
    for_each(t2.begin(),t2.end(),[](int i){cout<<i<<"\t";});
    return 0;
} 

Of course, there are also methods to solve the above problem of changing the internal state of the imitation function.

There are two methods:

1,Passing a functor by reference;

2,application for_each()The return value of the algorithm.

because for_each()The algorithm returns its functor. In other words, we can get the state of the functor through the return value. 

(1) Passing a functor by reference

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

class IntSequence{
private:
int value;
public:
    IntSequence(int initValue) : value(initValue) {}

    int operator()() {
         return value++;
    }
};

int main() {
    list<int> col1;
    IntSequence seq(1);
    //Adopt reference type
    generate_n < back_insert_iterator < list < int > > ,
            int, IntSequence &>(back_inserter(col1),
            4,
            seq);
    //1 2 3 4;
    for (auto t : col1) {
        cout << t << " ";
    }
    cout << endl;
    //It is equivalent to rebuilding an object and inserting four elements from 42
    generate_n(back_inserter(col1),4,IntSequence(42));
    //1 2 3 4; 42 43 44 45
    for (auto t : col1) {
        cout << t << " ";
    }
    cout << endl;
    //The reference type was used earlier, so the internal state of seq has been changed
    //Interpolation starts at 5 after last completion
    //Note: this call still uses the value passing type
    generate_n(back_inserter(col1),4,seq);
    //1 2 3 4 42 43 44 45 5 6 7 8
    for (auto t : col1) {
        cout << t << " ";
    }
    cout << endl;
    //The last call used the value passing type, so this time the interpolation starts from 5
    generate_n(back_inserter(col1),4,seq);
    //1 2 3 4 42 43 44 45; 5 6 7 8 5 6 7 8
    for (auto t : col1) {
        cout << t << " ";
    }
    cout << endl;
    return 0;
} 
  • The difference between imitative function and unary predicate
#include <iostream>
#Include < set > / / STL package
#Include < algorithm > / / algorithm package

using namespace std;

// Imitation function (strong expansibility) C + + built-in source code, high frequency of using imitation function and strong expansibility
class showActionObj {
public:
    int count = 0;
    void _count() { cout << "The output times are:" << this->count << endl; }

    void operator() (int __first) {
        cout << "functor " << __first << endl;
        count++;
    }
};

// Callback function (simple)
void showAction(int __first) {
    cout << "one-element predicate" << __first << endl;
}

int main() {
    // Understanding: type passing
    // set<int, showActionObj> setVar;  The grammar written in this way is OK and can't be bracketed
    set<int> setVar;

    setVar.insert(10);
    setVar.insert(20);
    setVar.insert(30);
    setVar.insert(40);
    setVar.insert(50);
    setVar.insert(60);

    //  In the first method, the printing times cannot be counted
    for_each(setVar.begin(), setVar.end(), showAction);

    //  The second method can count the printing times
    showActionObj s; // Understanding: Value Passing
    // The incoming s is the new copy, and the external s is the old address
    s = for_each(setVar.begin(), setVar.end(), s);
    s._count();
    return 0;
} 

(2) Use for_ Return value of each() algorithm

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class MeanValue {
private:
    long num;
    long sum;
public:
    MeanValue() : num(0), sum(0) {}
    void operator()(int elem) {
        num++;
        sum += elem;
    }

    double value() {
        return static_cast<double>(sum) / static_cast<double>(num);
    }
};

class MeanSum {
private:
    long sum;
public:
    MeanSum() : sum(0) {}

    void operator()(int elem) {
        sum += elem;
    }

    double value() {
        return sum;
    }
};

int main() {
    vector<int> col1;
    for (int i = 1; i <= 8; ++i) {
        col1.push_back(i);
    }
    for (auto t : col1) {
        cout << t << " ";
    }
    cout << endl;
    MeanValue mv = for_each(col1.begin(), col1.end(), MeanValue());
    MeanSum sum = for_each(col1.begin(), col1.end(), MeanSum());
    cout << "Mean Value: " << mv.value() << endl;
    cout << "Mean Sum: " << sum.value() << endl;
    return 0;
} 

(3) Judgement and imitation function

  • A judgement is a function or an imitation function that returns a Boolean. For STL, not all functions that return Boolean values are legal judgements. This may lead to many unexpected behaviors, such as the following example:
#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

class Nth {
private:
    int nth;
    int count;
public:
    Nth(int n) : nth(n), count(0) {}

    bool operator()(int) {
        return ++count == nth;
    }
};

int main() {
    list<int> col1;
    for (int i = 1; i <= 9; ++i) {
        col1.push_back(i);
    }
    list<int>::iterator pos;
    pos = remove_if(col1.begin(), col1.end(), Nth(3));
    col1.erase(pos, col1.end());
    for (auto t : col1) {
        cout << t << " ";
    }
    cout << endl;
    return 0;
} 

Predicate affine function

A functor whose return value is bool of boolean type is called a predicate. If operator() accepts one parameter, it is called a unary predicate. If operator() accepts two parameters, it is called a binary predicate.

one-element predicate

When you use the algorithm template in algorithm to process user-defined data, or do not want to process according to the default rules, you also need to write the predicate to complete the corresponding logical judgment and pass the predicate to the function template.

Built in function objects: many () have been overloaded in the functional header file of C + +, which can be used directly. The following are common built-in function objects:

  • 1. Arithmetic affine function: it is worth noting that the first five affine functions only support the operation of two operands with the same data type, so only the data type of one operand is passed. In addition, note that taking the inverse affine function is a unary predicate.
template<class T> T plus<T>//addition
template<class T> T minus<T>//subtraction
template<class T> T multiplies<T>//multiplication
template<class T> T divides<T>//division
template<class T> T modulus<T>//Take mold
template<class T> T negate<T>//Negative, that is, negative 
  • 2. Relational imitation functions: as above, they only support the comparison of two operands with the same data type, and only pass the data type of one operand.
template<class T> bool equal_to<T>//Judgment relative to=
template<class T> bool not_equal_to<T>//Judgment is not equal to=
template<class T> bool greater<T>//Judge greater than >
template<class T> bool greater_equal<T>//Judge greater than or equal to >=
template<class T> bool less<T>//Judge less than<
template<class T> bool less_equal<T>//Judge less than or equal to<= 
  • 3. Logic imitation function: similarly, logic and, logic or only support two operands with the same data type for operation. Only the data type of one operand needs to be passed. Note that logic is not a unary predicate.
template<class T> bool logical_and<T>//Logic and
template<class T> bool logical_or<T>//Logical or
template<class T> bool logical_not<T>//Logical non 

two-place predicate

  • Example 1
#include <iostream>
#include <set>

using namespace std;

class Cmp {
public:
    bool operator()(int a, int b) {
        return a > b;
    }
};

int main() {
    set<int, Cmp> s;
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    for ( auto it = s.begin(); it != s.end(); it++) {
        cout << *it << "\t";
### summary

**In fact, it's easy to master it. There are two main points:**

1.  Find a set of good video materials and follow Daniel's sorted knowledge framework for learning.
2.  Practice more( The advantage of video is that it has a strong sense of interaction and is easy to concentrate)

**You don't need to be a genius or have a strong talent. As long as you do these two points, the probability of success in the short term is very high.**

For many junior and intermediate Android For engineers, if they want to improve their skills, they often grope and grow by themselves. The learning effect of fragmentation is inefficient, long and helpless.**The screenshots of the following materials were sorted out by me for several months. They are full of sincerity: they are especially suitable for 3-5 Years of development experience Android Programmers learn.**

> **[CodeChina Open source projects:< Android study PDF+Architecture video+Interview documents+Source code Notes](https://codechina.csdn.net/m0_60958482/android_p7)**

![](https://img-blog.csdnimg.cn/img_convert/cbc54f9f9b233a44c364c2e076703b33.png)



(easy to concentrate)

**You don't need to be a genius or have a strong talent. As long as you do these two points, the probability of success in the short term is very high.**

For many junior and intermediate Android For engineers, if they want to improve their skills, they often grope and grow by themselves. The learning effect of fragmentation is inefficient, long and helpless.**The screenshots of the following materials were sorted out by me for several months. They are full of sincerity: they are especially suitable for 3-5 Years of development experience Android Programmers learn.**

> **[CodeChina Open source projects:< Android study PDF+Architecture video+Interview documents+Source code Notes](https://codechina.csdn.net/m0_60958482/android_p7)**

[External chain picture transfer...(img-nXCNQDTK-1630936831990)]



Tags: Android Design Pattern Interview

Posted on Mon, 06 Sep 2021 17:29:26 -0400 by KevMull