c++-standard-library

Should std::list::size have constant complexity in C++11?

ε祈祈猫儿з 提交于 2019-12-17 20:43:54
问题 I am using gcc 4.8.1 and after hours of debugging a horrible mysterious performance issue I found out that the std::list::size is actually implemented as a call to std::distance . /** Returns the number of elements in the %list. */ size_type size() const _GLIBCXX_NOEXCEPT { return std::distance(begin(), end()); } This surprised me, since the reference says that the complexity of std::list::size should be constant and the complexity of std::distance is linear for std::list::iterator . I am

Is there a use case for std::function that is not covered by function pointers, or is it just syntactic sugar? [duplicate]

我的梦境 提交于 2019-12-17 18:53:23
问题 This question already has answers here : Why do we use std::function in C++ rather than the original C function pointer? [duplicate] (3 answers) Closed 6 years ago . The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers? 回答1: std::function<> gives you the possibility of encapsulating any type of callable object , which is

Can I take the address of a function defined in standard library?

守給你的承諾、 提交于 2019-12-17 18:34:13
问题 Consider the following code: #include <cctype> #include <functional> #include <iostream> int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*)(int); char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2 std::cout << c << "\n"; } Here, the two calls to std::invoke are labeled for future reference. The expected output is: a Is the expected output guaranteed in C++20? (Note: there are two functions called tolower — one in <cctype> and the other in

std::lexical_cast - is there such a thing?

空扰寡人 提交于 2019-12-17 08:34:26
问题 Does the C++ Standard Library define this function, or do I have to resort to Boost? I searched the web and couldn't find anything except Boost, but I thought I'd better ask here. 回答1: Only partially. C++11 <string> has std::to_string for the built-in types: [n3290: 21.5/7]: string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float

Properly overload operator << in Boost.Log

只谈情不闲聊 提交于 2019-12-12 10:56:54
问题 In the Boost.Log documentation, it is said that Note The library uses basic_formatting_ostream stream type for record formatting, so when customizing attribute value formatting rules the operator<< must use basic_formatting_ostream instead of std::ostream . However, throughout the documentation, all I see is overloading operator << on std::ostream rather than basic_formatting_ostream in the example code. For example, see the overload for the custom type severity_level here. According to my

How to find the depth of each node in std::map?

五迷三道 提交于 2019-12-11 10:08:31
问题 If I construct, my own binary tree, then I can find the depth of each node. The sample code is as follows template<class datatype> void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth) { if ( left ) left->printNodeWithDepth(currentNodeDepth+1); std::cout << value << " and the depth is " << currentNodeDepth << std::endl; if ( right) right->printNodeWithDepth(currentNodeDepth+1); } But wondering, since map is a b-tree, is it possible to write something similar to this for a std:

What are the alternatives to the standard C++ library and boost with a clear implementation? [closed]

£可爱£侵袭症+ 提交于 2019-12-11 09:37:31
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I don't have particular problems with the standard library, the only real issue is that the C++ library is used interfacing the code

Doubly linked, cyclic list in standard library

纵饮孤独 提交于 2019-12-11 08:19:32
问题 I need doubly linked cyclic list, but a cant find it in STL containers (singly linked cyclic list too). It may looks like that: element 0 <-> element 1 <-> ... <-> ... ^ ^ | | v v element n <-> element n-1 <-> element n-2 Can you show me what i missed? Thanks in advance. 回答1: You haven't missed anything. There is no such container in the standard library. 来源: https://stackoverflow.com/questions/21649295/doubly-linked-cyclic-list-in-standard-library

How to remove an object from std::vector?

喜夏-厌秋 提交于 2019-12-11 07:15:59
问题 ALL, Consider following code: class CPlayer { public: CPlayer(bool new) { m_new = new; }; bool IsNewPlayer() { return m_new; } private: bool m_new; }; int main() { std::vector<CPlayer> players_pool; players_pool.push_back( false ); players_pool.push_back( false ); players_pool.push_back( true ); players_pool.push_back( false ); } Now what I'm looking for is to remove the players which has m_new as true. Is it possible to do something like this: players_pool.erase( std::remove( players_pool

Does joining a std::thread flush memory?

送分小仙女□ 提交于 2019-12-11 02:28:39
问题 Consider this example: #include <string> #include <chrono> #include <atomic> #include <thread> #include <iostream> std::string some_variable; void writer_thread() { std::this_thread::sleep_for( std::chrono::seconds( 1 ) ); some_variable = "done"; } int main() { { std::thread w( &writer_thread ); w.join(); } std::cout << some_variable; } Is it necessary for me to add a synchronization mechanism to ensure that some_variable is correctly read from main() ? Said differently: does joining or