c++11

why sort function of STL is not working?

我只是一个虾纸丫 提交于 2021-02-10 12:40:02
问题 A set of numbers will be passed as input. Also the redefined relationship of the digits 0-9 in ascending order will be passed as input. Based on the redefined relationship, the set of numbers must be listed in ascending order. Sample I/O Input: 20 50 11 121 9231476058 Output: 50 11 20 121 The program which i wrote having an error about which i am not aware of. so, please help me in debugging it. Program specification: 1.) I created one adjacency list for grouping the numbers on the basis of

Access a type in a variadic template by index

柔情痞子 提交于 2021-02-10 11:47:38
问题 I would like to obtain a type in a variadic template by index. The index is specified as a template argument. I managed to find a 'hack' that works, but I believe that it is not in the spirit of variadic template programming. Besides, it uses extra memory. Here is the code with some explanations: template <typename... InputPortTypes> class PipelineReceiver { protected: // This tuple is used for storing types only // Hence, I would like to get rid of it, but I am not sure how. std::tuple< std:

Access a type in a variadic template by index

巧了我就是萌 提交于 2021-02-10 11:45:09
问题 I would like to obtain a type in a variadic template by index. The index is specified as a template argument. I managed to find a 'hack' that works, but I believe that it is not in the spirit of variadic template programming. Besides, it uses extra memory. Here is the code with some explanations: template <typename... InputPortTypes> class PipelineReceiver { protected: // This tuple is used for storing types only // Hence, I would like to get rid of it, but I am not sure how. std::tuple< std:

Container that does not invalidate iterators (and pointers)

假如想象 提交于 2021-02-10 08:50:31
问题 I am currently looking for container that provides some inserting (insert or push_back) and some removing (erase, pop_back is not sufficient) methods, and that does not invalidate iterators nor pointers when calling these two methods. More clearly, I want a set of elements where I can add an element (I do not care where), and where I can remove any element (so I do care where). In addition, I would have external pointers to specific elements, and I want them to remain valid if I add or remove

Transform tuple to “triangular” tuple

和自甴很熟 提交于 2021-02-10 06:54:31
问题 How do I transform this type: std::tuple<T0, T1, ..., TN1, TN> into this: std::tuple< std::function<T0()>, std::function<T1(T0)>, std::function<T2(T0, T1)>, ... std::function<TN(T0, ..., TN1 )> > 回答1: Right ... isn't suffice, but you could always use pattern matching (i.e. partial specialization) with recursion: #include <tuple> #include <functional> #include <cstdlib> // A type to store list of integers template <size_t... ns> struct integers { template <size_t n> using push_back = integers

Thrust reduce with tuple accumulator

耗尽温柔 提交于 2021-02-10 06:18:27
问题 I want to use thrust::reduce on a thrust::host_vector of thrust::tuple<double,double> . Because there is no predefined thrust::plus<thrust::tuple<double,double>> I wrote my own and used the variant of thrust::reduce with four arguments. Since I'm a good citizen I put my custom version of plus in my own namespace where I left the primary template simply undefined and specialized it for thrust::tuple<T...> . #include <iostream> #include <tuple> #include <thrust/host_vector.h> #include <thrust

constexpr literal initialization with a constexpr

会有一股神秘感。 提交于 2021-02-10 06:15:17
问题 simple example here: static constexpr const char literal1[] = "abcde"; static constexpr const char literal2[] = literal1; compilation error. How to make it work and why it doesn't? 回答1: update: In response to comment, here's a revised verson. The class immutable::string models a constexpr string-like object which tracks the original string literal. It's very similar to c++17's string_view except that the template constructor avoids the need for a call to strlen at any time. #include <cstdint>

Constexpr Factorial Compilation Results in VS2015 and GCC 5.4.0

感情迁移 提交于 2021-02-10 05:58:45
问题 Wondering if the following surprises anyone, as it did me? Alex Allain's article here on using constexpr shows the following factorial example: constexpr factorial (int n) { return n > 0 ? n * factorial( n - 1 ) : 1; } And states: Now you can use factorial(2) and when the compiler sees it, it can optimize away the call and make the calculation entirely at compile time. I tried this in VS2015 in Release mode with full optimizations on (/Ox) and stepped through the code in the debugger viewing

Code using SFINAE working with GCC but not with Clang

拈花ヽ惹草 提交于 2021-02-10 05:12:52
问题 I'm trying to use SFINAE in C++11 to implement a serialization library. My code works fine with GCC but not with Clang. I've reduced it here to a minimal code: template <typename A, typename T> constexpr auto has_save_method(A& ar, T& t) -> decltype(t.save(ar), bool()) { return true; } template<class A, typename T, bool has_save> struct saver; template<class A, typename T> struct saver<A,T,true> { static void apply(A& ar, T& t) { t.save(ar); } }; class MyClass { public: template<typename A>

asio::io_service is ending immediately with work

亡梦爱人 提交于 2021-02-10 05:12:38
问题 I'm trying to learn io_service and work with shared pointers. I want the code to work infinitely until I call stop method or sth like this. Unfortunately after seeing workHandler's output on the screen the program shutdowns. Can anybody explain why this happen? #include <boost/asio.hpp> #include <iostream> #include <atomic> #include <memory> #include <thread> #include <vector> class Service : public std::enable_shared_from_this<Service> { std::shared_ptr<boost::asio::io_service> _service; std