学习笔记之IKM C++ 11

 https://github.com/haotang923/interview/tree/master/IKM

  • Q1. If most of the calls to function foo() below pass one of 10 particular values, which method will significantly reduce the execution time of calling the function?
  • A. Replace * with an if-else block testing for the 10 values, assigning r accordingly
  • B. Remove inline
  • C. Delete foo() and move the time-consuming operation to its caller
  • D. Replace * with code performing a table lookup, with the 10 values and corresponding values of r 
  • E. Replace * with swtich
  • A1. D ?
1 inline int foo (int x) {
2   int r;
3 
4   * // time-consuming operation on x, result stored in r
5 
6   return r;    
7 }
View Code

 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 using namespace std;
11 
12 class SomeClass {
13 public:
14     static int data;
15 };
16 
17 /*
18  The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
19  */
20 int SomeClass::data = 0; // Definition or redeclaration of 'data' not allowed inside a function (including the main function)
21 
22 SomeClass   objSome;
23 
24 int main(int argc, char* argv[])
25 {
26     /*
27      0
28      0
29      */
30     cout << SomeClass::data << endl;
31     cout << objSome.data << endl;
32     
33 //    cout << SomeClass<static int>.data << endl;
34 //    cout << objSome->data << endl;
35 //    cout << SomeClass::objSome.data << endl;
36 //    cout << objSome::data << endl;
37 
38 
39     return 0;
40 }
View Code

  • Q3. When must a C++ destructor be declared virtual in a base class? 
  • A. virtual default constructor of base class
  • B. derived class allocates system resources which are released in its destructor
  • C. the destructor of the base class will clean up the derived class data
  • D. implementation for the derived class destructor is not desired
  • E. ensure that a derived class destructor is invoked when an instance is destroyed through a base class pointer
  • A3. E ?

  • Q4. Complete code to perform a categorization of the letter type.
  • stringstream::stringstream - C++ Reference
    • http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <string>
10 #include <vector>
11 #include <sstream>
12 using namespace std;
13 
14 int main(int argc, char* argv[])
15 {
16     enum    letterType {complement, complaint, unknown};
17     
18     string  letter = "happy sad happy sad sad";
19     string  sHappy = "happy";
20     string  sSad = "sad";
21     
22     pair<string, int>   happy = make_pair(sHappy, 0);
23     pair<string, int>   sad = make_pair(sSad, 0);
24     
25     string  word;
26     stringstream    ss(letter);
27     vector<string>  letterWords;
28     while (ss >> word) letterWords.push_back(word);
29     
30     vector<string>::iterator    words;
31     for (words = letterWords.begin(); words < letterWords.end(); words ++) {
32         word = *words;
33         
34         /*
35          Following 2 lines is the answer
36          */
37         if (happy.first.find(word) != string::npos) happy.second ++;
38         if (sad.first.find(word) != string::npos)   sad.second ++;
39     }
40     
41     /*
42      1
43      */
44     if (happy.second > sad.second)  return complement;
45     else if (happy.second < sad.second) return complaint;
46     else    return unknown;
47 }
View Code

  • Q5. Modify code to correctly access the member Driver to compiler error
  • A. Derive a class from Bus. Make the object driver a dedrived class pointer.
  • B. Make the function createDriver() non-static.
  • C. Declare the function useBus() to be a friend of class Bus.
  • D. Change the access level of member Driver to public
  • E. Make the function createDriver() return a Driver& then make the object driver a reference object.
  • A5. CD
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class Bus {
12 protected:
13     class Driver; // forward declarations
14 
15 public:
16     static Driver* createDriver() {
17         return new Driver;
18     }
19 
20 //    friend void useBus();
21     
22 private:
23     int seats;
24 protected:
25     class Driver {
26     public:
27         string  name;
28     };
29 };
30 
31 class DeriveDriver : public Bus {};
32 
33 void useBus()
34 {
35     Bus::Driver*    driver = Bus::createDriver(); // 'Driver' is a protected member of 'Bus'
36 //    DeriveDriver::Driver*    driver = Bus::createDriver(); // 'Driver' is a protected member of 'Bus'
37     
38     driver->name = "DRIVER";
39 }
40 
41 int main(int argc, char* argv[])
42 {
43     return 0;
44 }
View Code

  • Q6. Which are legal calls to the function template?
  • A. double output = power<int, 100>(3.0, 'a');
  • C. double output = power<int, 500>(3.0, 100);
  • A6. AC
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 template <typename T, int duration>
12 double power (double amplitude, int frequency = 1000) { return amplitude * frequency; }
13 
14 int main(int argc, char* argv[])
15 {
16     double output;
17 
18     output = power<int, 100>(3.0, 'a');
19 //    output = power<500>(3.0); // No matching function for call to 'power'
20     output = power<int, 500>(3.0, 100);
21 //    output = power<int>(3.0, 100); // No matching function for call to 'power'
22 //    output = power<int, char>(3.0); // No matching function for call to 'power'
23     output = power<int, 500>(3.0);
24 
25     cout << output << endl;
26     
27     return 0;
28 }
View Code

  • Q7. Which are NOT elements of exception processing in C++?
  • A. Making an exception specification using throw()
  • B. Using dynamic_cast<> on references
  • C. Using assert ()
  • D. Making an exception specification using noexcept
  • E. Throwing an object of class Exception
  • A7. C ?
  • C++ 异常处理 | 菜鸟教程
    • http://www.runoob.com/cplusplus/cpp-exceptions-handling.html
  • bad_cast thrown by dynamic_cast when it fails in a dynamic cast
    • Exceptions - C++ Tutorials
      • http://www.cplusplus.com/doc/tutorial/exceptions/
  • Exceptions and asserts are two distinct mechanisms for detecting run-time errors in a program.
    • https://msdn.microsoft.com/en-us/library/hh279678.aspx

  • Q8. Which are correct declarations for a copy constructor for a class named Gizmo? 
  • D. Gizmo(const Gizmo& v);
  • E. Gizmo(const Gizmo&);
  • A8. DE
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class Gizmo{
12     Gizmo();
13     ~Gizmo();
14     
15     // declaration goes here
16 //    Gizmo(const Gizmo* copy_me) const; // 'const' qualifier is not allowed on a constructor
17     
18 //    Gizmo(const Gizmo&) const; // 'const' qualifier is not allowed on a constructor
19     
20 //    Gizmo(Gizmo& const copy_me); // 'const' qualifier may not be applied to a reference
21     
22     Gizmo(const Gizmo& v);
23     
24     Gizmo(const Gizmo&);
25 };
26 
27 int main(int argc, char* argv[])
28 {
29     return 0;
30 }
View Code 

  • Q9. Which are true about C++ inheritance access specifications?
  • A. Protected inheritance from the base class makes all of the base class members protected in the derived class.
  • B. Dynamic inheritance from the base class allows derived class member's access control to be changed on the fly.
  • C. Public inheritance from the base class makes all of the base class members public in the derived class.
  • D. Private inheritance from the base class makes all of the base class members private in the derived class.
  • E. Private members in the base class are not changed via the inheritance access specifications.
  • A9. E ?

  • Q10. Which correctly describe C++ references?
  • A. A reference may refer to a different object by assignment.
  • B. A reference creates an alias to another object.
  • C. A reference remains valid irrespective of the scope or lifetime of the referenced object.
  • D. The operator -> must be used to access the referenced object.
  • E. A reference must be initialized when it is declared.
  • A10. BE ?

  • Q11. Which C++ code contain problems?
  • A11. DE
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 int getLength() {
12     return 10;
13 }
14 
15 int size = getLength();
16 // char message[size]; // Variable length array declaration not allowed at file scope
17 
18 int main(int argc, char* argv[])
19 {
20     bool ok = true;
21     
22     if (!ok) {
23         cout << "not ok" << endl;
24     }
25 
26     const int size = 10;
27     char message[size];
28 
29     int numerator, denominator;
30 
31     numerator = 10.0;
32     denominator = 3.0;
33     
34     float quotient = numerator / denominator;
35 
36     // signed char scope is -128 ~ 127
37     for (char idx = 0; idx < 250; idx ++) { // Comparison of constant 250 with expression of type 'char' is always true
38         cout << "hello world" << endl;
39     }
40 
41     return 0;
42 }
View Code

  • Q12. Which regarding the C++ 11 operators alignof() and alignas() are correct?
  • A. alignof () is used to obtain the alignment of a specified variable or type.
  • B. alignas () is used to specify custom alignment of variables and user-defined types.
  • C. alignas () returns the size, in bytes, of the expression in parentheses.
  • D. alignof () returns the size, in bytes, of the expression in parentheses.
  • E. alignas () works on expressions.
  • A12. ABE ?

  • Q13. Which legal for-loop syntax in C++?
  • A13. ABD
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 int main(int argc, char* argv[])
12 {
13     for (double d = 1.0; d <= 1.9; d += 0.1)
14         cout << "testA" << endl;
15     
16     for (int i = 15; i; i --)
17         cout << "testB" << endl;
18     
19 //    for (true) // Expected ';' in 'for' statement specifier
20         cout << "testC" << endl;
21     
22     for (; ;)
23         cout << "testD" << endl;
24     
25 //    for (int i = 0, i < 10, i ++) // Redefinition of 'i'
26         cout << "testE" << endl;
27 
28     return 0;
29 }
View Code

  • Q14. When using the multi-threading library in the C++ standard library, which are NOT callable objects which may be run in a thread?
  • A. A class with an overridden operator()
  • B. A class member function
  • C. A lambda function
  • D. An executable file
  • E. A regular function
  • A14. D ?
  • C++语言有几种callable objects:函数、函数指针、lambda表达式、bind创建的对象以及重载了函数调用运算符()的类。

  • Q15. Which are true regarding the C++ code segment below?
  • A. The variable numCities accurately reflects the number of complete State/Capital-city pairs.
  • B. After the initial 3 insertions, the map will be sorted such that the pair containing Albany will be the first entry.
  • C. The two lines indicated by the comment Alteration 2 are attempting to do something that is not allowed in C++.
  • D. The two lines indicated by the comment Alteration 1 is a valid way to find and alter this State/Capital-city pair.
  • E. The variable numStates accurately reflects the number of complete State/Capital-city pairs.
  • A15. CE
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <string>
10 #include <map>
11 using namespace std;
12 
13 int main(int argc, char* argv[])
14 {
15     map<string, string> capitals;
16     map<string, string>::iterator it;
17     string west = "California";
18     size_t numCities, numStates;
19 
20     capitals.insert(make_pair ("NewYork", "Albany"));   // misspelled
21     capitals.insert(make_pair("Texas", "Houston"));     // wrong city
22     capitals.insert(make_pair("California", "Sacramento"));
23 
24     for (auto &i : capitals) {
25         cout << i.first << " " << i.second << endl;
26     }
27     
28     cout << endl;
29 
30     numCities = capitals.count(west);
31     
32     cout << "numCities = " << numCities << endl;
33     cout << endl;
34 
35     it = capitals.find("Houston");      // Alteration 1
36     it->second = "Austin";              // Alteration 1
37 
38     for (auto &i : capitals) {
39         cout << i.first << " " << i.second << endl;
40     }
41 
42     cout << endl;
43     
44     it = capitals.find("NewYork");      // Alteration 2
45     // No viable overloaded '='
46 //    it->first = "New York";             // Alteration 2
47 
48     string ILcap = capitals["Illinois"];
49     numStates = capitals.size();
50     
51     for (auto &i : capitals) {
52         cout << i.first << " " << i.second << endl;
53     }
54 
55     cout << endl;
56 
57     cout << "ILcap = " << ILcap << endl;
58     cout << "numStates = " << numStates << endl;
59 
60     return 0;
61 }
View Code

  • Q16. Which correctly describe the overloading of the comparison operator in the C++ code fragment below?
  • A. The < operator overloading is not needed. Use the default implementation.
  • B. The < operator has the wrong return type.
  • C. The < operator may not be overloaded.
  • D. The < operator is implemented correctly as is.
  • E. The < operator will not be accessible to code using an object of type x.
  • A16. BC ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class x {
12     x& operator< (const x& rhs) const {
13         int thisVol = height * width * length;
14         int thatVol = rhs.height * rhs.width * rhs.length;
15         
16         // Non-const lvalue reference to type 'x' cannot bind to a temporary of type 'bool'
17         return thisVol < thatVol;
18     }
19 /*
20     bool operator< (const x& rhs) const {
21         int thisVol = height * width * length;
22         int thatVol = rhs.height * rhs.width * rhs.length;
23         
24         return thisVol < thatVol;
25     }
26 */
27 private:
28     int height;
29     int length;
30     int width;
31 };
32 
33 int main(int argc, char* argv[])
34 {
35     return 0;
36 }
View Code

  • Q17. Which of the following describe situations in which the C++ compiler generates a default constructor for a class?
  • A. When a compiler switch is used to instruct the C++ compiler to generate the default constructor
  • B. When the class has no default constructor defined
  • C. When the class has declared a virtual constructor
  • D. When the class has no constructor defined or deleted
  • E. When the C++ compiler needs to construct a temporary object of the class
  • A17. D ?

  • Q18. Which are C++ declarations of pointer variables (defined elsewhere) which cannot be changed (cannot be made to point to anything else)?
  • A. extern float* const ptr;
  • B. extern const std::vector<int>* ptr;
  • C. extern const void* ptr;
  • D. extern const int* const ptr;
  • E. extern double* ptr const;
  • A18. AD

  • Q19. Which represent a valid declaration for the code below for overloading the array subscript operator that returns int& in C++?
  • A. int& operator()(int);
  • B. int& operator[int](int);
  • C. int& operator[int]();
  • D. int& operator(int)();
  • E. int& operator[](int);
  • A19. E
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class MyArray {
12 public:
13     // declaration goes here
14     int& operator()(int);
15 //    int& operator[int](int);    // Expected ']'
16 //    int& operator[int]();       // Expected ']'
17 //    int& operator(int)();       // Expected ')'
18     int& operator[](int);
19 };
20 
21 int main(int argc, char* argv[])
22 {
23     return 0;
24 }
View Code

  • Q20. Which correctly describe C++ code optimization?
  • A. It is the phase of generation of an executable program during which library function references are resolved.
  • B. It is the process of modifying source code to enhance some aspect of its execution, usually at the expense of other aspects.
  • C. It is the process of tailoring machine code generated during compilation so that the resultant code runs more quickly or efficiently.
  • D. It refers to the collection and precompilation of header files for more efficient processing at compile time.
  • E. It is the process of capturing the required aspects of a software package before the source code is written.
  • A20. B ?

  • Q21. Which can be the results of executing the C++ code fragment below?
  • A. An error because member code is a char.
  • B. An output of "B".
  • C. Undefined results.
  • D. An output of "65".
  • E. Unspecified (implementation-dependent) results.
  • A21. D
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class base {
12 public:
13     base() : code('B') { }
14     char code;
15 };
16     
17 class derived : public base
18 {
19 public:
20     int code;
21 };
22 
23 int main(void)
24 {
25     derived d;
26     d.code = 65;
27     std::cout << d.code;
28 };
View Code

  • Q22. Which, when substituted for ***** in the C++ program below, will output "0 1 2 3 4 5 6 7 8 9 10 11" ?
  • E. iota( ++sequence.begin(), sequence.end(), 1 );
  • A22. E
  • http://www.cplusplus.com/reference/numeric/iota/
  • http://www.cplusplus.com/reference/utility/move/?kw=move
  • http://www.cplusplus.com/reference/algorithm/copy_if/?kw=copy_if
  • http://www.cplusplus.com/reference/algorithm/for_each/?kw=for_each
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <vector>
10 #include <numeric>      // std::iota
11 #include <utility>      // std::move
12 #include <algorithm>    // std::copy_if, std::for_each
13 using namespace std;
14 
15 int main()
16 {
17     std::vector<int> sequence{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
18     
19     /*****/
20     
21     // 1 2 3 4 5 6 7 8 9 10 11 12
22 //    iota( sequence.begin(), sequence.end(), 1 );
23 
24     // 0 1 1 0 1 5 8 13 21 34 55 89
25 //    move( sequence.begin(), sequence.begin() + 2, sequence.begin() + 3 );
26 
27     // 0 1 1 1 1 1 1 1 1 1 1 1
28 //    for_each( sequence.begin(), sequence.end(),
29 //             []( int& elem ) { if ( elem > 1 ) elem = 1; } );
30 
31     // 0 1 1 0 1 5 8 13 21 34 55 89
32 //    copy_if( sequence.begin (), sequence.begin() + 2, sequence.begin() + 3,
33 //            []( int elem ) { return elem <= 1; } );
34 
35     // 0 1 2 3 4 5 6 7 8 9 10 11
36     iota( ++sequence.begin(), sequence.end(), 1 );
37 
38     // Print out the modified sequence -- what will it be?
39     for( int elem : sequence )
40     {
41         std::cout << elem << " ";
42     }
43     
44     std::cout << std::endl;
45     
46     return 0;
47 }
View Code

  • Q23. Which are types of polymorphism in C++?
  • A. Bi-metric polymorphism
  • B. Hybrid polymorphism
  • C. Parametric polymorphism
  • D. Post processing polymorphism
  • E. Single polymorphism
  • A23. C
  • The Four Polymorphisms in C++ :
    • http://www.catonmat.net/blog/cpp-polymorphism/
    • Subtype polymorphism is also known as runtime polymorphism.
    • Parametric polymorphism is also known as compile-time polymorphism.
    • Ad-hoc polymorphism is also known as overloading.
    • Coercion is also known as (implicit or explicit) casting.

  • Q24. Which if substituted for ***** will be illegal?
  • C. bObj.weight = 110;
  • A24. C
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class A {
12 public:
13     int age;
14     int weight;
15 protected:
16     int iq;
17 };
18 
19 class B : protected A {
20 public:
21     using A::iq;
22     using A::age;
23 };
24 
25 class C : private B {
26 public:
27     using B::iq;
28     using B::weight;
29 };
30 
31 int main()
32 {
33     B   bObj;
34     C   cObj;
35     
36     /*****/
37     
38     bObj.iq = 190;
39     cObj.iq = 190;
40 //    bObj.weight = 110; // Cannot cast 'B' to its protected base class 'A'
41     bObj.age = 20;
42     cObj.weight = 110;
43     
44     return 0;
45 }
View Code

  • Q25. Which are valid when using multi-threading library that is part of the C++ STL?
  • A. Multiple threads of a single program have no way of communicating with each other.
  • B. Multi-threading requires proprietary libraries.
  • C. Multiple threads of a single program may share the same resources.
  • D. Multi-threading is not possible on a computer with a single CPU containing a single core.
  • E. Multi-threading is an ability to create a process that consists of multiple threads of execution (the smallest sequence of programming instructions that can be managed independently by a scheduler).
  • A25. CE ?

  • Q26. Which changes to the C++ code snippet below are required for it to compile?
  • A. Change the line at location #1 to: controllers::spin(false);
  • B. Change the line at location #1 to: rotary::spin(false);
  • C. Change the line at location #2 to: controllers::horizontal::move(true);
  • D. Change the line at location #2 to: horizontal::move(true);
  • E. Change the line at location #1 to: horizontal::spin(false);
  • A26. B
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 namespace controllers {
12     namespace rotary {
13         void spin(bool start) {
14             if (!start) { }
15         }
16     }
17 
18     namespace horizontal {
19         void move(bool start) {
20             if (start) {
21                 // Use of undeclared identifier 'spin'; did you mean 'rotary::spin'?
22 //                spin(false); // location #1
23 
24                 // No member named 'spin' in namespace 'controllers'; did you mean 'rotary::spin'?
25 //                controllers::spin(false);
26                 
27                 rotary::spin(false);
28                 
29                 // No member named 'spin' in namespace 'controllers::horizontal'; did you mean 'rotary::spin'?
30 //                horizontal::spin(false);
31             }
32         }
33     }
34 }
35 
36 int main(int argc, char **argv)
37 {
38 //    move(true); // location #2
39     
40 //    controllers::horizontal::move(true);
41     
42     // Use of undeclared identifier 'horizontal'; did you mean 'controllers::horizontal'?
43 //    horizontal::move(true);
44 }
View Code

  • Q27. Which are valid ways to create a type alias?
  • A. template<class T> using vec = std::vector<T>; vec<float> incomes;
  • C. typedef int intAlias; intAlias count;
  • E. template<typename T> using myGenric = T; myGenric<int> age;
  • A27. ACE
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <vector>
10 using namespace std;
11 
12 template<class T> using vec = std::vector<T>;
13 vec<float> incomes;
14 
15 /*
16 typedef std::vector<T>  vec; // Use of undeclared identifier 'T'
17 vec<double> weights;
18 */
19 
20 typedef int intAlias;
21 intAlias count;
22 
23 /*
24 typedef T genricAlias<T>; // Unknown type name 'T'
25 genricA1ias<int> population;
26 */
27 
28 template<typename T> using myGenric = T;
29 myGenric<int> age;
30 
31 int main(int argc, char **argv)
32 {
33 }
View Code

  • Q28. Which are correct statements about C++ exception handling?
  • A. The most general catch block exception should appear last, with increasingly more specific catch block exceptions preceding it.
  • B. An exception handler with no exception parameter - such as catch() - will catch any exception regardless if matching catch blocks also exist.
  • C. Multiple classes can be caught in a single catch clause as multiple arguments.
  • D. Exceptions not matching other catch blocks may be caught and handled by a handler specified by catch (…)
  • E. An exception is routed to the most appropriate/matching catch block regardless of the order the catch blocks appear.
  • A28. AD ?

  • Q29. Given the array of pointers to C++ function, which are correct declaration for the function processor()?
  • A. double processor(int i, double(*f), int idx);
  • B. double processor(int i, double(*f(int)), int idx);
  • C. double processor(int i, double(*f)(int), int idx);
  • D. double processor(int i, double(*f[]), int idx);
  • E. double processor(int i, double(f[])(), int idx);
  • A29 ?

  • Q30. Which of the following statements are true with respect to C++ code excerpt below?
  • A. a6 is a const reference to a3.
  • B. d5 is an int initialized to 5.
  • C. al is an instance of class auto, instantiated by an integer value of "5" being passed to a constructor.
  • D. a2 is an int equal to 10.
  • E. d4 is an int initialized to 10.
  • A30. BD ?
  • auto (C++) | Microsoft Docs
    • https://docs.microsoft.com/en-us/cpp/cpp/auto-cpp
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <typeinfo> // std::typeid
10 using namespace std;
11 
12 int somFunc() { return 10; }
13 
14 int main(int argc, char **argv)
15 {
16     auto a1 = 5;
17     auto a2 = somFunc();
18     
19     decltype(a2)    d4;
20     decltype(somFunc()) d5 = 5;
21     
22     double a3 = 4.0;
23     auto &a6 = a3;
24     
25     cout << typeid(a1).name() << endl;
26     cout << typeid(a2).name() << endl;
27     cout << typeid(d4).name() << endl;
28     cout << typeid(d5).name() << endl;
29     cout << typeid(a3).name() << endl;
30     cout << typeid(a6).name() << endl;
31 }
View Code

  • Q31. Which are member functions of a C++ STL container class which return an iterator to the first element in the container?
  • A. start()
  • B. iterator()
  • C. _first()
  • D. begin()
  • E. bof()
  • A31. D

  • Q32. Which can be used to replace the ***** in the C++ code excerpt below to create a 10x5 rectangular array of doubles on the heap?
  • A. dArray[r] = new double*;
  • B. dArray[r] = new double[columns];
  • C. dArray[r] = new double;
  • D. *dArray[r] = new double[columns];
  • E. dArray[r] = new double*[columns];
  • A32. B
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 int main(int argc, char **argv)
12 {
13     int rows = 10;
14     int columns = 5;
15     
16     double **dArray = new double*[rows];
17     
18     for (int r = 0; r < rows; r ++) {
19         /*****/
20         
21         // Assigning to 'double *' from incompatible type 'double **'; dereference with *
22 //        dArray[r] = new double*;
23         dArray[r] = new double[columns];
24         dArray[r] = new double;
25         // Assigning to 'double' from incompatible type 'double *'; dereference with *
26 //        *dArray[r] = new double[columns];
27         // Assigning to 'double *' from incompatible type 'double **'; dereference with *
28 //        dArray[r] = new double*[columns];
29     }
30 }
View Code

  • Q33. Which of the following correctly describe the purpose of the C++ Standard Library numeric_limits type?
  • A. It is a type that specifies the limits of various resources a program has including memory, storage space and CPU time.
  • B. It is a type that is used to designate actions to be taken when variables exceed their limits.
  • C. It is a template which is specialized for basic types designating the upper and lower limits for those on the current platform.
  • D. It is used to set limits on numeric variables, beyond which an exception will be generated.
  • E. It is used to set the upper and lower limits a variable may have.
  • A33. C ?
  • numeric_limits - C++ Reference
    • http://www.cplusplus.com/reference/limits/numeric_limits/
  • numeric_limits Class | Microsoft Docs
    • https://docs.microsoft.com/en-us/cpp/standard-library/numeric-limits-class

  • Q34. Which are true about the declarations representing the C++ class hierachy below?
  • A. Level5::func2(int) will override any functionality defined in Level2::func2(int).
  • B. Level1::func1() functionality has been removed from Level2 but is still available to subsequent levels.
  • C. Level5::func4(char) will produce a compiler error since it is not a true override.
  • D. Level5::func3(int) will produce a compiler error since virtual functions implemented elsewhere cannot be deleted.
  • E. Level5 results in a compiler error since it cannot inherit from Levei3.
  • A34. CE ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class Level1 {
12 public:
13     void func1();
14 };
15 
16 class Level2 : public Level1 {
17 public:
18     void func1() = delete;
19     int func2(char);
20 };
21 
22 class Level3 final : public Level2 {
23 public:
24     virtual double func3(int);
25 };
26 
27 class Level4 : public Level2 {
28     virtual double func4(int);
29     double func5(char a);
30 };
31 
32 class Level5 : public Level3 { // Base 'Level3' is marked 'final'
33 public:
34     double func2(char) override; // Only virtual member functions can be marked 'override'
35     virtual double func3(int) = delete;
36     virtual double func4(char) override; // 'func4' marked 'override' but does not override any member functions
37 };
38 
39 int main(int argc, char **argv)
40 {
41     Level3 obj;
42     
43     obj.func1(); // Attempt to use a deleted function
44     
45     return 0;
46 }
View Code

  • Q35. Which correctly describe C++11 unordered associative containers, for example std::unordered_set, std::unordered_map, std::unordered multiset, std::unordered—multimap?
  • A. They perform their lookup based on a hash function.
  • B. Lookup has o ( log (n) ) complexity.
  • C. They perform their lookup based on an ordering criterion.
  • D. They are implemented as binary trees.
  • E. All allow multiple entries for the same key.
  • A35.  A ?

  • Q36. Which correctly describe attributes of function overloading in C++ programming?
  • A. The functions have to be nested within a namespace.
  • B. The code of the functions must be different.
  • C. The names of the functions must be different.
  • D. The argument types and/or the number of arguments to the functions must be different.
  • E. The return types from the functions must be different.
  • A36. AD

  • Q37. Immediately after the evaluation of the expression and the assignment of the results to interger variable 'a', which will be correct, regarding the value of 'a'?
  • A. The value of 'a' cannot reliably be known in all cases and environments since the order that x(), y(), z() will be evaluated is not fixed by the standard.
  • E. 13
  • A37. E
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 int b = 1;
12 
13 int x() { b ++; return b; }
14 int y() { b *= 3; return b - 1; }
15 int z() { b = b + 8; return b + 2; }
16 
17 int main(int argc, char **argv)
18 {
19     int a = x() - y() + z(); // 2 - 5 + 16
20     
21     // 13
22     cout << a << endl;
23     
24     // 14
25     cout << b << endl;
26 
27     return 0;
28 }
View Code

  • Q38. Which class types must contain at least one pure virtual function in C++?
  • A. An abstract class
  • B. A mixin class
  • C. A virtual class
  • D. A pure virtual class
  • E. A closure class
  • A38. AD ?
  • A class that contains at least one pure virtual function is considered an abstract class.
    • https://msdn.microsoft.com/en-us/library/c8whxhf1.aspx

  • Q39. Which changes must be made to the C++ code below so it can compile? 
  • A.    Remove the constexpr qualifier from Line 5.
  • B.    Remove the constexpr qualifier from Line 2.
  • C.    Add the constexpr qualifier to Line 3.
  • D.    Add the constexpr qualifier to Line 4.
  • E.    Remove the constexpr qualifier from Line 1.
  • A39. A
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class Vector {
12 public:
13     constexpr Vector( double i, double j, double k ) // Line 1
14     : ivec( i ), jvec( j ), kvec( k )
15     {}
16     
17     constexpr Vector( const Vector &rhs )   // Line 2
18     : ivec( rhs.ivec ), jvec( rhs.jvec ), kvec( rhs.kvec )
19     {}
20     
21 private:
22     double ivec, jvec, kvec; // Line 3
23 };
24 
25 int main(int argc, char **argv)
26 {
27     Vector vec1( 2, 7, -1 ); // Line 4
28 //    constexpr Vector vec2( vec1 ); // Line 5
29     Vector vec2( vec1 ); // Line 5
30 
31     return 0;
32 }
View Code

  • Q40. When writing a multi-threaded program, using the multi-threading library that is part of the C++ standard library, which are valid regarding threads accessing a shared resource?
  • A. Resource sharing among threads cannot be done safely in all situations and is best avoided.
  • B. When multiple threads are sharing a resource, the order in which the threads access the resource does not matter since they all have their own virtual instance of that resource.
  • C. There are several things the threads must do, in order to safely share a resource.
  • D. If a thread "locks" a resource, that resource is automatically unlocked when the thread terminates.
  • E. When multiple threads are accessing a shared resource, access control is done automatically by the C++ runtime environment and is seamless to the individual threads.
  • A40. C ?

  • Q41. Which are valid C++ expressions for creating and using an alias of a type?
  • A. typedef int MYVAR1; MYVAR1 m1;
  • C. using MYVAR2 = char; MYVAR2 m2; m2 = 'a';
  • A41. AC
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 typedef int MYVAR1;
12 MYVAR1 m1;
13 
14 // alias MYVAR4 long; // Unknown type name 'alias'
15 
16 using MYVAR2 = char;
17 MYVAR2 m2;
18 
19 // type MYVAR3 double; // Unknown type name 'type'
20 
21 // typedef MYVAR5 using int; // Unknown type name 'MYVAR5'
22 
23 int main(int argc, char **argv)
24 {
25     m2 = 'a';
26 
27     return 0;
28 }
View Code

  • Q42. Given the array of pointers to C++ functions below, which of the following lines may be inserted into the loop at ***** such that successive functions fx(), fy() and fz() are pointed to and executed, one per loop?
  • A. PFvv[i]();
  • B. *functions[i]();
  • C. &functions[i]();
  • D. functions[i]();
  • E. functions[i];
  • A42. DE ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 void fx() {}
12 void fy() {}
13 void fz() {}
14 
15 typedef void(*PFvv)();
16 PFvv functions[3] = {fx, fy, fz};
17 
18 int main(int argc, char **argv)
19 {
20     for (int i = 0; i < 3; i++ ) {
21         /*****/
22 //        PFvv[i](); // Redefinition of 'i'
23 //        *functions[i](); // Indirection requires pointer operand ('void' invalid)
24 //        &functions[i](); // Cannot take the address of an rvalue of type 'void'
25         functions[i]();
26         functions[i];
27     }
28 
29     return 0;
30 }
View Code

  • Q43. Throwing and catching C++ exceptions is an alternative to which techniques?
  • A. Running the program with stack tracing enabled
  • B. Returning error codes
  • C. Using hardware exceptions
  • D. Using assertions
  • E. Exhaustive debugging
  • A43. BD

  • Q44. Which are true about the C++11 compliant program below?
  • A. Line 1 does nothing if NDEBUG is not defined.
  • B. The program compiles normally, but prints the warning message If the collection is empty.
  • C. Line 2 results in a compiler error if the collection is empty.
  • D. Line 2 will cause a compiler error because ">>" will be mistaken for the right-shift operator.
  • E. Line 2 results in a compiler error if type std::vector<int> is not an empty class.
  • A44. E ?
  • static_assert | Microsoft Docs
    • https://docs.microsoft.com/en-us/cpp/cpp/static-assert
  • is_empty Class | Microsoft Docs
    • https://docs.microsoft.com/en-us/cpp/standard-library/is-empty-class
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <vector>
10 using namespace std;
11 
12 template <typename T>
13 void processCollection(T t) {
14     // Static_assert failed "collection is empty"
15     static_assert(std::is_empty<T>::value, "collection is empty"); // Line 1
16     
17     // ...
18 };
19 
20 int main(int argc, char **argv)
21 {
22     std::vector<int>    ages;
23     
24     // ...
25     
26     processCollection<std::vector<int>>(ages);  // Line 2
27 
28     return 0;
29 }
View Code

  • Q45. Which is a DeMorgan equivalent to the statement below? return !a || !b;
  • A. return !b || !a;
  • B. return !a && !b;
  • C. return a && b;
  • D. return !(a && b);
  • E. return !(a || b);
  • A45. AD

  • Q46. which about std::weak_ptr in C++ are correct?
  • A. An instance can transfer ownership of its contained pointer if assigned to an object of std::unique_ptr.
  • B. The object being referenced by the std::unique_ptr instance must be checked to see if it still exists before it can be accessed.
  • C. Access to an instance's contained pointer is via operator->().
  • D. Each instance does not increase the reference count of the pointer object being shared.
  • E. An instance must be initialized by either an object of std::shared_ptr or another std::weak_ptr object.
  • A46. D ?
  • weak_ptr Class | Microsoft Docs
    • https://docs.microsoft.com/en-us/cpp/standard-library/weak-ptr-class

  • Q47. Which correctly identify the results of building the C++ code below?
  • A. Compiling generates an error because main() is not allowed to access the protected member data of SomeClass.
  • B. The build will be successful, and the program will display an output of "5" when it is executed.
  • C. Compiling generates an error because SomeFunc() is not allowed to access the protected member data of SomeClass.
  • D. Compiling generates an error because Anotherclass is not allowed to declare someFunc() as a friend as it is already a friend of SomeClass.
  • E. Compiling generates an error because Another() of AnotherClass is not allowed to access the protected member data of SomeClass.
  • A47. AC
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class SomeClass {
12 protected:
13     int data;
14     friend class AnotherClass;
15 };
16 
17 void SomeFunc(SomeClass sc) {
18     sc.data = 5; // 'data' is a protected member of 'SomeClass'
19 }
20 
21 class AnotherClass {
22 public:
23     void Another(SomeClass sc) {
24         sc.data = 25;
25     }
26     
27     friend void SomeFunc(SomeClass sc);
28 };
29 
30 int main(int argc, char **argv)
31 {
32     SomeClass sc;
33     SomeFunc(sc);
34     
35     cout << sc.data << endl; // 'data' is a protected member of 'SomeClass'
36 
37     return 0;
38 }
View Code

  • Q48. Which are true?
  • A. It cannot be assigned to.
  • B. It can be passed to a function by value.
  • C. It cannot be returned from a funtion by value.
  • D. It can be copied.
  • E. Another class can be derived from it.
  • A48. ACE
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class BBus {
12     int     DriverID;
13     string  routeName;
14     
15     BBus(BBus &orig) {
16         DriverID = orig.DriverID;
17         routeName = orig.routeName;
18     }
19     
20     BBus& operator=(BBus &rhs) {
21         DriverID = rhs.DriverID;
22         routeName = rhs.routeName;
23         return *this;
24     }
25 
26 public:
27     BBus(int id = 0, string rt = "") : DriverID(id), routeName(rt) {}
28 };
29 
30 class C : public BBus {
31 public:
32     void print() {
33         cout << __func__ << endl;
34     }
35 };
36 
37 void testB(BBus objB) {
38     cout << __func__ << endl;
39 }
40 
41 /*
42 BBus testB2() {
43     BBus objB;
44     return objB; // Calling a private constructor of class 'BBus'
45 }
46  */
47 
48 int main(int argc, char **argv)
49 {
50     BBus obj;
51 
52     BBus obj3;
53     //    obj3 = obj; // 'operator=' is a private member of 'BBus'
54 
55     //    testB(obj); // Calling a private constructor of class 'BBus'
56 
57     //    BBus obj2(obj); // Calling a private constructor of class 'BBus'
58     
59     C objC;
60     
61     objC.print();
62     
63     return 0;
64 }
View Code

  • Q49. Which of the following statements are valid regarding the usage of STL algorithms with STL containers?
  • A. STL algorithms provide for operations commonly performed on STL containers.
  • B. STL algorithms are used with STL containers through the use of iterators.
  • C. STL algorithms are used with STL containers to prevent conflicts with other programs.
  • D. STL algorithms provide safe multi-threaded access to STL container elements.
  • E. STL algorithms cannot be used to sort STL container elements.
  • A49. AB ?

  • Q50. Which of the following statements correctly describe C++ exceptions?
  • A, When an exception is thrown, and there is more than one applicable exception handler for the given exception type, control is transferred to the handler nearest to the point of the exception.
  • B. When an exception is caught, it cannot be thrown further.
  • C. A try block can be declared without a catch block.
  • D. When an exception is thrown, all objects dynamically created on the heap are released and their destructors invoked.
  • E. If there are multiple catch blocks, the exceptions they catch may not be from the same inheritance hierarchy.
  • A50. C ?

  • Q51. Which of the following declarations correctly use overloading of the "<<" operator of cout to display the contents of a C++ user-defined class of type Record?
  • C. std::ostream& operator <<(std::ostrearas, Records);
  • A51. C
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 //add declaration here
12 
13 int main()
14 {
15     Record myRecord;
16     
17     // other code omitted
18     cout << myRecord << endl;
19 }
View Code

  • Q52. Which of the following are errors with the use of dynamic memory in the C++ code segment below?
  • A. There is an attempt to access memory allocated on the heap after it has been deleted.
  • B. Allocation with the new operation must to be enclosed in a try/catch block.
  • C. There is an attempt to delete an object which was declared on the stack.
  • D. Since the pointer checking is overwritten, the allocated memory cannot be accessed or freed.
  • E. There is an attempt to delete memory allocated on the heap by using a non-pointer.
  • A52. E
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class Account {
12 public:
13     void deposit(int);
14 };
15 
16 int main(int argc, char **argv)
17 {
18     Account *checking = new Account;
19     checking->deposit(1000);
20     
21     Account myAccount;
22     myAccount.deposit(1000);
23     
24     checking = &myAccount;
25     
26     checking->deposit(1000);
27     delete checking;
28     
29     Account* saving = new Account;
30     
31     Account & rsav = *saving;
32     rsav.deposit(2000);
33     delete rsav; // Cannot delete expression of type 'Account'
34     
35     return 0;
36 }
View Code

  • Q53. Which of the following statements about the use of virtual functions in C++ are valid?
  • A. A data member can be declared as virtual.
  • B. A static member function can be declared as virtual.
  • C. A virtual function needs to be declared as virtual only in the base class and optionally in the derived classes.
  • D. An operator can be declared as virtual.
  • E. A friend function can be declared as virtual.
  • A53. C ?

  • Q54. Which of the following statements correctly describe the usage of an unrestricted union in C++?
  • A. It is allowed to contain members of class type.
  • B. It is not allowed to contain members of built-in type.
  • C. It has the unrestricted keyword qualifying the union name.
  • D. It must define a constructor.
  • E. It must define an assignment operator.
  • A54. A ?

  • Q55. Which of the following lines of C++ code are valid implementations of the Penguin swim() method defined below?
  • A. SwimmingAnimal.move();
  • B. SwimmingAnimal::move();
  • C. Animal::changePosition(2);
  • D. move();
  • E. position += 2;
  • A55. BC
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class Animal {
12 private:
13     int position;
14 public:
15     Animal() { position = 0; }
16     void changePosition(int i) {
17         position += i;
18     }
19 };
20 
21 class WalkingAnimal : virtual public Animal {
22 public:
23     void move() { changePosition(2); }
24 };
25 
26 class SwimmingAnimal : virtual public Animal {
27 public:
28     void move() { changePosition(1); }
29 };
30 
31 class Pengiun : public WalkingAnimal, public SwimmingAnimal {
32 public:
33     void swim() {
34         // insert code here
35 //        SwimmingAnimal.move(); // Use of undeclared identifier 'SwimmingAnimal'
36         SwimmingAnimal::move();
37         Animal::changePosition(2);
38 //        move(); // Member 'move' found in multiple base classes of different types
39 //        position += 2;  // 'position' is a private member of 'Animal'
40     }
41     void walk() {}
42 };
43 
44 int main(int argc, char **argv)
45 {
46     return 0;
47 }
View Code

  • Q56. When overloading C++ unary operators, which of the following are legal options with respect to the number of parameters to be used?
  • A. One (dummy) parameter, when the operator is a particular type of increment/decrement class member function.
  • B. One parameter, when the operator function is a free standing function (not a class member).
  • C. Any number of parameters, when the operator function is a free standing function (not a class member).
  • D. No parameters, when the operator function is a free standing function (not a class member).
  • E. No parameters, when the operator function is a class member.
  • A56. E ?

  • Q57. Which of the following are true statements about the use of std::promise and std::future in the snippet below, when using the multi-threading library that is part of the C++ standard library?
  • A. The return statement on Line C does not return a value to function get() (on Line I) in main(). Other mechanisms are needed to pass return values back to a parent thread.
  • B. After assigning a value to y1 on Line I, the thread function func1() can, in theory, assign a new value to the promise object and that value can be assigned to variable y1 with another call to get() as on Line K.
  • C. If the thread function func1() is still running when the parent thread calls member function get() (at Line I) the function get() will throw an exception.
  • D. If the function on Line B throws an exception, the exception will be reported back to main() similarly to if the function get() (on Line I) threw the exception.
  • E. The get() member functions called on Line M and Line N will block until the thread function func2() has completed and exited.
  • A57. B ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <future>
10 using namespace std;
11 
12 int func1(std::future<int>& delayedParam)
13 {
14     int x = delayedParam.get();     // Line A
15     
16     // do other work here...
17 
18     int y = function_potentially_throwing_exception();  // Line B
19 
20     return y;                       // Line C
21 }
22 
23 void func2(std::promise<int> &result, std::promise<bool> &done)
24 {
25     // do other work here...
26     
27     result.set_value(10);           // Line D
28     done.set_value(true);           // Line E
29 
30     // could do other work here...
31 }
32 
33 int main()
34 {
35     //-----------------for func1----------------------------
36     std::promise<int>   promisedArgument;
37     std::future<int>    futureArgument = promisedArgument.get_future();
38 
39     auto f = std::async(std::launch::async, func1,
40                         std::ref(futureArgument));  // Line F
41 
42     promisedArgument.set_value(4);                  // Line G
43 
44     try {
45         bool isValid = f.valid();                   // Line H
46         int yl = f.get();                           // Line I
47 
48         isValid = f.valid();                        // Line J
49         int y2 = f.get();                           // Line K
50     }
51     catch (...) {
52     //...
53     }
54 
55     //--------------for func2--------------
56     std::promise<int>       resultPromise;
57     std::promise<bool>      donePromise;
58 
59     std::future<int>    resultFuture = resultPromise.get_future();
60     std::future<bool>   doneFuture = donePromise.get_future();
61 
62     std::async(std::launch::async, func2,
63                std::ref(resultPromise), std::ref(donePromise) );    // Line L
64 
65     bool done = doneFuture.get();           // Line M
66     int result_f2 = resultFuture.get();     // Line N
67 
68     // do other things with result_f2 ...
69 
70     return 0;
71 }
View Code

  • Q58. Which of the following correctly identify the output of executing the C++ code below?
  • D. Type of new_arg is not const / Type of new一arg is not const / Type of new_arg is not const
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 template<class T>
12 void stripConst( T arg ) {
13     typename std::remove_const<T>::type new_arg;
14     
15     if (std::is_const< decltype(new_arg) >::value)
16         cout << "Type of new_arg is const" << endl;
17     else
18         cout << "Type of new_arg is not const" << endl;
19 }
20 
21 int main(int argc, char **argv)
22 {
23     stripConst( "Blinkin" );
24     stripConst( 676 );
25     stripConst( 3.14 );
26     
27     return 0;
28 }
View Code

  • Q59. For the C++ declarations below which are valid operations?
  • D. team1 = bonds;
  • A59. D
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 enum REG {stocks, bonds};
12 enum LTC {swaps, swaptions};
13 enum DRV {futures, options};
14 
15 REG team1;
16 LTC team2;
17 DRV team3;
18 
19 int main(int argc, char **argv)
20 {
21     //    team2 = 1;
22     //    team1 = stocks + 1;
23     //    team3 = options + futures;
24     
25     team1 = bonds;
26     
27     //    team1 = futures;
28     
29     return 0;
30 }
View Code

  • Q60. A programmer has decided to store objects of a user-defined type (a structure) in an unordered_set. Which of the following are steps that must be taken in order for this to work properly?
  • A. The structure will have to overload operator() so that elements may be located in the collection.
  • B. The structure will have to create a specialization of std::hash for the user defined type.
  • C. The structure will have to overload operator==() in order for this type to be supported by this collection.
  • D. The structure will have to define a default constructor.
  • E. The structure will have to overload operator<() so that the elements can be stored in the proper place within the collection.
  • A60. C

  • Q61. Given the code below, when using the regular expression library that is part of the C++ standard library, which of the following statements are true after the execution of Line 1?
  • A. The element matches [n] corresponds to the nth character of the match.
  • B. The size() member function of the matches object contains the number of matches found in the target string.
  • C. The element matches [n] corresponds to the nth match of the expression in the target string.
  • D. The size() member function of the matches object contains the number of characters contained in the match.
  • E. The empty() member function of the matches object deletes all of the stored matches.
  • A61. BC ?
  • regex_search - C++ Reference
    • http://www.cplusplus.com/reference/regex/regex_search/?kw=regex_search
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <string>
11 #include <regex>
12 using namespace std;
13 
14 int main(int argc, char **argv)
15 {
16     smatch matches;
17     
18     string  target("this subject has a submarine as a subsequence");
19     regex  expr("\b(sub)([^ ]*)");
20     
21     auto x = regex_search(target, matches, expr);  // Line 1
22 
23     return 0;
24 }
View Code

  • Q62. A requirements spedficatlon for a C++ class called Rotator calls for the exclusive management of a critical resource. It has been decided to enforce the exclusiveness, in part, by preventing the class from being copied. In addition, the design calls for the use of the compiler generated default constructor for this class. Which of the following are steps that may be taken to implement this part of the requirements?
  • A. Let the compiler generate the default constructor and manually declare that it is final such as Rotator() final; .
  • B. Declare a copy assignment operator, then explicitly delete it such as void operator=(Rotator const &) = delete;.
  • C. Declare a copy constructor, then explicitly delete it such as Rotator (Rotator const &) = delete;.
  • D. Declare the compiler generated constructor to be explicitly defaulted such as Rotator() = default;.
  • E. Let the compiler generate the default constructor and don't make any other reference to it In the class declaration.
  • A62. BC ?

  • Q63. Given the C++ excerpt below, which of the following techniques can be used to assign pointer cw* to pointer w*?
  • A. First assign cw to a temporary variable, then assign the temporary variable to w.
  • B. Make the assignment in the normal way.
  • C. Make the assignment in the normal way with the addition of the reinterpret cast operator.
  • D. Make the assignment in the normal way with the addition of the static_cast operator.
  • E. Make the assignment in the normal way with the addition of the const cast operator.
  • A63. E ?
  • reinterpret_cast 运算符 | Microsoft Docs
    • https://docs.microsoft.com/zh-cn/cpp/cpp/reinterpret-cast-operator
  • static_cast 运算符 | Microsoft Docs
    • https://docs.microsoft.com/zh-cn/cpp/cpp/static-cast-operator
  • const_cast 运算符 | Microsoft Docs
    • https://docs.microsoft.com/zh-cn/cpp/cpp/const-cast-operator
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 using namespace std;
11 
12 class widget {};
13 
14 int main(int argc, char **argv)
15 {
16     widget const *cw = new widget;
17     widget *w;
18 
19 /*
20     w = cw; // Assigning to 'widget *' from incompatible type 'const widget *'
21     w = reinterpret_cast< widget* >(cw); // Reinterpret_cast from 'const widget *' to 'widget *' casts away qualifiers
22     w = static_cast< widget* >(cw); // Static_cast from 'const widget *' to 'widget *', which are not related by inheritance, is not allowed
23 */
24     w = const_cast< widget* >(cw);
25     
26     return 0;
27 }
View Code

  • Q64. Which of the following are correct declarations of a pointer and assignment to the C++ member function avg of class Myciass?
  • A. double (MyClass::ptr)(); ptr = &MyClass::avg;
  • B. double (MyClass::*f)(); f = SMyClass::avg;
  • C. double (*fptr)(); fptr = &MyClass::avg;
  • D. double (MyClass::*f()); *f = SMyClass::avg;
  • E. double (MyClass::*f)() = SMyClass::avg;
  • A64. BE
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class MyClass {
12     double scores[100];
13 public:
14     double avg () { return 0; }
15     double high() { return 0; }
16     double low () { return 0; }
17 };
18 
19 int main(int argc, char **argv)
20 {
21 /*
22     double (MyClass::*f)();
23     f = &MyClass::avg;
24 */
25     
26     double (MyClass::*f)() = &MyClass::avg;
27     
28     return 0;
29 }
View Code

  • Q65. Which of the following statements regarding the C++ program are correct?
  • A. There will a memory leak because the object created on Line A still exists but there is no reference to it and cannot be deleted.
  • B. After the function process () has executed, Line X will retrieve the name "New Fluffy" for the object stray.
  • C. There will be an error on Line B as the pointer myPet no longer refers to any object.
  • D. The move operation on Line D is unnecessary.
  • E. There will be an error on Line X because the object referred to no longer exists.
  • A65. C ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 class animal {
12     string name = "";
13 public:
14     void setName(string n) { name = n; }
15     string getName(void) { return name; }
16 };
17 
18 void process(animal *pet)
19 {
20     //I already have a pet
21     unique_ptr<animal> myPet(new animal);   // Line A
22     myPet->setName("Fluffy");
23 
24     // now you are getting a pet
25     unique_ptr<animal> yourPet;
26 
27     // I'll give you my pet
28     yourPet = move(myPet);                  // Line D
29 
30     // but you can't my pet's name
31     myPet->setName ("Not Fluffy");          // Line B
32 
33     // I'll take the stray
34     myPet.reset (pet);
35 
36     // the stray gets a new name
37     myPet->setName("New Fluffy");
38 }
39 
40 int main ()
41 {
42     // in main...
43 
44     animal *stray = new animal;
45 
46     stray->setName("Scratchy");
47     process (stray);
48     string name = stray->getName();         // Line X
49 
50     // other code here
51     
52     return 0;
53 }
View Code

  • Q66. In the given C++ code snippet, which of the following statements correctly identify how Mon of enum day can be assigned to a variable named var?
  • A. enum DAY var = Mon;
  • B. DAY var = DAY::Mon;
  • C. enum DAY var = dynamic_cast<enum DAY>(Mon);
  • D. enum DAY var = DAY::Mon;
  • E. enum DAY var = DAY.Mon;
  • A66. ABD
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 int main ()
12 {
13     enum DAY { Mon = 11, Tue = 12 };
14     
15     enum DAY var = Mon;
16     DAY var = DAY::Mon;
17 //    enum DAY var = dynamic_cast<enum DAY>(Mon); // 'enum DAY' is not a reference or pointer
18     enum DAY var = DAY::Mon;
19 //    enum DAY var = DAY.Mon; // 'DAY' does not refer to a value
20 
21     return 0;
22 }
View Code

  • Q67. Which of the following are accurate statements concerning the behavior of the dynamic_cast operatorin C++?
  • A. The dynamic_cast operator can only be applied to a pointer or reference to a polymorphic object,
  • B. A dynamic_cast applied to a pointer returns 0 (zero) if the operation fails,
  • C. The dynamic_cast operator’s behavior is a subset of the reinterpret_cast operator's behavior.
  • D. A dynamic_cast applied to a reference throws an exception if the operation fails.
  • E. The dynamic_cast operator fails if used for upcasting.
  • A67. B ?

  • Q68. A programmer has partially defined the C++11 compliant template function below: Not knowing what the lhs and rhs types will be, the coder is not sure how to specify the return value of this function. Which of the following approaches can the programmer take?
  • A. The coder must use auto as the return type and let the compiler determine what to replace it with, such as: auto myfunc (const T1 &lhs, const T2 &rhs) {return lhs + rhs;}
  • B. The coder must use the utility function decltype() to determine the return type, such as: decltype (lhs + rhs) myfunc (const T1 &lhs, const T2 &rhs) {return lhs + rhs;}
  • C. The coder must specify a single return type that will be returned from any instantiation of the template function.
  • D. The coder must use a combination of auto and decltype: auto myfunc (const T1 &lhs, const T2 &rhs) -> decltype (lhs+rhs) {return lhs + rhs;}
  • E. The coder must specify one of template parameter types as the return type, then ensure that the template function converts/casts its results to match that return type.
  • A68. D ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 using namespace std;
10 
11 template<class T1, class T2>
12 // RETURN-TYPE-HERE myfunc(const T1 &lhs, const T2 &rhs) {return lhs + rhs;}
13 // auto myfunc (const T1 &lhs, const T2 &rhs) {return lhs + rhs;} // 'auto' return without trailing return type; deduced return types are a C++14 extension
14 
15 // decltype(lhs + rhs) myfunc (const T1 &lhs, const T2 &rhs) {return lhs + rhs;} // Use of undeclared identifier 'rhs'
16 
17 auto myfunc(const T1 &lhs, const T2 &rhs)->decltype(lhs+rhs) {return lhs + rhs;}
18 
19 int main ()
20 {
21     return 0;
22 }
View Code

  • Q69. Which of the following are NOT valid segments of code when inserted at and in the C++ program segment below (the choices for a given insertion point are not mutually exclusive)?
  • A. **1** : std::unordered_map<std::string, int> colorCommands = {{"red", 3}, {"yellow", 2}, {"green", 1}};
  • B. **1** : std::unordered_map<std::string, int> colorCommands; colorCommands["red”] = 3; colorCommands["yellow"] = 2; colorCommands["green"] = 1;
  • C. **1** : std::unordered_map<std::string, int> colorCommands; colorCommands.insert(std::make_pair("red", 3)); colorCommands.insert({ "yellow", 2 }); colorCommands.emplace("green", 1);
  • D. **2** : switch(colorCommands.find(color))
  • E. **2** : switch (colorCommands[color])
  • A69. D
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <unordered_map>
10 
11 std::string checkTheLight() {
12     return "green";
13 }
14 
15 int main ()
16 {
17     enum colors {green, yellow, red};
18     
19     /**1**/
20     std::unordered_map<std::string, int> colorCommands = {{"red", 3}, {"yellow", 2}, {"green", 1}};
21 /*
22     std::unordered_map<std::string, int> colorCommands;
23     colorCommands["red"] = 3;
24     colorCommands["yellow"] = 2;
25     colorCommands["green"] = 1;
26 
27     std::unordered_map<std::string, int> colorCommands;
28     colorCommands.insert(std::make_pair("red", 3));
29     colorCommands.insert({ "yellow", 2 });
30     colorCommands.emplace("green", 1);
31 */
32     std::string color = checkTheLight();
33 
34     /**2**/
35 //    switch (colorCommands.find(color)) // Statement requires expression of integer type ('iterator' (aka '__hash_map_iterator<__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<std::__1::basic_string<char>, int>, void *> *> >') invalid)
36     
37     switch (colorCommands[color])
38     {
39     case green:
40         std::cout << "get going";
41         break;
42     case yellow:
43         std::cout << "slow down";
44         break;
45     case red:
46     default:
47         std::cout << "stop";
48         break;
49     }
50 
51     std::cout << std::endl;
52     
53     return 0;
54 }
View Code

  • Q70. A programmer wishes to construct a new type trait, based on is_floating_point<T> In the C++11 standard library. This new trait will validate not only the built-in floating point types, but std::complex<T>, parameterized by those same types, as well. Which of the following declarations will accomplish this?
  • A. template< class T > struct is_complex_or_fp< std::complex< T > > std::is_floating_point< T > { };
  • B. template< class T > struct is_complex_or_fp : std::is_floating_point< T > { static constexpr bool value{true}; };
  • C. template< class T > struct is_complex_or_fp : std::ia_floating_point< T > { }; template< class T > struct is_complex_or_fp< T > : std::is_floating_point< std::complex< T > > { };
  • D. template< class T > struct is_complex_or_fp : std::is_floating_point< T > { };template< class T > struct is_complex_or_fp< std::complex< T > > : std::is_floating_point< T > { };
  • E. template< class T > struct is_complex_or_fp< T > : std::is_floating_point< std::complex< T > > { };
  • A70. BD
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 #include <complex>
10 
11 /*
12 template< class T >
13 struct is_complex_or_fp< std::complex< T > > std::is_floating_point< T > { }; // Explicit specialization of non-template struct 'is_complex_or_fp'
14 */
15 
16 
17 template< class T >
18 struct is_complex_or_fp : std::is_floating_point< T > { static constexpr bool value{true}; };
19 
20 
21 /*
22 template< class T >
23 struct is_complex_or_fp : std::is_floating_point< T > { };
24 
25 template< class T >
26 struct is_complex_or_fp< T > : std::is_floating_point< std::complex< T > > { }; // Class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
27  */
28 
29 
30 template< class T >
31 struct is_complex_or_fp : std::is_floating_point< T > { };
32 
33 template< class T >
34 struct is_complex_or_fp< std::complex< T > > : std::is_floating_point< T > { };
35 
36 
37 /*
38 template< class T >
39 struct is_complex_or_fp< T > : std::is_floating_point< std::complex< T > > { }; // Explicit specialization of non-template struct 'is_complex_or_fp'
40 */
41 
42 int main ()
43 {
44     return 0;
45 }
View Code

  • Q71. Which of the following statements are true with respect to the C++ code excerpt below?
  • A. The constructor Course (double d) may not call another constructor such as Course (int a).
  • B. The default constructor results in an object with all data members uninitialized.
  • C. The default constructor results in an object with all data members initialized with the values shown in the class definition.
  • D. The use of braces for the class instantiation of objects course1, course2, course3 and course4 is not valid C++ syntax.
  • E. Static data members may not be initialized in the class definition as shown.
  • A71. E ?
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 
10 class Course {
11     static int  classCount = 0; // Non-const static data member must be initialized out of line
12     std::string courseName {"CS101"};
13     int         room = 100;
14     int quizzes[5] {0, 0, 0, 0, 0};
15 
16 public:
17     Course( ) {};
18     Course(int a) : room(a) {}
19     Course (std::string b) : courseName(b), room(200) {}
20     Course(double d) : Course( static_cast<int>(d) ) {}
21 };
22 
23 int main ()
24 {
25     Course course1{};
26     Course course2{300};
27     Course course3{"EnglishlOl"};
28     Course course4{3.14};
29 
30     return 0;
31 }
View Code

  • Q72. Which of the following changes must be made to the C++ code below so that the widget class definition is const-correct for its usage in main() ?
  • A. Change Widget(Widget& w); to Widget(const Widget& w);
  • B. Change Gizmo (int p, int r) ; to Gizmo (const int p, const int r);
  • C. Change Widget& operator=(Widget& rhs); to Widget& operator=(const Widget& rhs);
  • D. Change Widget (Gizmo& g) ; to Widget (const Gizmo& g);
  • E. Use const_cast during assignment otherWidget = basicWidget;
  • A72. AD
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 #include <iostream>
 9 
10 struct Gizmo {
11     int pressure, rpm;
12     Gizmo(int p, int r) : pressure(p), rpm(r) {}
13 //    Gizmo(const int p, const int r) : pressure(p), rpm(r) {}
14 };
15 
16 struct Widget {
17     int temp, speed;
18     Widget() : temp (68), speed(100) {}
19     
20 //    Widget(Widget& w);
21     Widget(const Widget& w);
22     
23     Widget& operator=(Widget& rhs);
24 //    Widget& operator=(const Widget& rhs);
25 
26 //    Widget(Gizmo& g);
27     Widget(const Gizmo& g);
28 };
29 
30 int main(int argc, char **argv)
31 {
32     const Widget prototype;
33 
34     Widget basicWidget = prototype; // No matching constructor for initialization of 'Widget'
35 
36     Widget otherWidget;
37     otherWidget = basicWidget;
38 
39     const Gizmo gadget(10, 100);
40 
41     Widget thirdWidget(gadget); // No matching constructor for initialization of 'Widget'
42 
43     return 1;
44 }
View Code

  • Q73. Which of following actions are required to achieve the specifications of the C++ class which will contain some special purpose "helper" functionality with the requirements specified below?
    • 1) The functionality is used by more than one public member function in a given class.
    • 2) The functionality accesses private data members of the class.
    • 3) The functionality must be a separate function.
    • 4) The functionality must be available to derived classes but not available to users of the derived directly.
  • A. Put the functionality in a member function and make its access level private.
  • B. Put the functionality in a friend function of the given class.
  • C. Put the functionality in a member function and make its access level protected.
  • D. Put the functionality in a separate class and inherit from that class.
  • E. Put the functionality in a global function that is called by the class.
  • A73. B ?

  • Q74. In the C++ class Box below, the == operator is overloaded such that boxes of the same volume are considered "equal". Given the two boxes of this type(called box1 and box2) comparisons 1 and 2 may be made. Which can be used to implement the 3rd (comparison 3) shown below?
  • A. Implement a member operator== taking a Box and a double.
  • B. Implement a global function called operator== which takes a double and a Box and makes it a friend of class Box.
  • C. Modify one of the existing member operator== implementations so that it takes an additional parameter.
  • D. The existing overloaded operators already handle the 3rd comparison.
  • E. Implement a global functino called operator== which takes a double and a Box and have it call an existing member operator== implementation with conforming parameters.
  • A74. B 
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 //
 9 //  main.cpp
10 //  LeetCode
11 //
12 //  Created by Hao on 2017/3/16.
13 //  Copyright © 2017年 Hao. All rights reserved.
14 //
15 #include <iostream>
16 using namespace std;
17 
18 class Box {
19     double  volume;
20 public:
21     Box()   {};
22     Box(double v) : volume(v)   {};
23     
24     bool operator==(Box &rhs) {
25         return this->volume == rhs.volume;
26     }
27     
28     bool operator==(double v) {
29         Box rhs(v);
30         return volume == rhs.volume;
31     }
32     /*
33      bool operator==(Box &rhs, double v) { // Overloaded 'operator==' must be a binary operator (has 3 parameters)
34      Box temp(v);
35      return rhs.volume == temp.volume;
36      }
37      */
38     friend bool cmp(double v, Box &rhs); // must declared w/ the parameter nameb98
39 };
40 
41 bool cmp(double v, Box &rhs) {
42     Box temp(v);
43     return rhs.volume == temp.volume;
44 }
45 
46 int main(int argc, char **argv)
47 {
48     Box box1, box2(8.75);
49     
50     if (box1 == 7.25)   // comparison 1
51         cout << 1 << endl;
52     
53     if (box1 == box2)   // comparison 2
54         cout << 2 << endl;
55 
56 /*
57     // Invalid operands to binary expression ('double' and 'Box')
58     if (8.75 == box2)   // comparison 3
59         cout << 3 << endl;;
60 */
61     
62     if (cmp(8.75, box2))
63         cout << 3 << endl;
64     
65     return 0;
66 }
View Code

  • Q75. What's the situation used for std::unexpected()?
  • A75. ?

原文地址:https://www.cnblogs.com/pegasus923/p/8465745.html