Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <chrono>
- #include <map>
- using namespace std;
- using namespace std::chrono;
- struct Person
- {
- string name;
- string surname;
- int age;
- };
- vector <Person> GetMoscowPopulation();
- //Bad
- //В C++ параметры в функцию передаются по значению (полная глубокая копия)
- //void PrintPopulationSize(vector <Person> p)
- //{
- // cout << "There are " << p.size() <<
- // " people in Moscow" << endl;
- //}
- //Good
- void PrintPopulationSize(const vector <Person>& p)
- {
- cout << "There are " << p.size() <<
- " people in Moscow" << endl;
- }
- int main()
- {
- /*vector <Person> staff;
- staff.push_back({ "Ivan", "Ivanov", 25 });*/
- auto start = steady_clock::now();
- vector <Person> people = GetMoscowPopulation();
- auto finish = steady_clock::now();
- cout << "GetMoscowPopulation "
- << duration_cast<milliseconds>(finish - start).count() << "ms" << endl;
- start = steady_clock::now();
- PrintPopulationSize(people);
- //PrintPopulationSize(GetMoscowPopulation());
- finish = steady_clock::now();
- cout << "PrintPopulationSize "
- << duration_cast<milliseconds>(finish - start).count() << "ms" << endl;
- return 0;
- }
- **************************************************************************************************************
- 1. В С++ параметры в функцию передаются по значению (полная глубокая копия)
- 2. Не можем передать результат вызова одной функции в другую, если она принимает его ПРОСТО по ССЫЛКЕ!
- (По константной ссылке можно принимать результат вызова других функций)
- 3. При передаче по константной ссылке не происходит лишнего копирования
- 4. К.С защищает от случайного изменения объекта
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement