How to reset std::cin when using it?

You could use, when the condition, std::cin.fail() happens: std::cin.clear(); std::cin.ignore(); And then continue with the loop, with a continue; statement. std::cin.clear() clears the error flags, and sets new ones, and std::cin.ignore() effectively ignores them (by extracting and discarding them). Sources: cin.ignore() cin.clear()

Conversion from boost::shared_ptr to std::shared_ptr?

Based on janm’s response at first I did this: template<class T> std::shared_ptr<T> to_std(const boost::shared_ptr<T> &p) { return std::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } template<class T> boost::shared_ptr<T> to_boost(const std::shared_ptr<T> &p) { return boost::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } But then I realized I could do this instead: namespace { template<class SharedPointer> struct Holder { … Read more

std::lexical_cast – is there such a thing?

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 val); string to_string(double val); string to_string(long double val); Returns: Each function returns a string object holding the character representation … Read more

Trouble reading a line using fscanf()

It’s almost always a bad idea to use the fscanf() function as it can leave your file pointer in an unknown location on failure. I prefer to use fgets() to get each line in and then sscanf() that. You can then continue to examine the line read in as you see fit. Something like: #define … Read more

Why does std::vector work with incomplete types in class definitions?

Standard says (draft N3690; this is post C++11, pre C++14): [res.on.functions] 1 In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++standard library depends on components supplied by a C++program. If these components do not meet their requirements, the Standard places no requirements on the implementation. … Read more

What is the difference between using a struct with two fields and a pair?

std::pair provides pre-written constructors and comparison operators. This also allows them to be stored in containers like std::map without you needing to write, for example, the copy constructor or strict weak ordering via operator < (such as required by std::map). If you don’t write them you can’t make a mistake (remember how strict weak ordering … Read more