std

Is it okay to use std::this_thread* functions from boost::threads?

孤人 提交于 2019-12-22 05:06:09
问题 Is it okay to mix and match things from boost::thread and std::thread , or should one set of functions be used for each? I ask because my code uses boost::thread s, but I've found that boost::this_thread::sleep_for doesn't behave properly when setting the system time back, but std::this_thread::sleep_for does, so I'd like to change my sleep function call and avoid changing all my boost::thread s to std::thread s if possible. 回答1: In practice you might get away with things iff/because the

Searching c++ std vector of structs for struct with matching string

半世苍凉 提交于 2019-12-22 05:05:09
问题 I'm sure I'm making this harder than it needs to be. I have a vector... vector<Joints> mJointsVector; ...comprised of structs patterned after the following: struct Joints { string name; float origUpperLimit; float origLowerLimit; }; I'm trying to search mJointsVector with "std::find" to locate an individual joint by its string name - no luck so far, but the examples from the following have helped, at least conceptually: Vectors, structs and std::find Can anyone point me further in the right

set_difference and set_intersection simultaneously

ぃ、小莉子 提交于 2019-12-22 04:57:13
问题 I'm wondering if there is any facility in the standard library to simultaneously compute the set intersection and set difference between two sorted ranges. Something with a signature along the lines of: template <class Input1, class Input2, class Output1, class Output2, class Output3> Output3 decompose_sets (Input1 first1, Input1 last1, Input2 first2, Input2 last2, Output1 result1, Output2 result2, Output3 result3 ); Such that after a call to decompose sets , result1 contains all the elements

find an item in a list of pointers

末鹿安然 提交于 2019-12-22 04:17:25
问题 I am trying to understand how to find an item in a list of pointers in C++, using std::find If I had for example: std::list<string> words; std::string word_to_be_found; I could just search like this: std::list<string>::iterator matching_iter = std::find(words,begin(), words.end(), word_to_be_found) but what if I have a lsit of pointers? std::list<string *> words; the above syntax will not work anymore. Can I do it some similar way? thanks! 回答1: You can pass a predicate to the std::find_if

std::addressof - strange implementation

試著忘記壹切 提交于 2019-12-22 04:05:25
问题 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

std::addressof - strange implementation

冷暖自知 提交于 2019-12-22 04:05:15
问题 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

What is the difference between Boost smart pointers and std smart pointers?

余生颓废 提交于 2019-12-22 02:05:13
问题 So, I'd like to use smart pointers instead of raw and almost every topic on SO says about Boost library. But std has such things as std::auto_ptr and std::shared_ptr . Why Boost? What is the difference? It was a question not about difference of the implementation, but about reasons to use Boost pointers. I suppose, given answer, including date of answering and context, is reasonably useful. It helps to understand how Boost pointers were added to std. 回答1: Basically Boost did shared_ptr first.

Inspecting contents of std::vector in Eclipse CDT debugger

半城伤御伤魂 提交于 2019-12-22 01:34:13
问题 I'm using Eclipse with the CDT plugin to develop in C++. I'm also using std library to create vectors and I am having an issue while debugging: Eclipse does not allow me to view the content of the vectors. Is there any way to be able to debug it properly? 回答1: Debugging STL containers in Eclipse is not quite straightforward. Please have a look at this question and the answers explaining the reasons for this. The simplest way without fiddling with the GDB for me is this answer, which can be

Why does std::visit take a variable number of variants?

依然范特西╮ 提交于 2019-12-22 01:26:44
问题 Trying to get more familiar with C++17, I've just noticed std::visit: template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple

Is there anything like “std::and” or “std::or”?

元气小坏坏 提交于 2019-12-22 01:24:56
问题 Given a container of boolean values (An example is std::vector<bool> ), is there a standard function that returns true if all the values are true ("and") or true if at least one value is true ("or"), with short circuit evalutation ? I digged trough www.cplusplus.com this morning but couldn't find anything close. 回答1: You can implement by: AND: std::find(vector.begin(), vector.end(), false) == vector.end() // all the values are true OR: std::find(vector.begin(), vector.end(), true) != vector