久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<legend id='vc9gr'><style id='vc9gr'><dir id='vc9gr'><q id='vc9gr'></q></dir></style></legend>
    <bdo id='vc9gr'></bdo><ul id='vc9gr'></ul>
<i id='vc9gr'><tr id='vc9gr'><dt id='vc9gr'><q id='vc9gr'><span id='vc9gr'><b id='vc9gr'><form id='vc9gr'><ins id='vc9gr'></ins><ul id='vc9gr'></ul><sub id='vc9gr'></sub></form><legend id='vc9gr'></legend><bdo id='vc9gr'><pre id='vc9gr'><center id='vc9gr'></center></pre></bdo></b><th id='vc9gr'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vc9gr'><tfoot id='vc9gr'></tfoot><dl id='vc9gr'><fieldset id='vc9gr'></fieldset></dl></div>

    1. <tfoot id='vc9gr'></tfoot>

        <small id='vc9gr'></small><noframes id='vc9gr'>

        將 lambda 傳遞給函數模板

        Passing a lambda into a function template(將 lambda 傳遞給函數模板)

            • <small id='C2Vjc'></small><noframes id='C2Vjc'>

              <legend id='C2Vjc'><style id='C2Vjc'><dir id='C2Vjc'><q id='C2Vjc'></q></dir></style></legend>

                <tfoot id='C2Vjc'></tfoot>
              • <i id='C2Vjc'><tr id='C2Vjc'><dt id='C2Vjc'><q id='C2Vjc'><span id='C2Vjc'><b id='C2Vjc'><form id='C2Vjc'><ins id='C2Vjc'></ins><ul id='C2Vjc'></ul><sub id='C2Vjc'></sub></form><legend id='C2Vjc'></legend><bdo id='C2Vjc'><pre id='C2Vjc'><center id='C2Vjc'></center></pre></bdo></b><th id='C2Vjc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='C2Vjc'><tfoot id='C2Vjc'></tfoot><dl id='C2Vjc'><fieldset id='C2Vjc'></fieldset></dl></div>

                  <tbody id='C2Vjc'></tbody>
                • <bdo id='C2Vjc'></bdo><ul id='C2Vjc'></ul>

                  本文介紹了將 lambda 傳遞給函數模板的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在學習 C++,我正在嘗試實現一個二分查找函數,該函數查找謂詞所包含的第一個元素.該函數的第一個參數是一個向量,第二個參數是一個計算給定元素謂詞的函數.二分查找函數如下所示:

                  I'm learning C++, and I'm trying to implement a binary search function that finds the first element for which a predicate holds. The function's first argument is a vector and the second argument is a function that evaluates the predicate for a given element. The binary search function looks like this:

                  template <typename T> int binsearch(const std::vector<T> &ts, bool (*predicate)(T)) {
                      ...
                  }
                  

                  如果這樣使用,它會按預期工作:

                  This works as expected if used like this:

                  bool gte(int x) {
                      return x >= 5;
                  }
                  
                  int main(int argc, char** argv) {
                      std::vector<int> a = {1, 2, 3};
                      binsearch(a, gte);
                      return 0;
                  }
                  

                  但是如果我使用 lambda 函數作為謂詞,我會得到一個編譯器錯誤:

                  But if I use a lambda function as a predicate, I get a compiler error:

                  search-for-a-range.cpp:20:5: error: no matching function for call to 'binsearch'
                      binsearch(a, [](int e) -> bool { return e >= 5; });
                      ^~~~~~~~~
                  search-for-a-range.cpp:6:27: note: candidate template ignored: could not match 'bool (*)(T)' against '(lambda at
                        search-for-a-range.cpp:20:18)'
                  template <typename T> int binsearch(const std::vector<T> &ts,
                                            ^
                  1 error generated.
                  

                  以上錯誤是由

                  binsearch(a, [](int e) -> bool { return e >= 5; });
                  

                  怎么了?為什么編譯器不相信我的 lambda 有正確的類型?

                  What's wrong? Why is the compiler not convinced that my lambda has the right type?

                  推薦答案

                  你的函數 binsearch 接受一個函數指針作為參數.lambda 和函數指針是不同的類型:可以將 lambda 視為實現 operator() 的結構體實例.

                  Your function binsearch takes a function pointer as argument. A lambda and a function pointer are different types: a lambda may be considered as an instance of a struct implementing operator().

                  請注意,無狀態 lambda(不捕獲任何變量的 lambda)可隱式轉換為函數指針.由于模板替換,這里隱式轉換不起作用:

                  Note that stateless lambdas (lambdas that don't capture any variable) are implicitly convertible to function pointer. Here the implicit conversion doesn't work because of template substitution:

                  #include <iostream>
                  
                  template <typename T>
                  void call_predicate(const T& v, void (*predicate)(T)) {
                      std::cout << "template" << std::endl;
                      predicate(v);
                  }
                  
                  void call_predicate(const int& v, void (*predicate)(int)) {
                      std::cout << "overload" << std::endl;
                      predicate(v);
                  }
                  
                  void foo(double v) {
                      std::cout << v << std::endl;
                  }
                  
                  int main() {
                      // compiles and calls template function
                      call_predicate(42.0, foo);
                  
                      // compiles and calls overload with implicit conversion
                      call_predicate(42, [](int v){std::cout << v << std::endl;});
                  
                      // doesn't compile because template substitution fails
                      //call_predicate(42.0, [](double v){std::cout << v << std::endl;});
                  
                      // compiles and calls template function through explicit instantiation
                      call_predicate<double>(42.0, [](double v){std::cout << v << std::endl;});
                  }
                  

                  <小時>

                  你應該讓你的函數 binsearch 更通用,比如:

                  template <typename T, typename Predicate>
                  T binsearch(const std::vector<T> &ts, Predicate p) {
                  
                      // usage
                  
                      for(auto& t : ts)
                      {
                          if(p(t)) return t;
                      }
                  
                      // default value if p always returned false
                  
                      return T{};
                  }
                  

                  從標準算法庫中汲取靈感.

                  這篇關于將 lambda 傳遞給函數模板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)

                    • <bdo id='1MyB5'></bdo><ul id='1MyB5'></ul>
                    • <tfoot id='1MyB5'></tfoot>

                          1. <i id='1MyB5'><tr id='1MyB5'><dt id='1MyB5'><q id='1MyB5'><span id='1MyB5'><b id='1MyB5'><form id='1MyB5'><ins id='1MyB5'></ins><ul id='1MyB5'></ul><sub id='1MyB5'></sub></form><legend id='1MyB5'></legend><bdo id='1MyB5'><pre id='1MyB5'><center id='1MyB5'></center></pre></bdo></b><th id='1MyB5'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1MyB5'><tfoot id='1MyB5'></tfoot><dl id='1MyB5'><fieldset id='1MyB5'></fieldset></dl></div>

                            <small id='1MyB5'></small><noframes id='1MyB5'>

                              <tbody id='1MyB5'></tbody>
                            <legend id='1MyB5'><style id='1MyB5'><dir id='1MyB5'><q id='1MyB5'></q></dir></style></legend>
                          2. 主站蜘蛛池模板: 久久久91精品国产一区二区三区 | 国产成人99久久亚洲综合精品 | av在线影院 | 色综合一区二区 | 免费精品国产 | 国产精品综合视频 | 午夜影院视频在线观看 | 久久国内精品 | 亚洲国产精品第一区二区 | 一区二区三区亚洲视频 | 日本小电影在线 | 久久99精品国产自在现线小黄鸭 | 免费性视频 | 国产精品久久国产精品久久 | 欧美日韩不卡 | aaaaaaa片毛片免费观看 | 中文字幕第100页 | 精品一区二区三区不卡 | 伊人久久一区二区 | 国产视频久久久 | 蜜桃视频在线观看www社区 | 精品久久久久一区二区国产 | 久久aⅴ乱码一区二区三区 亚洲国产成人精品久久久国产成人一区 | 亚洲精品丝袜日韩 | 久久久久一区二区 | 中文字幕在线精品 | 天天干夜夜操 | 日韩欧美成人一区二区三区 | 久久免费视频网 | 黄色在线免费网站 | 精品国产一区二区国模嫣然 | 综合久久综合久久 | 国产99精品| 国产高清精品一区二区三区 | 欧美日韩亚洲国产 | 亚洲精品18 | 久久精品网 | 日韩精品一区二区三区中文字幕 | 国产欧美精品一区 | 亚洲精品一区中文字幕乱码 | 精品视频一区二区三区在线观看 |