std

How to understand C++ std::setw 's inconsistent behaviour?

不问归期 提交于 2019-12-06 11:56:34
Given the following code: /*Formatting Output **Goal: practice using cout to format output to console **Print the variables in three columns: **Ints, Floats, Doubles */ #include <iostream> #include <iomanip> using namespace std; int main() { int a = 45; float b = 45.323; double c = 45.5468; int aa = a + 9; float bb = b + 9; double cc = c + 9; int aaa = aa + 9; float bbb = bb + 9; double ccc = cc + 9; // 1st attempt :> cout << "\n\n\n" << "// 1st attempt :>" << "\n"; cout << "12345678901234567890123456789012345678901234567890" << "\n"; cout << "Ints" << setw(15) << "Floats" << setw(15) <<

std::string getting (char *) instead of (const char *)

∥☆過路亽.° 提交于 2019-12-06 11:46:11
std::string.c_str() returns a (const char *) value. I Googled and found that I can do the following: std::string myString = "Hello World"; char *buf = &myString[0]; How is this possible? &myString[0] is an object of type std::string , so how can this work? &myString[0] is a object of type std::string No it isn't. myString[0] is a reference to the first character of the string; &myString[0] is a pointer to that character. The operator precedence is such that it means &(myString[0]) and not (&mystring)[0] . Beware that, accessed this way, there's no guarantee that the string will be zero

Error handling. Mapping system error codes to generic

时光毁灭记忆、已成空白 提交于 2019-12-06 11:29:37
I've found that function default_error_condition doesn't work as expected in my code auto ec = std::system_category().default_error_condition(EACCES); std::cout << ec.value() << std::endl << ec.category().name() << std::endl; Returned ec value has system error category, but it has to be generic , if I got it right from the documentation e.g. cppreference and gcc source code system_error.cc UPD: also found this remark in the standard 19.5.1.5 Error category objects The object’s default_error_condition virtual function shall behave as follows: If the argument ev corresponds to a POSIX errno

How can I block a Python stdlib module from being imported?

折月煮酒 提交于 2019-12-06 11:04:33
问题 In my Python script, I want to prevent certain stdlib modules, such as os and sys , from being imported. How would I accomplish this? 回答1: Taking you very literally, and if you just mean "to stub them out so that they won't be loaded by a straight import", not "make them unloadable by untrusted code", then: import sys sys.modules['os'] = None sys.modules['system'] = None Of course, there is no module system so you might have meant sys , in which case you're in trouble. If you're trying to

C++ UTF-8 actual string length

倖福魔咒の 提交于 2019-12-06 09:53:38
问题 Is there any native (cross platform) C++ function in any of standard libraries which returns the actual length of std::string ? Update: as we know std::string.length() returns the number of bytes not the number of characters. I already have a custom function which returns the actual one, but I'm looking for an standard one. 回答1: codecvt ought to be helpful, the Standard provides implementations for UTF-8, for example codecvt_utf8<char32_t>() would be appropriate in this case. Probably

How to track memory assign by STL library

五迷三道 提交于 2019-12-06 08:44:56
问题 I want to track all the memory(size allocated by std lib) allocated by all STL containers like map,list,vector etc. I just want to track STL container not regular object creation. Basically want to override new and delete of std lib. Example class demo { public: int i; std::list<int> mylist; } int main() { demo dd = new demo(); // -> Don't want to track this. Just want to track // mylist(size of my list) } I found out that std has it's own allocator option. For example list has it is

Wrong use of std::copy?

泄露秘密 提交于 2019-12-06 07:58:40
问题 Here is a simple test program that illustrates the problem I faced: #include <iostream> #include <stdlib.h> #include <inttypes.h> #include <vector> using namespace std; typedef unsigned char Byte; int main( ) { uint32_t ui32 = 12; size_t sizeofUi32 = sizeof ui32; cout << "sizeofUi32: " << sizeofUi32 << endl; vector<Byte> v(10); std::copy(&ui32, &ui32 + sizeof ui32, &v[4]); uint32_t result = 0; std::copy(&v[4], &v[4] + sizeof ui32, &result); cout << "Result: " << result << " sizeofUi32: " <<

How does a C++ std::container (vector) store its internals (element address, access by index)?

混江龙づ霸主 提交于 2019-12-06 07:37:23
I am trying to "hack" a game (Red Alert 3), I try to make a program which shows the unit list of my opponents. As for that I first need to find a (static) pointer to my own list which I can do on single player. I have noticed this behaviour: (by looking at which addresses are changed by the add_unit code): if a units hasn't been build yet, create a new address for it (random?) and set the value to 1 (amount of units of that type) when the unit has been already build once in the game, increment the original address of the unit type by 1 This looks to me like std::vector behaviour. Now I am

Is std::equal_to guaranteed to call operator== by default?

本小妞迷上赌 提交于 2019-12-06 07:11:49
I had always thought that the standard required the non-specialized template for std::equal_to<T> to call T::operator== , but I noticed the description at cppreference.com almost implies it's the other way around; certainly it doesn't mention it as a requirement. I also checked the C++11 draft standard N3337 and couldn't find any guarantees there either. If you create a class with an operator== you'd hope it would get used in all circumstances. I can't honestly think of a way to implement std::equal_to that wouldn't work this way, but am I missing something? Is std::equal_to guaranteed to call

C++ STL container ::clear ::swap

妖精的绣舞 提交于 2019-12-06 07:10:15
What's the fastest way to "clear" a large STL container? In my application, I need to deal with large size std::map , e.g., 10000 elements. I have tested the following 3 methods to clear a std::map . Create a new container every time I need it. Calling map::clear() method. Calling map::swap() method. It seems that ::swap() gives the best result. Can anyone explain why this is the case, please? Is it safe to say that using map::swap() method is the proper way to "clear" a std::map? Is it the same for other STL containers, e.g., set , vector , list , etc. m_timer_start = boost::posix_time: