std

How can I insert element into beginning of vector?

喜夏-厌秋 提交于 2020-03-14 06:55:31
问题 i'm newbie in writing code and i need a little help.I need to insert values into the beginning of a std::vector and i need other values in this vector to be pushed to further positions for example: something added to beginning of a vector and values moved from position 1 to 2, from 2 to 3 etc. how do i do that? 回答1: Use std::vector::insert function accepting iterator to the first element as a target position (iterator before which to insert the element): #include <vector> int main() { std:

std::set custom comparator for 2D points

眉间皱痕 提交于 2020-03-02 14:52:37
问题 I need a list of non-duplicated 2D points, so I use a std::set with a custom comparison function. The function I use has problems after inserting points because sometimes the std::find does not find the already inserted points. const double tolerance = 0.1; struct MyPoint2D { MyPoint2D(double x, double y) : _x(x), _y(y) {} double _x, _y; }; auto compMyPoint2D = [&](const MyPoint2D& pointA, const MyPoint2D& pointB) -> bool { if (pointA._x < pointB._x - tolerance) return true; if (pointA._x >

std::set custom comparator for 2D points

青春壹個敷衍的年華 提交于 2020-03-02 14:41:43
问题 I need a list of non-duplicated 2D points, so I use a std::set with a custom comparison function. The function I use has problems after inserting points because sometimes the std::find does not find the already inserted points. const double tolerance = 0.1; struct MyPoint2D { MyPoint2D(double x, double y) : _x(x), _y(y) {} double _x, _y; }; auto compMyPoint2D = [&](const MyPoint2D& pointA, const MyPoint2D& pointB) -> bool { if (pointA._x < pointB._x - tolerance) return true; if (pointA._x >

A question regarding the implementation of std::add_pointer

巧了我就是萌 提交于 2020-02-28 07:53:27
问题 From std::add_pointer Possible implementation namespace detail { template <class T> struct type_identity { using type = T; }; // or use std::type_identity (since C++20) template <class T> auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>; template <class T> auto try_add_pointer(...) -> type_identity<T>; } // namespace detail template <class T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {}; The description for the above (possible)

Prevent std::move on object?

假装没事ソ 提交于 2020-02-22 07:16:22
问题 I'm trying to create a not-null unique_ptr . template <typename T> class unique_ref { public: template <class... Types> unique_ref(Types&&... Args) { mPtr = std::make_unique<T, Types...>(std::forward<Types>(Args)...); } T* release() && { return mPtr.release(); } T* release() & = delete; private: std::unique_ptr<T> mPtr; }; My goal is to allow release() only if the unique_ref is a temporary. The problem is someone could use std::move() to "get around" this: unique_ref<int> p; int* p2 = std:

Prevent std::move on object?

99封情书 提交于 2020-02-22 07:15:10
问题 I'm trying to create a not-null unique_ptr . template <typename T> class unique_ref { public: template <class... Types> unique_ref(Types&&... Args) { mPtr = std::make_unique<T, Types...>(std::forward<Types>(Args)...); } T* release() && { return mPtr.release(); } T* release() & = delete; private: std::unique_ptr<T> mPtr; }; My goal is to allow release() only if the unique_ref is a temporary. The problem is someone could use std::move() to "get around" this: unique_ref<int> p; int* p2 = std:

Prevent std::move on object?

守給你的承諾、 提交于 2020-02-22 07:13:27
问题 I'm trying to create a not-null unique_ptr . template <typename T> class unique_ref { public: template <class... Types> unique_ref(Types&&... Args) { mPtr = std::make_unique<T, Types...>(std::forward<Types>(Args)...); } T* release() && { return mPtr.release(); } T* release() & = delete; private: std::unique_ptr<T> mPtr; }; My goal is to allow release() only if the unique_ref is a temporary. The problem is someone could use std::move() to "get around" this: unique_ref<int> p; int* p2 = std:

Prevent std::move on object?

↘锁芯ラ 提交于 2020-02-22 07:12:38
问题 I'm trying to create a not-null unique_ptr . template <typename T> class unique_ref { public: template <class... Types> unique_ref(Types&&... Args) { mPtr = std::make_unique<T, Types...>(std::forward<Types>(Args)...); } T* release() && { return mPtr.release(); } T* release() & = delete; private: std::unique_ptr<T> mPtr; }; My goal is to allow release() only if the unique_ref is a temporary. The problem is someone could use std::move() to "get around" this: unique_ref<int> p; int* p2 = std:

Prevent std::move on object?

拟墨画扇 提交于 2020-02-22 07:10:03
问题 I'm trying to create a not-null unique_ptr . template <typename T> class unique_ref { public: template <class... Types> unique_ref(Types&&... Args) { mPtr = std::make_unique<T, Types...>(std::forward<Types>(Args)...); } T* release() && { return mPtr.release(); } T* release() & = delete; private: std::unique_ptr<T> mPtr; }; My goal is to allow release() only if the unique_ref is a temporary. The problem is someone could use std::move() to "get around" this: unique_ref<int> p; int* p2 = std:

Make custom type “tie-able” (compatible with std::tie)

北慕城南 提交于 2020-01-30 13:58:26
问题 Consider I have a custom type (which I can extend): struct Foo { int a; string b; }; How can I make an instance of this object assignable to a std::tie , i.e. std::tuple of references? Foo foo = ...; int a; string b; std::tie(a, b) = foo; Failed attempts: Overloading the assignment operator for tuple<int&,string&> = Foo is not possible, since assignment operator is one of the binary operators which have to be members of the left hand side object. So I tried to solve this by implementing a