【C++】LINK类型错误分析记录
LINK類型錯(cuò)誤
情況1:
根據(jù)生成路徑,查找是否成功生成靜態(tài)庫/動(dòng)態(tài)庫,一般在./bin文件中。
情況2:
是否在CMakeLists中target_link_libraries中添加鏈接靜態(tài)庫操作
情況3:
是都存在類模板,需要實(shí)例化,接口函數(shù),這種情況很隱蔽,一般不容易想到。
情況4:
是否需要添加相關(guān)的頭文件
情況5:
是否添加多余的頭文件 ,導(dǎo)致頭文件循環(huán)引用的問題,這種情況很隱蔽,一般不容易想到。
情況6:
沒有成功生成靜態(tài)庫或者靜態(tài)庫,這種情況下查找是否缺少依賴,是否程序缺少頭文件,是否是程序?qū)懙挠袉栴},不一而論。
C++ STL min_element 使用說明
說明
std::min_element 用于尋找范圍 [first, last) 中的最小元素。
前2個(gè)參數(shù)指定容器的范圍,第3個(gè)參數(shù)是比較函數(shù),為可選參數(shù)。
返回值為指向范圍 [first, last) 中最小元素的迭代器。
若范圍中有多個(gè)元素等價(jià)于最小元素,則返回指向首個(gè)這種元素的迭代器。若范圍為空則返回 last 。
關(guān)于比較函數(shù), 默認(rèn)是用 operator< 比較元素, 也可以自定義比較函數(shù)。
所以std::min_element兩種函數(shù)簽名如下:
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與std::min_element類似,只是用于尋找最大的元素。
頭文件
#include <algorithm>
例子:求數(shù)組里最下的元素
#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;
}
結(jié)果如下:
min element: 1
min element at:1
例子:自定義比較函數(shù)
比如如下自定義比較函數(shù),把求最小指編程求最大值
#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;
}
結(jié)果如下:
max element: 9
max element at:5
std::min_element 與 std::min 的區(qū)別
std::min一般用于求 a 與 b 的較小者 或者求 initializer_list ilist 中值的最小者。
std::min_element是求一個(gè)范圍內(nèi)的最小者的迭代器。范圍可以是全部容器,也可以是容器的一個(gè)子區(qū)間。
所以它們的適用范圍和返回值不一樣。
參考
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
總結(jié)
以上是生活随笔為你收集整理的【C++】LINK类型错误分析记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求花非花歌词
- 下一篇: 【C++】动态内存管理/move/以及移