source codephp
https://github.com/haotang923/interview/blob/master/interview%20summary%20of%20C%20and%20CPP/html
學習筆記之IKM C++ 11 - 浩然119 - 博客園java
學習筆記之100 TOP Ikm C++ Online Test Questions - 浩然119 - 博客園node
面試總結之指針 - 浩然119 - 博客園linux
C structure,數據結構裏有inter,char,float時,數據的內存佈局會是怎樣ios
爲何會有這種對齊c++
C pointer,指向數據結構與指向char的指針有區別嗎git
函數指針,什麼是函數指針,有什麼用處github
設計一個函數,函數中有一段功能是對相關數據的結理,但具體的處理方式是不定的。面試
如何對消息實現同步響應
struct和union的區別:
struct和class的區別:
Structures | Classes | Unions |
---|---|---|
class key is struct |
class key is class | class key is union |
Default access is public | Default access is private | Default access is public |
No usage constraints | No usage constraints | Use only one member at a time |
Default inheritance is public | Default inheritance is private | - |
Could not use Template | Could use Template | - |
static關鍵字至少有下列n個做用:
const關鍵字至少有下列n個做用:
#include 「filename.h」和#include <filename.h>的區別
用C語言,將一個數字乘以7倍的效率最快的方法是什麼?
C++
變量的內存分區
size_t_百度百科
什麼是深淺拷貝?
短小而被頻繁調用的程序如何處理?
指針和引用的初始化區別
delete數組指針,只delete第一個後果
什麼是拷貝構造函數?
要在C++ 防止對象被複制,有什麼方法
虛析構函數
函數對象功能
C++虛擬機制
抽象類能被實例化嗎
Virtual:虛函數:派生類能夠覆蓋掉的函數,純虛函數:只是個空函數,沒有函數實現體。
C++如何實現JAVA接口
Object Slicing
異常,異常的功能
C++模板
STL containers內部實現的數據結構
list v.s. vector
智能指針
RAII
RTTI
volatile
大家要的C++面試題答案來了--基礎篇
What will be printed, and why?
1 class A 2 { 3 public: 4 A () : data (0) { SetData (); printf ("data=%d", data); } 5 virtual void SetData () { data = 1; } 6 protected: 7 int data; 8 }; 9 10 class B : public A 11 { 12 public: 13 B () {} 14 virtual void SetData () { data = 2; } 15 }; 16 17 int main(int argc, char* argv[]) 18 { 19 B b; 20 return 0; 21 }
What's wrong, and how to fix?
1 class TryConst 2 { 3 public: 4 TryConst () {} 5 private: 6 const int aaa; 7 };
What's wrong, and how to fix?
1 class TryStatic 2 { 3 public: 4 TryStatic () : aa (0) {} 5 private: 6 static int aa; 7 };
What will be printed, and why?
1 class TestSize1 2 { 3 public: 4 TestSize1 () : a (0) {} 5 virtual void F () = 0; 6 private: 7 int a; 8 }; 9 class TestSize2 : public TestSize1 10 { 11 public: 12 TestSize2 () : b (1) {} 13 virtual void F () { b = 3; } 14 private: 15 int b; 16 }; 17 int main(int argc, char* argv[]) 18 { 19 printf ("size of TestSize2 = %d", sizeof (TestSize2)); 20 return 0; 21 }
What's wrong, and how to fix?
1 class P1 2 { 3 public: 4 P1 () { p = new char [10]; } 5 ~P1 () { delete [] p; } 6 private: 7 char * p; 8 }; 9 class P2 : public P1 10 { 11 public: 12 P2 () { q = new char [20]; } 13 ~P2 () { delete [] q; } 14 private: 15 char * q; 16 }; 17 18 int main(int argc, char* argv[]) 19 { 20 P1 * pp = new P2; 21 ... 22 delete pp; 23 return 0; 24 }
What's wrong, and how to fix?
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 Thing { 12 public: 13 void doSomething() { cout << __func__ << endl; } 14 Thing* next; 15 }; 16 17 class Things { 18 public: 19 Things(Thing *myThing) : head(myThing), current(myThing) {} 20 21 Thing* First() { 22 return head; 23 } 24 25 Thing* Next() { 26 current = current->next; 27 return current; 28 } 29 30 bool OK() { 31 if (current != nullptr) 32 return true; 33 else 34 return false; 35 } 36 37 private: 38 Thing *head, *current; 39 }; 40 41 int main() 42 { 43 Thing* myThing = new Thing; 44 Things myThings(myThing); 45 46 // This is how we do with class Things 47 /* 48 doSomething 49 */ 50 for (Thing *ptr = myThings.First(); myThings.OK(); myThings.Next()) { 51 ptr->doSomething(); 52 } 53 54 return 0; 55 }
strcpy,使用strcpy需注意什麼,爲何,有什麼更安全的函數
1 #include <assert.h> 2 3 // 爲了實現鏈式操做,將目的地址返回 4 char * strcpy(char *strDest, const char *strSrc) 5 { 6 // 對原地址和目的地址加非0斷言 7 assert(strDest != NULL && strSrc != NULL); 8 9 char *address = strDest; 10 11 while ((*strDest++ = *strSrc++) != '\0'); 12 13 return address; 14 }
strlen
1 #include <assert.h> 2 3 // 入參const 4 int strlen(const char *str) 5 { 6 // 斷言字符串地址非0 7 assert(str != NULL); 8 9 int len = 0; 10 11 while ((*str++) != '\0') len ++; 12 13 return len; 14 }
struct數組定義和初始化,以及for循環第一次循環會執行哪些語句,比較tricky,注意i--語句執行前後順序。
1 // 2 // main.c 3 // LeetCode 4 // 5 // Created by Hao on 2017/11/22. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <stdio.h> 10 11 // Calculate the number of elements in x 12 #define myMacro(x) sizeof((x)) / sizeof((x[0])) 13 14 // Definition and initialization of array of struct 15 struct structTest { 16 int digit; 17 char *number; 18 } strTest[] = { {1, "one"}, {2, "two"}, 19 {3, "three"}, {4, "four"}, 20 {5, "five"}, {6, "six"}, 21 {7, "seven"}, {8, "eight"}, 22 {9, "nine"}, {10, "ten"} }; 23 24 int getNumber1(char *number) 25 { 26 int i; 27 28 printf("Before for-loop : i = %d\n\n", i); 29 30 for (i = myMacro(strTest); i --;) // i -- will be executed at the BEGINNING of the first loop 31 { 32 printf("Loop i = %d\n", i); 33 printf("[%d : %s]\n", strTest[i].digit, strTest[i].number); 34 if (strcmp(strTest[i].number, number)) // return value 0 only if the contents of both strings are equal 35 { 36 printf("Before continue : %d\n", i); 37 continue; 38 } 39 printf("Before break : %d\n", i); 40 break; 41 } 42 43 printf("\nAfter for-loop : %d\n\n", i); 44 return i; 45 } 46 47 int getNumber2(char *number) 48 { 49 int i; 50 51 printf("Before for-loop : i = %d\n\n", i); 52 53 for (i = myMacro(strTest); ; i --) // i -- will be executed at the END of the first loop 54 { 55 printf("Loop i = %d\n", i); 56 printf("[%d : %s]\n", strTest[i].digit, strTest[i].number); 57 if (strcmp(strTest[i].number, number)) 58 { 59 printf("Before continue : %d\n", i); 60 continue; 61 } 62 printf("Before break : %d\n", i); 63 break; 64 } 65 66 printf("\nAfter for-loop : %d\n\n", i); 67 return i; 68 } 69 70 int main() 71 { 72 printf("myMacro(strTest) = %d\n\n", myMacro(strTest)); 73 74 printf("getNumber1(\"zero\") = %d\n\n", getNumber1("zero")); 75 76 printf("getNumber1(\"nine\") = %d\n\n", getNumber1("nine")); 77 78 printf("getNumber2(\"zero\") = %d\n\n", getNumber2("zero")); 79 80 printf("getNumber2(\"nine\") = %d\n\n", getNumber2("nine")); 81 82 return 0; 83 }
myMacro(strTest) = 10 Before for-loop : i = 0 Loop i = 9 [10 : ten] Before continue : 9 Loop i = 8 [9 : nine] Before continue : 8 Loop i = 7 [8 : eight] Before continue : 7 Loop i = 6 [7 : seven] Before continue : 6 Loop i = 5 [6 : six] Before continue : 5 Loop i = 4 [5 : five] Before continue : 4 Loop i = 3 [4 : four] Before continue : 3 Loop i = 2 [3 : three] Before continue : 2 Loop i = 1 [2 : two] Before continue : 1 Loop i = 0 [1 : one] Before continue : 0 After for-loop : -1 getNumber1("zero") = -1 Before for-loop : i = 0 Loop i = 9 [10 : ten] Before continue : 9 Loop i = 8 [9 : nine] Before break : 8 After for-loop : 8 getNumber1("nine") = 8 Before for-loop : i = 0 Loop i = 10 [0 : (null)] (lldb)
編寫類String的構造函數、析構函數、拷貝構造函數和賦值函數。
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class String { 6 public : 7 String (const char *str = NULL); // default initial value 8 String (const String &other); 9 ~ String (void); 10 String & operator=(const String &other); 11 friend ostream &operator<<(ostream &os, const String &str); 12 friend istream &operator>>(istream &is, String &str); 13 private : 14 char *m_data; 15 }; 16 17 // 普通構造函數 18 String::String(const char *str) 19 { 20 if (str == NULL) { // empty string 21 m_data = new char[1]; 22 *m_data = '\0'; 23 } else { 24 int length = strlen(str); 25 m_data = new char[length + 1]; 26 strcpy(m_data, str); 27 } 28 } 29 30 // 析構函數 31 String::~String(void) 32 { 33 delete[] m_data; 34 } 35 36 // 拷貝構造函數 37 String::String(const String &other) 38 { 39 int length = strlen(other.m_data); 40 m_data = new char[length + 1]; 41 strcpy(m_data, other.m_data); 42 } 43 44 // 賦值函數 45 String & String::String::operator=(const String &other) 46 { 47 // 檢查自賦值 48 if (this == &other) 49 return *this; 50 51 // 釋放原有的內存資源 52 delete[] m_data; 53 54 int length = strlen(other.m_data); 55 m_data = new char[length + 1]; 56 strcpy(m_data, other.m_data); 57 58 return *this; 59 } 60 61 ostream &operator<<(ostream &os, const String &str) 62 { 63 os << str.m_data; 64 65 return os; 66 } 67 68 istream &operator>>(istream &is, String &str) 69 { 70 char *sTemp = new char[1000]; 71 72 is >> sTemp; 73 74 // Must check if input succeeds 75 if (is) { 76 delete[] str.m_data; 77 78 int length = strlen(sTemp); 79 str.m_data = new char[length + 1]; 80 strcpy(str.m_data, sTemp); 81 } else 82 str = String(); // if fail, set to the default value 83 84 delete[] sTemp; 85 86 return is; 87 } 88 89 int main() 90 { 91 String s1; 92 String s2("Test"); 93 String s3 = s2; 94 95 cout << s1 << endl; 96 97 s1 = s2; 98 99 cout << s1 << endl; 100 101 cin >> s1; 102 103 cout << s1 << endl; 104 cout << s2 << endl; 105 cout << s3 << endl; 106 107 return 0; 108 }
刪除字符串的首尾多餘空格
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 <cstddef> // std::size_t 12 using namespace std; 13 14 class Solution 15 { 16 public: 17 void TrimSpaces(string& ioStr) 18 { 19 size_t startPos = ioStr.find_first_not_of(" "); 20 size_t endPos = ioStr.find_last_not_of(" "); 21 22 cout << startPos << "\n" << endPos << endl; 23 24 // npos is a static member constant value with the greatest possible value for an element of type size_t. 25 // This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type. 26 if (startPos == string::npos) 27 startPos = 0; 28 if (endPos == string::npos) 29 endPos = 0; 30 31 cout << startPos << "\n" << endPos << endl; 32 33 ioStr = ioStr.substr(startPos, endPos - startPos + 1); 34 } 35 }; 36 37 int main () 38 { 39 Solution testSolution; 40 41 string str[5] = {" abc ", "abc ", " abc", " ", ""}; 42 43 for (auto i = 0; i < 5; i ++) 44 { 45 testSolution.TrimSpaces(str[i]); 46 cout << "\"" << str[i] << "\"" << "\n" << endl; 47 } 48 49 return 0; 50 }
4 4 "abc" 2 2 "abc" 6 6 "abc" 18446744073709551615 0 " " 18446744073709551615 0 "" Program ended with exit code: 0
輸出Fibonacci數列的第N項
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 Solution 13 { 14 public: 15 int Fibonacci(const int& n) 16 { 17 if (n == 0) 18 return 0; 19 else if (n == 1) 20 return 1; 21 else { 22 int t, f0 = 0, f1 = 1; 23 24 for (int i = 2; i < n + 1; i ++) 25 { 26 t = f1; 27 f1 = f0 + f1; 28 f0 = t; 29 } 30 31 return f1; 32 } 33 } 34 35 }; 36 37 int main(int argc, char* argv[]) 38 { 39 Solution testSolution; 40 41 for (auto i = 0; i < 10; i ++) 42 cout << testSolution.Fibonacci(i) << endl; 43 44 return 0; 45 }
0 1 1 2 3 5 8 13 21 34 Program ended with exit code: 0
判斷一個字符串是否是迴文串
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 <algorithm> // reverse 12 #include <cctype> // toupper, isalnum 13 using namespace std; 14 15 class Solution 16 { 17 public: 18 // Reverse the whole string 19 int isPalindrome(const string& str) 20 { 21 if (str.empty()) return false; 22 23 string sTemp = str; 24 25 cout << sTemp << endl; 26 27 // STL algorithm 28 reverse(sTemp.begin(), sTemp.end()); 29 30 cout << sTemp << endl; 31 32 return (sTemp == str); 33 } 34 35 // Considering only alphanumeric characters and ignoring cases 36 int isPalindrome2(const string& str) 37 { 38 if (str.empty()) return false; 39 40 size_t start = 0, end = str.size() - 1; 41 42 while (start < end) 43 { 44 if (! isalnum(str.at(start))) 45 ++ start; 46 else if (! isalnum(str.at(end))) 47 -- end; 48 else if (toupper(str.at(start)) != toupper(str.at(end))) 49 return false; 50 else { 51 ++ start; 52 -- end; 53 } 54 } 55 56 return true; 57 } 58 }; 59 60 int main(int argc, char* argv[]) 61 { 62 Solution testSolution; 63 64 auto words = {"A man, a plan, a canal: Panama", "race a car", "", "a b a"}; 65 66 for (auto word : words) 67 cout << testSolution.isPalindrome(word) << endl; 68 69 cout << endl; 70 71 for (auto word : words) 72 cout << testSolution.isPalindrome2(word) << endl; 73 74 return 0; 75 }
A man, a plan, a canal: Panama amanaP :lanac a ,nalp a ,nam A 0 race a car rac a ecar 0 0 a b a a b a 1 1 0 0 1 Program ended with exit code: 0
判斷一個數是否是10的冪
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 Solution 13 { 14 public: 15 bool isPowerOfTen(int n) 16 { 17 if (n < 1) 18 return false; 19 20 while (n % 10 == 0) { 21 n /= 10; 22 } 23 24 return n == 1; 25 } 26 }; 27 28 int main(int argc, char* argv[]) 29 { 30 Solution testSolution; 31 32 int result = 1; 33 34 result = result && testSolution.isPowerOfTen(1); 35 result = result && testSolution.isPowerOfTen(10); 36 result = result && testSolution.isPowerOfTen(10000000); 37 result = result && !testSolution.isPowerOfTen(0); 38 result = result && !testSolution.isPowerOfTen(-1); 39 result = result && !testSolution.isPowerOfTen(110); 40 41 if (result) 42 cout << "ALl tests pass!" << endl; 43 44 return 0; 45 }
找出最長重複字符子串
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 <stdio.h> 10 #include <string.h> 11 12 typedef struct substr { 13 int start, length; 14 } substr; 15 16 substr longest_uniform_substring(char * input) 17 { 18 if (input == nullptr || strlen(input) == 0) 19 return (substr){ -1, 0 }; 20 21 int st = 0, len = 1, resSt = 0, maxLen = 1, cur = 0; 22 char cTemp = *input; 23 24 ++ input; 25 cur += 1; 26 27 while (*input != '\0') // pay attention to the terminator '\0' 28 { 29 if (*input == cTemp) { 30 len += 1; 31 } else { 32 cTemp = *input; 33 st = cur; 34 len = 1; 35 } 36 37 if (len > maxLen) { 38 resSt = st; 39 maxLen = len; 40 } 41 42 ++ input; 43 cur += 1; 44 } 45 46 return (substr){ resSt, maxLen }; 47 } 48 49 int main(int argc, char* argv[]) 50 { 51 int result = 1; 52 53 substr test = longest_uniform_substring(""); 54 result = result && (test.start == -1 && test.length == 0); 55 56 test = longest_uniform_substring("10000111"); 57 result = result && (test.start == 1 && test.length == 4); 58 59 test = longest_uniform_substring("aabbbbbCdAA"); 60 result = result && (test.start == 2 && test.length == 5); 61 62 if (result) 63 printf("ALl tests pass!\n"); 64 65 return 0; 66 }
按照機率輸出determined rate result 01,例如rate = 0.25,00010001。
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 <cmath> 11 using namespace std; 12 13 class Rate { 14 public: 15 Rate(double rate) : m_rate(rate), m_total(rate) {} // inialization 16 17 bool keep() { 18 // cout << m_total << " "; 19 20 if (m_total < 1 - 1e-10) { // pay attention to the double precision control. Actually 1 is 0.999...9 in memory. 21 m_total += m_rate; 22 return false; 23 } else { // reset the rate 24 m_total = m_total - 1 + m_rate; 25 return true; 26 } 27 } 28 29 private: 30 double m_rate, m_total; 31 }; 32 33 int main() 34 { 35 vector<Rate> myRates{Rate(0.3), Rate(0.25)}; 36 37 /* 38 0 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 39 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 40 */ 41 for (auto & myRate : myRates) { 42 for (int i = 0; i < 20; i ++) { 43 if (myRate.keep()) 44 cout << "1 "; 45 else 46 cout << "0 "; 47 } 48 cout << endl; 49 } 50 51 return 0; 52 }
Write a program that creates a standard deck of cards, shuffles them and returns the top card from the deck. (This is a standalone program, you are not passed in any data.)
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 int main() 13 { 14 vector<int> cards; 15 int n = 54; 16 17 for (int i = 0; i < n; i ++) { 18 cards.push_back(i + 1); 19 } 20 21 for (int i = n - 1; i > 0; i --) { 22 int j = rand() % (i + 1); 23 swap(cards[i], cards[j]); 24 } 25 26 // STL for reference 27 // random_shuffle(cards.begin(), cards.end()); 28 29 /* 30 13 10 15 43 9 22 33 5 3 52 36 35 51 49 46 34 41 24 23 48 32 50 16 2 44 7 53 40 30 11 42 54 8 4 18 17 19 12 27 28 47 1 45 29 20 26 38 25 21 31 39 6 37 14 31 */ 32 for (auto card : cards) { 33 cout << card << " "; 34 } 35 36 return 0; 37 }
2019 C++開發工程師面試題大合集