天天看點

新加坡程式員考題一則

新加坡程式員考題一則

考題原文:

Problem statement

You must work out a super string class, String, that is derived from the C++ standard class string. This derived String class must add two new member functions

1; a pattern recognition function that returns the number of occurrences of a string pattern in a string. This function must use operator overloading of the / (division) operator.

2; a function get_token that returns tokens extracted from the string

A token is a substring occurring between space characters in the string or between space characters and the end of the string. The string " aaa bbb cc " has the tokens "aaa", "bbb", and "cc" . When the function is called the first time, it must return "aaa", the next time "bbb", and then "cc". When it is called the 4th time it must return an empty string, and when it is called the 5th time it starts all over returning "aaa". Optionally, you may extend the solution so that tokens may be separated by any character out of a set of character given as a string argument.

新加坡程式員考題一則

參考譯文:

問題描述:

你必須建立一個功能強大的串處理類,String,這個類必須從 C++ 中标準的 string 類派生,必須在該派生類中添加兩個成員函數:

1、模式識别函數,傳回某個串中指定串模式的出現次數。該函數必須使用 / (除法)運算符重載。

2、用函數 get_token 從串中吸取某個記号并傳回該記号。

記号指字元串的一個子串,它位于字元串的空格之間,或者位于空格和串尾之間。如:字元串“ aaa bbb cc ”中的記号有“aaa”、“bbb”和“cc”。當第一次調用該函數時,它必須傳回“aaa”,下次再調用時傳回“bbb”,第三次調用時傳回“cc”,依此類推。當第四次調用該函數時,它必須傳回一個空串,最後當第五次調用它時,傳回結果又從 “aaa” 開始。你可以随意擴充這個解決方案,讓記号可以用某組字元以外的任何字元分割,這組字元可以作為一個串參數傳遞。