c++11

best way to inherit from a class without virtual functions

馋奶兔 提交于 2021-02-11 04:59:00
问题 I am trying to implement some event handling. There are different types of events: integerChangedEvent, boolChangedEvent, stringChangedEvent and so on... Each of these events hold some of the same information like: std::string settingsName, std::string containerName... But also each of these different event types also hold some information which are unique for this event types: e.g. int double std::string... newValue and oldValue. My idea to not copy the same code thousands of times is to

Skip earlier implicit template parameter, but not later, in a template parameter list

ぃ、小莉子 提交于 2021-02-10 20:14:01
问题 I am just curious: On the following sample I show what I mean in place of /*implicit*/ . Is there a workaround to leave it blank? typename T cannot be first as you can see. template<typename C1, typename C2, typename T = decltype(typename C1::value_type() * typename C2::value_type())> T dot(const C1 &v1, const C2 &v2); int main() { std::vector<float> vec1; std::vector<double> vec2; // typical: auto result1 = dot(vec1, vec2); // auto -> double // avoid numerical unstable situations: auto

Skip earlier implicit template parameter, but not later, in a template parameter list

寵の児 提交于 2021-02-10 20:11:03
问题 I am just curious: On the following sample I show what I mean in place of /*implicit*/ . Is there a workaround to leave it blank? typename T cannot be first as you can see. template<typename C1, typename C2, typename T = decltype(typename C1::value_type() * typename C2::value_type())> T dot(const C1 &v1, const C2 &v2); int main() { std::vector<float> vec1; std::vector<double> vec2; // typical: auto result1 = dot(vec1, vec2); // auto -> double // avoid numerical unstable situations: auto

BOOST_SPIRIT_DEFINE not understand

我们两清 提交于 2021-02-10 18:21:41
问题 I'm trying to write an expression parser with boost spirit x3. I based my new code on old code that I written years ago (and worked well) with Spirit 2.x (qi). The core of my code is: //Make new rule(s) for expression auto term = factor >> *(('*' >> factor) | ('/' >> factor)); auto expression = term >> *(('+' >> term) | ('-' >> term)); auto group = '(' >> expression >> ')'; auto factor = lexeme["double_"] | group; string s="12.4 + 3.2"; auto first = s.begin(); auto last = s.end(); bool r = x3

BOOST_SPIRIT_DEFINE not understand

戏子无情 提交于 2021-02-10 18:20:42
问题 I'm trying to write an expression parser with boost spirit x3. I based my new code on old code that I written years ago (and worked well) with Spirit 2.x (qi). The core of my code is: //Make new rule(s) for expression auto term = factor >> *(('*' >> factor) | ('/' >> factor)); auto expression = term >> *(('+' >> term) | ('-' >> term)); auto group = '(' >> expression >> ')'; auto factor = lexeme["double_"] | group; string s="12.4 + 3.2"; auto first = s.begin(); auto last = s.end(); bool r = x3

How do I make a functor out of an arbitrary function?

为君一笑 提交于 2021-02-10 16:21:16
问题 I have a bunch of functions which I want to use as functors (that is, use types instead of pass pointers to the functions, or any other kind of data). Is there an elegant/idiomatic/standard way of doing this with the standard library, or the standard library + Boost? Perhaps using bind() somehow? Or should I go with something simplistic (well, kind of simplistic) such as: template<typename Function, Function& F, typename... Parameters> struct functor { using function_type = Function; using

Saving function evaluations while using std::min_element()

限于喜欢 提交于 2021-02-10 15:38:38
问题 Suppose you are given a vector of 2D points and are expected to find the point with the least Euclidean norm. The points are provided as std::vector<point_t> points whith the following typedef std::pair<double, double> point_t . The norm can be calculated using double norm(point_t p) { return pow(p.first, 2) + pow(p.second, 2); } Writing the loop myself I would do the following: auto leastPoint = points.cend(); auto leastNorm = std::numeric_limits<double>::max(); for (auto iter = points

How to print vector <vector<int>> in c++? [duplicate]

蓝咒 提交于 2021-02-10 14:25:41
问题 This question already has answers here : Print a vector of vectors (2 answers) Closed 5 months ago . I am trying to assign and print vector<vector<int>> dynamically , However i cannot figure out how to do this i am stuck here is my program #include<iostream> #include<vector> #include<string> using namespace std; class A { public: vector<int> getVector(int s) { vector <int> A(s); for (int j = 0; j < s; j++) { A.push_back(j); } return A; } }; int main() { A obj; int n = 5; vector<vector<int>> A

How can I create a normal integer distribution in C++11?

你。 提交于 2021-02-10 13:07:40
问题 The normal distribution: std::normal_distribution<T> only accepts a real-valued type such as float or double , why doesn't it accept an integer type? How can I create a normal integer distribution? 回答1: I know next to nothing about C++11 but I know a little math (or I did at one point) and a discrete normal distribution is called a Binomial distribution. In fact a normal distribution is the binomial distribution when you let n go to infinity. So assuming C++11 has a binomial distribution then

why sort function of STL is not working?

十年热恋 提交于 2021-02-10 12:41:03
问题 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