std

Function for calculating the mean of an array double[] using accumulate

假装没事ソ 提交于 2019-12-05 03:51:55
It must be the most common function for what everyone has a code snippet somewhere, but I have actually spent no less than 1.5 hour searching for it on SO as well as on other C++ sites and have not found a solution. I would like to calculate the mean of a double array[] using a function . I would like to pass the array to the function as a reference . There are millions of examples where the mean is calculated in a main() loop, but what I am looking for is a function what I can put in an external file and use it any time later. So far, here is my latest version, what gives a compile error:

Creating a shared_ptr of vector in C++ [duplicate]

独自空忆成欢 提交于 2019-12-05 03:29:06
This question already has an answer here: Calling initializer_list constructor via make_unique/make_shared 2 answers In modern C++ we can initialize a vector like this: std::vector<int> v = { 1, 2, 3, 4, 5 }; But if I try creating a smart pointer to a vector, this won't compile: auto v = std::make_shared<std::vector<int>>({ 1, 2, 3, 4, 5 }); Is there any better alternative than resorting to push_back s after creation? auto v = std::make_shared<std::vector<int>>(std::initializer_list<int>{ 1, 2, 3, 4, 5 }); This is working. Looks like compiler cannot eat {} in make_unique params without direct

Check if two std::function are Equal

泄露秘密 提交于 2019-12-05 03:28:43
If I have two std::function s, how can I check whether both hold the same function or not? Additional Information: I have a vector of functions std::vector<std::function<void()>> and before adding another function to the vector I want to check if it already is contained. I dont think that they both can be compared. Here is an example to explain some points on std::function comparison Generally std::function and boost::function are not comparable because they need their stored object be comparable but not all function objects and also lambdas have operator== so std or boost functions have no

Pass std algos predicates by reference in C++

流过昼夜 提交于 2019-12-05 02:43:14
I am trying to remove elements from a std::list and keep some stats of deleted elements. In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code for the predicate: class TestPredicate { private: int limit_; public: int sum; int count; TestPredicate(int limit) : limit_(limit), sum(0), count(0) {} bool operator() (int value) { if (value >= limit_) { sum += value; ++count; // Part where I gather the stats return true; } else return false; } }; And here is the code for the algo: std::list < int

std::addressof - strange implementation

无人久伴 提交于 2019-12-05 02:16:56
So a few days ago i learned about std::addressof. At http://en.cppreference.com/w/cpp/memory/addressof a possible implementation is given: template< class T > T* addressof(T& arg) { return reinterpret_cast<T*>( &const_cast<char&>( reinterpret_cast<const volatile char&>(arg))); } As far i see this can simply be implemented like: template<typename T> T* addressof( T& var ) { return &var; } Why the guys at cppreference chose to implement it with 3 casts? Is there any detail I am missing that is making their implementation better. What is the point in using volatile when all you do is cast? If it

Remove element from std::map based on the time of insertion

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:45:52
I need to erase elements from an std::map based on the time of insertion (or something else more efficient than that). The map will probably hold thousands of elements and if I store the time and iterate the map to check each elements time, it will probably end up being quite time consuming. Does anyone have any good idea how to erase elements from a std::map when they are getting old? The std::map<> type has no notion of when an element was inserted. It only serves to hold a key / value pair mapping. It also has no notion of insert order so it can't even provide a relative type of insert. To

Destroy std::vector without releasing memory

爷,独闯天下 提交于 2019-12-05 01:20:27
Lets say I have a function to get data into an std vector: void getData(std::vector<int> &toBeFilled) { // Push data into "toBeFilled" } Now I want to send this data to another function, that should free the data when finished: void useData(int* data) { // Do something with the data... delete[] data; } Both functions (getData and useData) are fixed and cannot be changed. This works fine when copying the data once: { std::vector<int> data; getData(data); int *heapData = new int[data.size()]; memcpy(heapData, data.data(), data.size()*sizeof(int)); useData(heapData); data.clear(); } However, this

Explicit copy constructor and std::sort

一笑奈何 提交于 2019-12-05 00:38:56
When sorting a container of objects having an explicit copy ctor I get compiler errors (from g++ 4.8.2 and clang++ 3.4, both in -std=c++11 mode) that I don't understand. I've created a simple example to demonstrate the problem class A { public: explicit A(int i): m_i(i) {}; explicit A(const A& other): m_i(other.m_i) {}; int i() const {return m_i;}; private: int m_i; }; bool is_less(const A& a, const A& b) { return a.i() < b.i(); } int main(int, char*[]) { std::vector<A> objects; objects.push_back(A(3)); objects.push_back(A(5)); objects.push_back(A(-1)); std::cout << is_less(objects[1], objects

Why std:: is not needed when using ispunct() in C++?

旧街凉风 提交于 2019-12-05 00:37:49
#include <iostream> #include <string> #include <cctype> using std::string; using std::cin; using std::cout; using std::endl; int main() { string s("Hello World!!!"); decltype(s.size()) punct_cnt = 0; for (auto c : s) if (ispunct(c)) ++punct_cnt; cout << punct_cnt << " punctuation characters in " << s << endl; } It seems that I can use ispunct() without std:: or declaring using std::ispunct; but I can't do that with std::cout or std::cin . Why is this happening? It means ispunct is part of the global namespace, rather than the std namespace. This is probably because ispunct is one of the

assigning true/false to std::string: what's going on?

六眼飞鱼酱① 提交于 2019-12-04 23:12:30
I was testing a c++11 compiler on my source code and it caught an error in one of my functions that I would have expected my non c++11 compiler to catch as well. I was returning false from a function that has a return type of std::string... Here's the code that demonstrates the problem #include <iostream> int main ( ) { std::string str = false; std::cerr << "'" << str << "'" << std::endl; return 0; } $ g++ test.cpp -W -Wall -Wextra $ ./a.out terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid Aborted I'm very surprised that this