將乙個string轉換成大寫或者小寫,是專案中經常需要做的事情,但string類裡並沒有提供這個方法。自己寫個函式來實現,說起來挺簡單,但做起來總讓人覺得不方便。打個比方:早上起來想吃個漢堡,冰箱裡有生牛肉,有麵粉,也有微波爐,是可以自己做的,但是實在是太費事,沒幾個人願意做。但是,打個**給肯德基宅急送,10分鐘後就有熱乎乎的漢堡送上門了,大大節省了時間(時間就是金錢,你可以將時間用在更重要的開發工作上),並且味道也不差,何樂而不為呢?
stl的algorithm庫確實給我們提供了這樣的便利,使用模板函式transform可以輕鬆解決這個問題,開發人員只需要提供乙個函式物件,例如將char轉成大寫的toupper函式或者小寫的函式tolower函式。
transform原型:
[cpp]view plain
copy
print?
template
< class inputiterator, class outputiterator, class unaryoperator >
outputiterator transform ( inputiterator first1, inputiterator last1,
outputiterator result, unaryoperator op );
template
< class inputiterator1, class inputiterator2,
class outputiterator, class binaryoperator >
outputiterator transform ( inputiterator1 first1, inputiterator1 last1,
inputiterator2 first2, outputiterator result,
binaryoperator binary_op );
template < class inputiterator, class outputiterator, class unaryoperator >
outputiterator transform ( inputiterator first1, inputiterator last1,
outputiterator result, unaryoperator op );
template < class inputiterator1, class inputiterator2,
class outputiterator, class binaryoperator >
outputiterator transform ( inputiterator1 first1, inputiterator1 last1,
inputiterator2 first2, outputiterator result,
binaryoperator binary_op );
測試**:
[cpp]view plain
copy
print?
#include
#include
using
namespace std;
int main()
#include #include using namespace std;
int main()
執行結果:
[cpp]view plain
copy
print?
stra:yasaken@126.com
strb:lury@lenovo.com
after transform to toupper:
stra:yasaken@126.com
strb:lury@lenovo.com
after transform to lower:
stra:yasaken@126.com
strb:lury@lenovo.com
stra:yasaken@126.com
strb:lury@lenovo.com
after transform to toupper:
stra:yasaken@126.com
strb:lury@lenovo.com
after transform to lower:
stra:yasaken@126.com
strb:lury@lenovo.com
C string大小寫轉換
include include include using namespace std int main q1 為什麼用 tolower 呢?經過查閱c 標準庫 一 二 我得到了結果,tolower和toupper 分別在兩個地方定義了。乙個是 std tolower 乙個是在 cctype中定義的...
大小寫轉換
小寫數值轉大寫 xieshuxu 傳入轉換字串 傳入整數單位 如 元 傳入小數點後一位單位 如 角 傳入小數點後兩位單位 如 分 public string xiaotoda string xiao,string one,string two,string tree if xiao.indexof ...
大小寫轉換
problem description x現在要學習英文以及各種稀奇古怪的字元的了。現在他想把一串字元中的小寫字母變成大寫字元,大寫字母變成小寫字母,其他的保持不變。input 輸入有多組。每組輸入乙個字串,長度不大於80,不包含空格。output 輸出轉換後的字串 sample input a b...