Re: [討論] 面試有鑑別度的問題??
※ 引述《PkmX (阿貓)》之銘言:
: 再來給幾個題目好了XD
: 1. 請解釋標準容器的allocator的用途?
: (加分題:allocator中的rebind template是幹嘛用的?)
allocator宣告時伴隨著template參數Type,使得他預設為建立該type的空間。
如果要使用同ㄧ個allocator來建立其他大小的空間,就要使用rebind。
: 2. 何時會用到operator->*?
使用member function pointer時。需要傳入該物件的value。
struct A {void f(int){}};
A::* pointer = &A::f;
A obj;
A *objptr = &obj;
(obj->*pointer)(10);
: 3. 請解釋std::bind如何使用以及用途為何?
將function pointer與數值、member function pointer與object與數值、
或是functor與數值互相結合,產生新的functor。
可以指定每個參數是固定值,或是產生的新functor所需的參數。
: 4. 承上題,請舉例如何用std::bind將參數綁定在一個member function上。
struct X {void f(int){}};
X obj;
auto func = std::bind(&X::f, &obj, std::placeholders::_1);
func(10);
: 5. 承上題,請解釋std::ref和std::cref的用途為何?
reference與const reference會在template resolve的過程中被去除,所以需要
明顯的宣告為reference與const reference。
使用方式是std::ref把變數包起來:std::ref(x)
: 6. 在template的宣告中,class和typename通常是可以互換的,
: 但在某個情況下只能使用class這個keyword而不能使用typename。
當template的參數是template class時就只能使用class。如:
template <template <typename X> class T> // 編譯成功
template <template <typename X> typename T> // 編譯失敗
: 7. 請解釋input/output/forward/bidirectional/random access iterator的差異為何,
: 他們各支援哪些operation?
input:傳回rvalue
output:傳回lvalue
forward:支援++
bidirectional:支援++與--
random access:支援+n與-n
另外,
std::advance與std::distance提供bidirectional的iterator
前進n與相減的功能
: 8. std::unordered_{set,map}和std::{set,map}有何差別?
std::unordered_{set,map}
使用hash來儲存資料。
使用std::hash來產生hash值。
這使得key type需要有對應的std::hash的partial specialization
std::{set,map}
使用紅黑二元樹來儲存資料。
這使得key要能互相比大小,也就是要有operator<
: 9. 請舉例如何使用variadic template與template aliases,並實現std::tuple。
參考libstdc++(遮臉
http://ideone.com/cfYwY8
: 10. 請解釋何謂copy-and-swap idiom。
為了在multithread中達到資料一致性,在改寫一個data pack時,使用三步驟:
1.複製一份原有資料
2.改寫複製後的內容
3.與原有資料atomic交換
好處是減少locking時間,壞處是會造成許多copy paste
最好要搭配memory pool使用
: 11. 為何T** -> const T**不合法與derived** -> base**不合法?
T** 的值型態是 T*
const T** 的值型態是 const T*
後者的const是去修飾T而不是T*
要是T** -> T* const *才合法
後者同理
: 12. 請解釋以下的這個&是幹嘛用的,可以幫助我們避免什麼錯誤?
: auto foo::operator=(const foo&) & -> foo&
reference-qualified functions
由編譯器幫你檢查:
& 可以保證參數是lvalue reference
&& 可以保證參數是rvalue reference
: ^
: 13. 試問以下的lambda為何不合法?要加入什麼才正確?
: int i = 0;
: auto l = [=](){ ++i; };
: (大家有沒有發現littleshan大大的第二題其實不只一個答案XD)
i要在lambda前方的定義區塊內指定為reference傳入才可更改。
auto l = [=,&i](){ ++i; };
: 14. 請寫一個scopeguard,達到以下的使用方式:
: {
: auto sg1 = scopeguard([](){ std::cout << "1"; });
: auto sg2 = scopeguard([](){ std::cout << "2"; });
: auto sg3 = std::move(sg1); // sg3 takes sg1's responsibility
: // to print 1 on destruction
: sg2.dismiss(); // sg2 no longer prints 2 on destruction
: } // print "1" here
: 15. 承上題,請使用scopeguard實作出類似其他語言中finally的功能出來,例如:
: at_scope_exit([](){
: // ...
: });
: (提示:可使用許多編譯器提供的__COUNTER__ macro)
#define at_scope_exit(x) auto sg_ ## __COUNTER__ (x)
: 16. 試問decltype和std::declval的用途和用法為何?
: 17. boost::variant與boost::any有何差別?
: 18. boost::function與boost::any中有用到type erasure的技巧,請解釋?
: 19. 請用constexpr寫出在compile-time計算factorial的function。
// 修正過了囧
constexpr uint64_t fibb(uint64_t x) {
return (x == 0 || x == 1) ? 1 : fibb(x-1) + fibb(x-2);
}
: 20. 請解釋何謂facet?它與locale的關係是?
: 加分題:有無使用其它boost、loki等函式庫的經驗?用在哪些地方?
題目太多慢慢寫orz
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.102.115
→
01/09 16:21, , 1F
01/09 16:21, 1F
→
01/09 16:23, , 2F
01/09 16:23, 2F
→
01/09 16:23, , 3F
01/09 16:23, 3F
→
01/09 16:24, , 4F
01/09 16:24, 4F
→
01/09 16:25, , 5F
01/09 16:25, 5F
→
01/09 16:26, , 6F
01/09 16:26, 6F
→
01/09 16:26, , 7F
01/09 16:26, 7F
→
01/09 16:27, , 8F
01/09 16:27, 8F
→
01/09 16:57, , 9F
01/09 16:57, 9F
→
01/09 16:59, , 10F
01/09 16:59, 10F
※ 編輯: ibmibmibm 來自: 140.123.102.115 (01/09 19:46)
→
01/10 14:33, , 11F
01/10 14:33, 11F
→
01/10 14:35, , 12F
01/10 14:35, 12F
→
01/14 09:17, , 13F
01/14 09:17, 13F
討論串 (同標題文章)
完整討論串 (本文為第 11 之 14 篇):