std

Is there a standard library equivalent of timeGetTime()?

落爺英雄遲暮 提交于 2020-01-16 13:16:21
问题 I've tried search without really finding an answer to this question that meets me requirements or explains it clearly enough. I'm looking for a function or a way to implement a function that can retrieve the number of ticks or milliseconds in the same way that the timeGetTime() function does in windows. I'm looking for a solution that only uses standard C++, no additional libraries or platform specifics (like timeGetTime() on windows or a linux equivalent; a multi-platform solution). I'm

Std::bind on a vector of std::functions compiles in VC10, but not VC11. What did I do wrong?

走远了吗. 提交于 2020-01-15 15:33:14
问题 When moving from VC10 to VC11, the following code fails to compile. #include <algorithm> #include <functional> #include <vector> using namespace std; using namespace std::placeholders; typedef std::function<void(int)> CB; int main() { vector<CB> m_CBs; int m_LongPressGesture; for_each(m_CBs.begin(), m_CBs.end(), bind(&CB::operator(), _1, m_LongPressGesture)); return 0; } The error output is illegal indirection. Here's the complete output. 1>------ Build started: Project: CompileError,

std::map, custom key type with sorting only on one variable

萝らか妹 提交于 2020-01-15 11:18:30
问题 I have a key type: struct KeyT { uint32_t timestamp; // example! uint32_t a; uint32_t b; uint32_t c; uint32_t d; uint32_t e; // ... bool operator== (const KeyT& key) const { if(timestamp == key.timestamp && a == key.a && b == key.b && d == key.d && c == key.c && e == key.e) return true; return false; } bool operator< (const KeyT& key) const { if(timestamp < key.timestamp) return true; else if(timestamp == key.timestamp && a < key.a && b < key.b && c < key.c && d < key.d && e < key.e) return

Difference between “destroy” “destructor” “deallocate” in std::allocator?

China☆狼群 提交于 2020-01-14 09:09:07
问题 In C++ std::allocator , there are three methods relating to a common concept: deallocate destroy destructor I want to know: How are they different from each other from the memory management perspective? when should I use this but not that? Thank you! Edit: More specific doubts: I am sorry to generalize it at first, here are some points I don't understand. What does destructor do? The documentation didn't talk about whether the memory will be released automatically when the destructor is

sorting std::lists using std::sort [duplicate]

六月ゝ 毕业季﹏ 提交于 2020-01-13 07:52:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Sort list using stl sort function why only std::list::sort()? My question is can we sort two std::lists using std::sort function? I have 2 string lists std::list<std::string>list1, list2; .....//entering values to list std::sort(list1.begin(), list1.end()); std::sort(list2.begin(), list2.end()); while i am sorting these lists i am getting error. I tried with std::vector, at this time the sort works. The error is

Why move constructor is called twice when passing temporaries to thread function?

元气小坏坏 提交于 2020-01-12 09:18:26
问题 In below code I could not understand why move constructor of class is called twice considering that my thread function is taking argument by rvalue reference and so I was hoping move constructor will be called only once when arguments will be moved to thread constructor.Can somebody give insights on how thread constructor works and how it passes argument to thread function. #include <iostream> #include <thread> #include <chrono> class Test { public: Test() {} Test(Test&&) { std::cout<<"Move

How is std::tuple implemented?

冷暖自知 提交于 2020-01-11 15:47:13
问题 I'd like to know how are tuple implemented in standard library for C++0x. I tried to read description in libstdc++ manual and then read template listing, but it's really hard to understand how it works, especially when reading code. Can someone explain me in few sentences the idea of tuple implementation? I want to know this, because I thinking about using tuples in my code and i want to understand how it works and what type of overhead does it brings (extends compile time only, perform many

No member named 'begin' in namespace 'std'

。_饼干妹妹 提交于 2020-01-11 09:25:30
问题 I successfully compiled on Windows a code that should be crossplatform. Now when compiling it in Xcode with Mac OS X, I get: std::valarray<float> v(32); ... std::sort(begin(v), end(v)); # Use of undeclared identifier 'begin' std::sort(std::begin(v), std::end(v)); # No member named 'begin' in namespace 'std' std::sort(std::valarray::begin(v), std::valarray::end(v)); # Idem, error as well Why does the error No member named 'begin' in namespace 'std' happen? 回答1: std::begin was introduced with C

How to preallocate(reserve) a priority_queue<vector>?

若如初见. 提交于 2020-01-11 00:06:57
问题 How can I preallocate a std::priority_queue with a container of type std::vector ? std::priority_queue<unsigned char, std::vector<unsigned char>> pq; pq.c.reserve(1024); Does not compile because the underlying vector is a protected member. Is it possible to use the constructor of the priority_queue to wrap it around a pre-reserved vector? 回答1: Yes, there's a constructor for that. It's slightly tedious that you also have to specify a comparator: std::vector<unsigned char> container; container

Why is std::basic_string::operator[] a const method if it's also a non-const method?

我们两清 提交于 2020-01-07 02:44:17
问题 http://cplusplus.com/reference/string/basic_string/operator[] I understand that it's advantageous to have a second version which returns const to prevent warnings when a const result is required and to mitigate casting but if the function already provides a non- const method (method-- not result) then what is the point of declaring the const -result method const ? 回答1: You need to understand, that the second ( const ) version not only returns a different result but is also marked itself as