c++-faq

What are the differences between struct and class in C++?

限于喜欢 提交于 2019-11-25 22:55:00
问题 This question was already asked in the context of C#/.Net. Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I\'ll start with an obvious difference: If you don\'t specify public: or private: , members of a struct are public by default; members of a class are private by default. I\'m sure there are other differences to be found in the obscure corners of the C++

What is a lambda expression in C++11?

牧云@^-^@ 提交于 2019-11-25 22:54:06
问题 What is a lambda expression in C++11? When would I use one? What class of problem do they solve that wasn\'t possible prior to their introduction? A few examples, and use cases would be useful. 回答1: The problem C++ includes useful generic functions like std::for_each and std::transform , which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function. #include <algorithm> #include <vector

Why should I not #include <bits/stdc++.h>?

帅比萌擦擦* 提交于 2019-11-25 22:51:45
问题 I posted a question with my code whose only #include directive was the following: #include <bits/stdc++.h> My teacher told me to do this, but in the comments section I was informed that I shouldn\'t. Why? 回答1: Including <bits/stdc++.h> appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year. I imagine the advantages are vaguely given thus: You only need write one #include line You do not need to

What is the difference between a definition and a declaration?

徘徊边缘 提交于 2019-11-25 22:51:37
问题 The meaning of both eludes me. 回答1: A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations: extern int bar; extern int g(int, int); double f(int, double); // extern can be omitted for function declarations class foo; // no extern allowed for type declarations A definition actually instantiates/implements this identifier. It's what the linker needs

Iterator invalidation rules

和自甴很熟 提交于 2019-11-25 22:51:22
问题 What are the iterator invalidation rules for C++ containers? Preferably in a summary list format. (Note: This is meant to be an entry to Stack Overflow\'s C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.

Where and why do I have to put the “template” and “typename” keywords?

独自空忆成欢 提交于 2019-11-25 22:51:01
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

What is The Rule of Three?

99封情书 提交于 2019-11-25 22:50:37
问题 What does copying an object mean? What are the copy constructor and the copy assignment operator ? When do I need to declare them myself? How can I prevent my objects from being copied? 回答1: Introduction C++ treats variables of user-defined types with value semantics . This means that objects are implicitly copied in various contexts, and we should understand what "copying an object" actually means. Let us consider a simple example: class person { std::string name; int age; public: person

Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

别等时光非礼了梦想. 提交于 2019-11-25 22:50:33
问题 I just found a comment in this answer saying that using iostream::eof in a loop condition is \"almost certainly wrong\". I generally use something like while(cin>>n) - which I guess implicitly checks for EOF. Why is checking for eof explicitly using while (!cin.eof()) wrong? How is it different from using scanf(\"...\",...)!=EOF in C (which I often use with no problems)? 回答1: Because iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next

Function with same name but different signature in derived class

℡╲_俬逩灬. 提交于 2019-11-25 22:35:40
问题 I have a function with the same name, but with different signature in a base and derived classes. When I am trying to use the base class\'s function in another class that inherits from the derived, I receive an error. See the following code: class A { public: void foo(string s){}; }; class B : public A { public: int foo(int i){}; }; class C : public B { public: void bar() { string s; foo(s); } }; I receive the following error from the gcc compiler: In member function `void C::bar()\': no

How to convert a number to string and vice versa in C++

一笑奈何 提交于 2019-11-25 22:33:18
问题 Since this question gets asked about every week, this FAQ might help a lot of users. How to convert an integer to a string in C++ how to convert a string into an integer in C++ how to convert a floating-point number to a string in C++ how to convert a string to a floating-point number in C++ 回答1: Update for C++11 As of the C++11 standard, string-to-number conversion and vice-versa are built in into the standard library. All the following functions are present in <string> (as per paragraph 21