База ответов ИНТУИТ

Программирование на С/С++

<<- Назад к вопросам

Какие из объявлений лямбда функций ниже выполнены корректно?

(Ответ считается верным, если отмечены все правильные варианты ответов.)

Варианты ответа
auto lf3 = [] (int x) -> double {return x+1;};(Верный ответ)
auto lf2 = [] -> double (int x) {return x+1;};
auto lf1 = [] (int x) {return x+1;};(Верный ответ)
auto lf4 = double [] (int x) {return x+1;};
Похожие вопросы
Каковы преимущества использования лямбда функций?
<pre> //====================== start of sample.cpp ========================== int main(int argc, char* argv[]) { int a = 0; int b = 0; int X = 0; auto lf1 = [&amp;a,&amp;b,&amp;X] (int x) {return x &gt; 0;}; auto lf2 = [a,b] (int x) { x++; return x;}; auto lf3 = [=] (int x) { x++; return x;}; auto lf4 = [&amp;] (int x) { x++; return x;}; auto lf5 = [] (bool&amp; z) { z = !z; return;}; return 0; } //====================== end of sample.cpp ========================== </pre> Какие из приведённых в примере лямбда функций могут изменить значения переменных а и b?
<pre> //====================== start of sample.h ========================== int main(int argc, char* argv[]) { auto lf1 = [] (int x) {return x > 0;}; auto lf2 = [] (int x) -> bool {return x+1;}; auto lf3 = [] (int&amp; x) { x++; return;}; auto lf4 = [] (int x, int&amp; y) -> double {return x+y;}; auto lf5 = [] (bool&amp; z) { z = !z; return;}; return lf1(0); } //====================== end of sample.h ========================== </pre> Какие из приведённых выше лямбда функций возвращают тип bool?
<pre> //====================== start of sample.cpp ========================== #include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/weak_ptr.hpp&gt; #include &lt;vector&gt; struct A { virtual ~A(); }; struct B: public A {}; struct C {}; void foo1(boost::shared_ptr&lt;A&gt;&amp; a); void foo2(boost::shared_ptr&lt;A const&gt; a); void foo3(boost::shared_ptr&lt;B&gt;&amp; a); void foo4(boost::shared_ptr&lt;const A&gt; a, boost::shared_ptr&lt;C&gt; c); void foo5(std::vector&lt; boost::shared_ptr&lt;C&gt; &gt;&amp; c); int main(int argc, char* argv[]) { boost::shared_ptr&lt;A&gt; b1(new A); boost::shared_ptr&lt;B&gt; b2(new B); boost::shared_ptr&lt;C&gt; b3(new C); boost::weak_ptr&lt;A&gt; b4(b1); std::vector&lt;boost::shared_ptr&lt;C&gt;&gt; v1; v1.push_back(b3); foo1(b1); foo2(b2); foo3(b3); foo4(b4.lock(), b3); foo5(v1); return 0; } //====================== end of sample.cpp ========================== </pre> Вызовы каких функций выполнены корректно и операции создания их параметров не содержат очевидных проблем?
<pre> //====================== start of sample.cpp ========================== #include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/scoped_ptr.hpp&gt; #include &lt;boost/weak_ptr.hpp&gt; #include &lt;vector&gt; struct A { virtual ~A(); }; struct B: public A {}; struct C {}; void foo1(boost::shared_ptr&lt;A&gt; a); void foo2(boost::weak_ptr&lt;B&gt;&amp; a); void foo3(boost::shared_ptr&lt;C&gt; a); void foo4(boost::scoped_ptr&lt;A&gt;&amp; a); void foo5(std::vector&lt; boost::weak_ptr&lt;C&gt; &gt;&amp; c); int main(int argc, char* argv[]) { boost::shared_ptr&lt;A&gt; a(new A); boost::shared_ptr&lt;B&gt; b(new B); boost::shared_ptr&lt;C&gt; c(new C); boost::weak_ptr&lt;A&gt; b1(a); boost::weak_ptr&lt;B&gt; b2(b); boost::weak_ptr&lt;C&gt; b3(c); std::vector&lt; boost::weak_ptr&lt;C&gt; &gt; v1; v1.push_back(b3); foo1(b2.lock()); foo2(b2); try { boost::shared_ptr&lt;C&gt; c1(c); foo3(c1); } catch(boost::bad_weak_ptr&amp; e) { } foo4(b2.lock()); foo5(v1); return 0; } //====================== end of sample.cpp ========================== </pre> Вызовы каких функций выполнены корректно и операции создания их параметров не содержат очевидных проблем?
<pre> //====================== start of sample.cpp ========================== #include &lt;algorithm&gt; #include &lt;vector&gt; int main(int argc, char* argv[]) { std::vector&lt;int&gt; v1; auto lf1 = [] (int x) {return x > 0;}; auto lf3 = [] (int&amp; x) { x++; return;}; auto lf5 = [] (bool&amp; z) { z = !z; return;}; int cnt1 = std::count_if(v1.begin(), v1.end(), lf1); int cnt2 = std::count_if(v1.begin(), v1.end(), [] (int x) -> bool {return x+1;}); int cnt3 = std::count_if(v1.begin(), v1.end(), lf3); int cnt4 = std::count_if(v1.begin(), v1.end(), [] (int x, int&amp; y) -> double {return x+y;}); int cnt5 = std::count_if(v1.begin(), v1.end(), lf5); return cnt1; } //====================== end of sample.cpp ========================== </pre> При вычислении каких переменных лямбда-функции в алгоритме count_if используются корректно?
В каких случаях эффективно использование лямбда-функции?
Какую парадигму программирования поддерживают лямбда-функция в языке С/С++
<pre> //====================== start of sample.cpp ========================== int main(int argc, char* argv[]) { int a = 0; int b = 0; int X = 0; auto lf1 = [a,b,X] (int x) {return x > 0;}; auto lf2 = [a,b] (int x) { x++; return x;}; auto lf3 = [=] (int x) { x++; return x;}; auto lf4 = [&amp;] (int x) { x++; return x;}; auto lf5 = [] (bool&amp; z) { z = !z; return;}; return 0; } //====================== end of sample.cpp ========================== </pre> Какие лямбда функции из примера выше имеют доступ к значению переменной X?
В ходе разработки дизайна должны быть обязательно выполнены следующие шаги: