Задачи для ТК (2015-2016)

31 views
Skip to first unread message

Alexey Sidnev

unread,
Apr 29, 2016, 2:36:10 AM4/29/16
to Основы программирования
У вас есть уникальная возможность повлиять на темы занятий на ТК. Можно проголосовать за наиболее интересный вариант по следующей ссылке: http://goo.gl/forms/RfXqXFwqiz

Alexey Sidnev

unread,
May 13, 2016, 2:36:12 AM5/13/16
to unn_pro...@googlegroups.com
1. const
class MyClass {
 
void print() const {
 
}
};


2. explicit
class MyClass {
 
explicit MyClass(int x) {
 
}
};


void qqq(MyClass m);


// Error!!!
qqq
(4);  // 4 -> MyClass


3. STL: vector, list, set, map, ...


vector
<int> v(5, 0);
for (vector<int>::iterator i = v.begin(); i != v.end(); ++i) {
  cout
<< *i;
}


set<int> s;


for (int i = 0; i < 100; ++i)
  s
.insert(rand());
 
int min = *(s.begin());
int max = *(++s.rbegin());


for (auto i = s.begin(); i != s.end(); ++i)
  cout
<< *i; //  Increase order


for (auto i = s.rbegin(); i != s.rend(); ++i)
  cout
<< *i; //  Decrease order
 
map
<int, string> m;
multimap
<int, string> multim;


m
[4] = "sdfgd";
m
[4] = "njksd";


multim
.emplace(4, "sdfgd");
multim
.emplace(4, "njksd");
multim
.insert(make_pair(4, "njksd"));  // pair


auto ret = multim.equal_range(4);
for (auto i = ret->first; i != ret->second; ++i)
  cout
<< i->second;
 
for (auto i = m.begin(); i != m.end(); ++i)
  cout
<< i->first << " " << i->second; //  Increase order
 
4. auto
auto x = 3;  // int
auto d = 4.5;  // double
auto f = 4.5f;  // float


for (auto i = v.begin(); i != v.end(); ++i) {
  cout
<< *i;
}


5. for (auto ...)
for (const int i : v) {
  cout
<< i;
}


for (const auto i : m) {
  cout
<< i.fisrt << i.second;
}


6. tuple
vector
<tuple<double, double ,double>> v2;

v2
.push_back(make_tuple(2.,3.,4.));
get<0>(v2[0]) = 5.;  // 5, 3, 4
get<1>(v2[0]) = 3.5;  // 5, 3.5, 4


auto &x = get<0>(v2[0]);
auto &y = get<1>(v2[0]);
auto &z = get<2>(v2[0]);

Alexey Sidnev

unread,
May 13, 2016, 2:37:48 AM5/13/16
to Основы программирования
7. override, final
class A {
 
virtual int test(int);
};


class B : public A {
 
virtual int test(short) override;  // Error
};


B x
;
x
.test(5);

8. unique_ptr, shared_ptr

void f(shared_ptr<int> p) {
  cout
<< *p;
}

shared_ptr
<int> g() {
 
return make_shared<int>(123);
}

int main() {
 
auto pi = g();
 
*pi = 456;
  f
(pi);
}

9. lambda, operator()
int test(double(*F)(int)) {
 
for(int i = 0; i < 100; ++i)
    cout
<< F(i);
}

struct Q {
 
int x;
 
int y;
 
double operator()(int) {
    x
++;
 y
++;
   
return (i + x + Y) / 10.;
 
}
};

int main() {
 
int x = 0;
 
int y;
  test
([=,&x](int i) -> double {
    x
++;
 y
++;
   
return (i + x + Y) / 10.;
 
});


  Q q
, q2;
  test
(q.operator());
}

Reply all
Reply to author
Forward
0 new messages