std

Why doesn't std::noskipws work, or what is it supposed to do?

断了今生、忘了曾经 提交于 2019-12-29 08:32:13
问题 First off my understanding is that cin >> std::noskipws >> str; should stick a whole line from cin like "i have spaces" into str . However this only puts "i" into str . This could be a false assumption in which case what does std::noskipws do? I know there is a function std::getline and that does work but simply for educational purposes I decided I would try to get std::noskipws to work for me. I have tried in the past and it just never works so I normally move on and use std::getline . What

Concatenate boost::dynamic_bitset or std::bitset

喜你入骨 提交于 2019-12-29 06:19:18
问题 what is the best way to concatenate 2 bitsets? For example i've got boost::dynamic_bitset<> test1( std::string("1111") ); boost::dynamic_bitset<> test2( std::string("00") ); they should be concatenated into a thrid Bitset test3 which then holds 111100 Solutions should use boost::dynamic_bitset. If the solution works with std::bitset, it would be nice too. There should be a focus on performance when concatenating the bits. UPDATE: I've compared both methods (stringmethod from me and Neil and

Concatenate boost::dynamic_bitset or std::bitset

China☆狼群 提交于 2019-12-29 06:19:16
问题 what is the best way to concatenate 2 bitsets? For example i've got boost::dynamic_bitset<> test1( std::string("1111") ); boost::dynamic_bitset<> test2( std::string("00") ); they should be concatenated into a thrid Bitset test3 which then holds 111100 Solutions should use boost::dynamic_bitset. If the solution works with std::bitset, it would be nice too. There should be a focus on performance when concatenating the bits. UPDATE: I've compared both methods (stringmethod from me and Neil and

C++ const std::map reference fails to compile

故事扮演 提交于 2019-12-29 05:50:12
问题 Is there a reason why passing a reference to a std::map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const: error: no match for ‘operator[]’ in ‘map[name]’ Here's the function prototype: void func(const char ch, std::string &str, const std::map<std::string, std::string> &map); And, I should mention that there is no problem when I remove the const keyword in front of std::map . If I've been instructed correctly, the [] operator will actually insert a

map with incomplete value type

妖精的绣舞 提交于 2019-12-29 01:37:30
问题 I'm getting an error with the following: class Test { std::map<std::string,Test> test; }; The error is "Field has incomplete type 'Test'". I read a few threads with suggested this might be a bug in the version of libcxx which ships with xcode, but it wouldn't surprise me at all if I just have to change it to: class Test { std::map<std::string,std::shared_ptr<Test>> test; }; I just wanted to double check that this is definitely a correct error and not a bug. Cheers! 回答1: The standard requires

Using numeric_limits::max() in constant expressions

折月煮酒 提交于 2019-12-28 16:30:54
问题 I would like to define inside a class a constant which value is the maximum possible int. Something like this: class A { ... static const int ERROR_VALUE = std::numeric_limits<int>::max(); ... } This declaration fails to compile with the following message: numeric.cpp:8: error: 'std::numeric_limits::max()' cannot appear in a constant-expression numeric.cpp:8: error: a function call cannot appear in a constant-expression I understand why this doesn't work, but two things look weird to me: It

Why is there no std::allocate_unique function in C++14?

雨燕双飞 提交于 2019-12-28 11:46:09
问题 Why does shared_ptr have allocate_shared while unique_ptr does not have allocate_unique? I would like to make a unique_ptr using my own allocator: do I have to allocate the buffer myself and then assign it to a unique_ptr? This seems like an obvious idiom. 回答1: Why does shared_ptr have allocate_shared while unique_ptr does not have allocate_unique ? shared_ptr needs it so that it can allocate its internal shared state (the reference count and deleter), as well as the shared object, using the

How to reset std::cin when using it?

半世苍凉 提交于 2019-12-28 06:52:05
问题 I have some problem with following code. I use it in Xcode (OS X). [removed my first try of that code] How to input reset std::cin? I try to input another values but I can't because std::cin seems to not work anymore after my wrong value. UPD2 In my second try I use this code: for ( unsigned char i = 0; i < 5; i++ ) { int value = 0; std::cout << "\n>> Enter \"value\": "; std::cin >> value; if ( std::cin.fail() ) { std::cin.clear(); std::cin.ignore(); std::cout << "Error: It's not integer

C++ standard wording: Does “through all iterators in the range” imply sequentiality?

女生的网名这么多〃 提交于 2019-12-28 06:18:30
问题 This SO question sparked a discussion about std::generate and the guarantees made by the standard. In particular, can you use function objects with internal state and rely on generate(it1, it2, gen) to call gen() , store the result in *it , call gen() again, store in *(it + 1) etc., or can it start at the back, for example? The standard (n3337, §25.3.7/1) says this: Effects: The first algorithm invokes the function object gen and assigns the return value of gen through all the iterators in

Why does unique_ptr take two template parameters when shared_ptr only takes one?

不羁岁月 提交于 2019-12-28 04:54:14
问题 Both unique_ptr and shared_ptr accept a custom destructor to call on the object they own. But in the case of unique_ptr , the destructor is passed as a template parameter of the class , whereas the type of shared_ptr 's custom destructor is to be specified as a template parameter of the constructor . template <class T, class D = default_delete<T>> class unique_ptr { unique_ptr(T*, D&); //simplified ... }; and template<class T> class shared_ptr { template<typename D> shared_ptr(T*, D); /