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

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

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

<pre> //====================== start of sample.cpp ========================== template &lt;typename ForwardIterator, typename T&gt; void replace ( ForwardIterator first, ForwardIterator last, const T&amp; x, const T&amp; y) { while(first != last) { if (*first == x) *first = y; ++first; } return first; } //====================== end of sample.cpp ========================== </pre> Каковы требования к итераторам, участвующим в алгоритме replace(), имплементация которого приведена выше, корректны?

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

Варианты ответа
тип ForwardIterator должен поддерживать операцию разыменования(Верный ответ)
объект типа Т должен поддерживать операцию сравнения "равно"(Верный ответ)
объект типа Т должен поддерживать операцию сравнения "меньше"
тип ForwardIterator должен поддерживать перезапись объекта, хранящегося в элементе контейнера, на который ссылается итератор(Верный ответ)
тип ForwardIterator должен поддерживать вставку нового элемента в контейнер
Похожие вопросы
<pre> //====================== start of sample.cpp ========================== #include &lt;type_traits&gt; template &lt;typename IT_1, typename IT_2, bool b&gt; IT_2 copy_imp(IT_1 first, IT_1 last, IT_2 out, const std::integral_constant&lt;bool, b&gt;&amp;) { while(first != last) { *out = *first; ++out; ++first; } return out; } template &lt;typename T&gt; T* copy_imp(const T* first, const T* last, T* out, const std::true_type&amp;) { memmove(out, first, (last-first)*sizeof(T)); return out * (last-first); } template &lt;typename I1, typename I2&gt; inline I2 copy(I1 first, I1 last, I2 out) { typedef typename std::iterator_traits&lt;I1&gt;::value_type value_type; return copy_imp(first, last, out, std::has_trivial_assign&lt;value_type&gt;()); } class A {}; int main() { std::vector&lt;A&gt; vec1; std::vector&lt;A&gt; vec2; copy(vec1.begin(), vec1.end(), vec2.begin()); return 0; } //====================== end of sample.cpp ========================== </pre> Какие утверждения про то какая функция копирования copy_impl() будет использована верны?
<pre> //====================== start of sample.cpp ========================== #include &lt;type_traits&gt; template &lt;typename IT_1, typename IT_2, bool b&gt; IT_2 copy_imp(IT_1 first, IT_1 last, IT_2 out, const std::integral_constant&lt;bool, b&gt;&amp;) { while(first != last) { *out = *first; ++out; ++first; } return out; } template &lt;typename T&gt; T* copy_imp(const T* first, const T* last, T* out, const std::true_type&amp;) { memmove(out, first, (last-first)*sizeof(T)); return out * (last-first); } template &lt;typename I1, typename I2&gt; inline I2 copy(I1 first, I1 last, I2 out) { typedef typename std::iterator_traits&lt;I1&gt;::value_type value_type; return copy_imp(first, last, out, std::has_trivial_assign<value_type>()); } class A {}; int main() { std::vector&lt;short&gt; arr1; std::vector&lt;short&gt; arr2; copy(arr1.begin(), arr1.end(), arr2.begin()); return 0; } //====================== end of sample.cpp ========================== </pre> Какие утверждения про используемую функцию копирования copy_impl() верны?
<pre> //====================== start of sample.cpp ========================== template &lt;typename T&gt; class multiplies: public binary_function&lt;T,T,T&gt; { public: T operator() (const T&amp; x, const T&amp; y) const { return x * y; } }; //====================== end of sample.cpp ========================== </pre> Какие утверждения про приведённый выше код функтора multiplies верны?
<pre> //====================== start of sample.cpp ========================== template&lt;typename T&gt; typename T::difference_type my_diff( T&amp; v1, T&amp; v2); template&lt;typename T&gt; T my_diff2(T&amp; v1, T&amp; v2); class A { public: A(int in = 5); typedef short difference_type; difference_type operator-(A&amp;); }; int main() { A i(5); A j(10); my_diff(i,j); int x = 5; int y = 10; my_diff(x,y); return 0; } //====================== end of sample.cpp ========================== </pre> Какие проблемы может решить использование идиомы SFINAE в вышеприведённом коде?
<pre> //====================== start of sample.cpp ========================== template &lt;typename T&gt; struct remove_extend { typedef T type; }; template &lt;typename T, std::size_t N&gt; struct remove_extend&lt;T[N]&gt; { typedef T type; }; //====================== end of sample.cpp ========================== </pre> Что произойдёт если не определять специализированный шаблон remove_extend<T[N]> для массива??
<pre> //====================== start of sample.cpp ========================== class User { public: struct Region { short country_code; short city_code; static char* city_name; }; const char* name; int age; double balance; Region region; const char* get_name() const { return (*this).name; } int get_age() const { return this-&gt;age; } double get_balance() const { return (*this)-&gt;balance; } short get_country_code() const { return Region::country_code; } short get_city_code() const { return region.city_code; } const char* get_city_name() const { return Region::city_name; } }; //====================== end of sample.cpp ========================== </pre> В каких методах обращения к членам объекта класса user типа User корректны?
<pre> //====================== start of sample.cpp ========================== template&lt;unsigned long N&gt; class binary { public: static unsigned long const value = binary&lt;N / 10&gt;::value &lt;&lt; 1 | N % 10; }; template&lt;&gt; class binary&lt;0&gt; { public: static unsigned long const value = 0; }; template&lt;&gt; class binary&lt;1&gt; { public: static unsigned long const value = 1; }; int main(int argc, char* argv[]) { static unsigned const x0 = binary&lt;0&gt;::value; static unsigned const x1 = binary&lt;1000&gt;::value; static unsigned const x2 = binary&lt;1001&gt;::value; static unsigned const x3 = binary&lt;1010&gt;::value; static unsigned const x4 = binary&lt;1011&gt;::value; return 0; } //====================== end of sample.cpp ========================== </pre> При инициализации какой переменной не будет использоваться конкретизация шаблона с параметром 0?
<pre> //====================== start of sample.cpp ========================== int main(int argc, char* argv[]) { const int a = 0; int b = 0; const int X = 0; int Y = 0; int Z = 0; auto lf1 = [&amp;Y, Z, &amp;a, &amp;b, X] (double x) {return x &gt; 0;}; return 0; } //====================== end of sample.cpp ========================== </pre> Значения каких переменным лямбда функция lf1 может изменить?
<pre> //====================== start of sample.h ========================== #include &lt;algorithm&gt; int main(int argc, char* argv[]) { size_t N = 40; int A[N]; size_t B[N*2]; char C[N]; int D[N]; std::replace(&amp;B[0], &amp;B[N/2], 0, 42); std::replace(C, (C+N+1), 'D', 'T'); std::copy(&amp;A[0], &amp;A[N-1], &amp;D[0]); std::copy(A, (A+N), D); return 0; } //====================== end of sample.h ========================== </pre> В каких из вызовов обобщённых алгоритмов встроенные массивы С++ использованы без ошибок?
<pre> //====================== start of sample.cpp ========================== template&lt;unsigned long N&gt; class binary { public: static unsigned long const value = binary&lt;N / 10&gt;::value &lt;&lt; 1 | N % 10; }; template&lt;&gt; class binary&lt;0&gt; { public: static unsigned long const value = 0; }; int main(int argc, char* argv[]) { static unsigned const xyz = binary&lt;111&gt;::value; return 0; } //====================== end of sample.cpp ========================== </pre> Конкретизация шаблона с значением 111 приводит: