std

Why does std::min only support initializer_list?

二次信任 提交于 2021-01-20 09:15:02
问题 We can use std::min in below way: // 1. int a = 1, b = 2; std::min(a, b); // 2. std::min({1,2,3,4}); But why can't use a std::vector or std::list , Because the param in the template is initializer_list . template <class T, class Compare> pair<T,T> minmax (initializer_list<T> il, Compare comp); What is the reason for this design? 回答1: To explain "why it doesn't accept a container", take the semantic into consideration: std::min({ "foo", "bar", "hello" }) The semantic of std::min() means "find

C++ integer type twice the width of a given type

爷,独闯天下 提交于 2021-01-17 23:41:21
问题 In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t : typedef int_least32_t coord_t; coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){ coord_squared_t _x=x; coord_squared_t _y=y; return _x*_x+_y*_y; } What could be used to express coord_squared_t in terms of coord_t ? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead

C++ integer type twice the width of a given type

寵の児 提交于 2021-01-17 23:37:10
问题 In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t : typedef int_least32_t coord_t; coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){ coord_squared_t _x=x; coord_squared_t _y=y; return _x*_x+_y*_y; } What could be used to express coord_squared_t in terms of coord_t ? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead

C++ integer type twice the width of a given type

不羁岁月 提交于 2021-01-17 23:35:02
问题 In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t : typedef int_least32_t coord_t; coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){ coord_squared_t _x=x; coord_squared_t _y=y; return _x*_x+_y*_y; } What could be used to express coord_squared_t in terms of coord_t ? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead

C++ integer type twice the width of a given type

你离开我真会死。 提交于 2021-01-17 23:34:18
问题 In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t : typedef int_least32_t coord_t; coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){ coord_squared_t _x=x; coord_squared_t _y=y; return _x*_x+_y*_y; } What could be used to express coord_squared_t in terms of coord_t ? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead

C++ integer type twice the width of a given type

喜欢而已 提交于 2021-01-17 23:34:16
问题 In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t : typedef int_least32_t coord_t; coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){ coord_squared_t _x=x; coord_squared_t _y=y; return _x*_x+_y*_y; } What could be used to express coord_squared_t in terms of coord_t ? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead

C++ integer type twice the width of a given type

隐身守侯 提交于 2021-01-17 23:32:19
问题 In this example, coord_squared_t is the alias for an integer type with at least twice the size of the integer type coord_t : typedef int_least32_t coord_t; coord_squared_t CalculateSquaredHypothenuse(coord_t x, coord_t y){ coord_squared_t _x=x; coord_squared_t _y=y; return _x*_x+_y*_y; } What could be used to express coord_squared_t in terms of coord_t ? Is there anything in the standard library that allows me to do something like double_width<coord_t>::type to get the correct width, instead

c++ max_element every n element

百般思念 提交于 2021-01-04 06:00:48
问题 Is there a way to find the max element in a container comparing every N elements and return the index. Using STL, BOOST, or... another lib? with every N, I mean use the std::max_element, but change the increase in the for, from ++first, to first += n; // based on std::max_element #ifndef NEWTON_ALGORITHM_SEARCH_MAX_INDEX_HPP #define NEWTON_ALGORITHM_SEARCH_MAX_INDEX_HPP #include <iterator> #include <newton/functional.hpp> namespace newton { // SAME THAT STD::MAX_ELEMENT template<class

Using `std::copy()` with `std::back_inserter()`

天涯浪子 提交于 2021-01-02 05:34:49
问题 I have two class A and B both have a member like below: class A { ... std::vector<std::vector<std::vector<size_t>>> grid; } class B { ... std::vector<std::vector<std::vector<size_t>>> grid; } I found when I use std::copy() to copy from A::grid to B::grid , it will fail. Here is what I do: // Here is in B's constructor. // I initialize B::grid with the same size of A::grid grid = vector<vector<vector<size_t>>>(GetSetting().grid_cols()); for (int i = 0; i < GetSetting().grid_cols(); i++) { grid

Transferring the ownership of object from one unique_ptr to another unique_ptr in C++11?

▼魔方 西西 提交于 2020-12-27 07:43:49
问题 In C++11 we can transfer the ownership of an object to another unique_ptr using std::move() . After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr. std::unique_ptr<int> p1(new int(42)); std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership What are the situations where this will be useful as it is transferring the ownership to another unique_ptr ? 回答1: The following situations involve transferring ownership from one unique