[C + +] LINK type error analysis record

Posted by BigToach on Mon, 31 Jan 2022 18:59:57 +0100

LINK type error

Case 1:

According to the generation path, find out whether the static library / dynamic library is successfully generated, generally in/ In the bin file.

Case 2:

Target in CMakeLists_ link_ Adding linked static libraries in Libraries

Case 3:

Yes, there are class templates, which need to be instantiated and interface functions. This situation is very hidden and generally not easy to think of.

Case 4:

Do you need to add related header files

Case 5:

Whether to add redundant header files leads to the problem of circular reference of header files. This situation is very hidden and generally not easy to think of.

Case 6:

The static library or static library is not generated successfully. In this case, find out whether there is a lack of dependency, whether the program lacks a header file, and whether there is a problem with the writing of the program.

C++ STL min_element instructions

explain

std::min_element is used to find the smallest element in the range [first, last].

The first two parameters specify the scope of the container, and the third parameter is the comparison function, which is an optional parameter.
The return value is an iterator pointing to the smallest element in the range [first, last].
If more than one element in the scope is equivalent to the smallest element, an iterator pointing to the first such element is returned. If the range is empty, it returns last.

For comparison functions, operator < comparison element is used by default. You can also customize the comparison function.
So STD:: Min_ The signatures of the two functions of element are as follows:

template< class ForwardIt > 
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );

std::max_element and std::min_element is similar, but it is used to find the largest element.

Header file

#include <algorithm>

Example: find the lowest element in the array

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

int main(int argc, char **argv) 
{  
    std::vector<int> v{3, 1, 4, 1, 5, 9};
    //auto minElement = std::min_element(v.begin(), v.end());
    std::vector<int>::iterator minElement = std::min_element(v.begin(), v.end());
    
    std::cout << "min element: " << *(minElement) << std::endl;
    std::cout << "min element at:" << std::distance(v.begin(), minElement) << std::endl;
    return 0;
}

The results are as follows:

min element: 1
min element at:1

Example: Custom comparison function

For example, the following user-defined comparison function is used to program the minimum to the maximum

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

int main(int argc, char **argv) 
{  
    std::vector<int> v{3, 1, 4, 1, 5, 9};
    
    auto comp = [](int i, int j){ return i>j;}; //  i<j : min;  i>j : max   
    
    std::vector<int>::iterator maxElement = std::min_element(v.begin(), v.end(), comp);
    
    std::cout << "max element: " << *(maxElement) << std::endl;
    std::cout << "max element at:" << std::distance(v.begin(), maxElement) << std::endl;
    return 0;
}

The results are as follows:

max element: 9
max element at:5

std::min_ Difference between element and STD:: Min

std::min is generally used to find the smaller of a and b or to find the initializer_ The smallest of the list IList median.
std::min_element is an iterator that finds the smallest in a range. The range can be all containers or a sub interval of containers.
Therefore, their scope of application is different from the return value.

reference resources

https://zh.cppreference.com/w/cpp/algorithm/min_element
http://www.cplusplus.com/reference/algorithm/min_element/
https://zh.cppreference.com/w/cpp/algorithm/min