int * minArr
внутри search_min_elements_of_array
отражаются на массиве int minArr[m/2]
, и отдельно что-то возвращать не надо.search_min_elements_of_array
за пределы (перед) main
, ну и вызов у вас неверный:// int minArray[m/2] = search_min_element_of_array(int array[][3], int *minArr); // больше похоже на объявление функции
search_min_element_of_array(array, minArr);
int & foo()
{
int x = 100;
return x;
}
int main()
{
int & y = foo();
y = 10; // UB
}
class Test
{
int x;
public:
int & getx() { return x; }
};
int main()
{
Test t;
int & y = t.getx();
y = 10; // t.x будет равен 10
}
class Test
{
int x;
public:
int & getx() { return x; }
};
int main()
{
int * y;
{
Test t;
y = &t.getx();
}
*y = 10; // UB, t умирает в конце блока, x тоже, а обращение идёт уже после
}
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
int str_compare(std::string const & l, std::string const & r)
{
if (l == r)
return 0;
return l < r ? -1 : 1;
}
int str_compare(void const * l, void const * r) { return str_compare(*static_cast<std::string const *>(l), *static_cast<std::string const *>(r)); }
int main()
{
std::string strs[3] = { "foo", "bar", "baz" };
std::qsort(strs, 3, sizeof(std::string), str_compare); // В общем, работает, но UB
// std::sort(&strs[0], &strs[0] + 3); // вот так - лучше
for (auto & s : strs)
std::cout << s << std::endl;
return 0;
}
#include "stdafx.h"
#include <thread>
#include <vector>
#include <iostream>
void foo(std::size_t i)
{
// nothing to do
}
int main()
{
std::vector<std::thread> ths;
std::size_t n;
std::cin >> n;
for (std::size_t i = 0; i < n; ++i)
ths.push_back(std::thread(&foo, i));
for (auto & th : ths)
th.join();
return 0;
}
Ret (Class::*)(Args...) [const]
. Сам указатель получать &Class::Fun
, а вызывать операторами .*
или ->*
std::function
(boost::function
), std::bind
(boost::bind
) или std::mem_fun, std::mem_fn
для более унифицированного подхода:struct some
{
some() : value(0) {}
int inc() { return ++value; }
int dec() { return --value; }
int get() const { return value; }
int value;
};
int main()
{
some s;
int (some::*inc_f)() = &some::inc; // inc_f - указатель на функцию
std::cout << "(s.*inc_f)() = " << (s.*inc_f)() << std::endl; // оператор .* - оператор вызова по указателю на функцию
int (some::*val_ptr) = &some::value; // val_ptr - указатель на член
s.*val_ptr = 10;
std::cout << "s.*val_ptr = " << s.*val_ptr << std::endl;
int (some::*const_f)() const = &some::get; // const-функция
std::cout << "s.*const_f() = " << (s.*const_f)() << std::endl;
using namespace std::placeholders;
std::function<int (some&)> mem_f;
mem_f = std::bind(&some::inc, _1); // биндим на функцию-член, _1 - placeholder для объекта
std::cout << "mem_f(s) = " << mem_f(s) << std::endl; // s.inc();
mem_f = std::mem_fn(&some::dec); // другой способ через mem_fn
std::cout << "mem_f(s) = " << mem_f(s) << std::endl; // s.dec();
std::function<int()> mem_f_ = std::bind(&some::inc, &s); // биндим на функцию член и сразу указываем объект, получаем функцию без аргументов
std::cout << "mem_f_() = " << mem_f_() << std::endl; /// s.inc();
std::function<int(some const &)> const_fn = std::mem_fn(&some::get); // some const &
std::cout << "const_fn(s) = " << const_fn(s) << std::endl;
return 0;
}
std::map<int, std::size_t>
std::map<int, std::size_t> m;
for (int v : vals) { ++m[v]; }
std::vector<std::pair<int, std::size_t> > v(m.begin(), m.end());
std::sort(v.begin(), v.end(), [] (std::pair<int, std::size_t> const & l, std::pair<int, std::size_t> const & r) { return l.second > r.second; });
// в v пары "число - кол-во таких чисел", отсортированы по убыванию
// можно откинуть нижнюю часть (те, которые встречаются реже, чем какой-то процент, например, 10%)
v.erase(
std::find_if(v.begin(), v.end(), [] (std::pair<int, std::size_t> const & x) { return x.second < (v.size() / 10); }),
v.end());
// а сверху взять часто встречающиеся
int row_value = v.front().first;
struct foo { foo(int const & x) : x_(x) {}; int const & x_; };
int bar() { return 42; }
void baz() { foo f(bar()); ... } // здесь в foo передается ссылка на временный объект, который умирает в конце выражения, однако ссылка сохраняется в объекте f.
struct foo { foo(int & x) : x_(x) {}; int & x_; };
void foo()
{
std::shared_ptr<foo> f;
{
int x;
f.reset(new foo(x));
} // тут x помирает, ссылка на него невалидна
f->x_ = 10; // UB
}
template <typename T> struct Vector { ... };
template <typename T> void foo(Vector<T> &); // definition in cpp
template struct Vector<Point2d>;
template struct Vector<Point3d>;
template void foo<Point2d>(Vector<Point2d> &);
template void foo<Point3d>(Vector<Point3d> &);
Vector<Point2d>
и Vector<Point3d>
будут экпортированы. class car {
public :
void Start () { ... }
void Accelerate() { ... }
void Brake() { ... }
void setYear(int year) { this->year = year; }
int getYear() { return year; }
private :
int year;
char model [255];
};
in.seekg (ios::end);
in.seekg (-1, ios::cur);
std::vector<char> cts;
in.seekg(0, in.end);
cts.resize(in.tellg());
in.seekg(0, in.beg);
in.read(&ctr[0], ctr.size());
std::ostream_iterator<char> out_it(out);
std::copy(cts.rbegin(), cts.rend(), out);