1. 程式人生 > >C++: string的大小寫轉換

C++: string的大小寫轉換

      將一個string轉換成大寫或者小寫,是專案中經常需要做的事情,但string類裡並沒有提供這個方法。自己寫個函式來實現,說起來挺簡單,但做起來總讓人覺得不方便。打個比方:早上起來想吃個漢堡,冰箱裡有生牛肉,有面粉,也有微波爐,是可以自己做的,但是實在是太費事,沒幾個人願意做。但是,打個電話給肯德基宅急送,10分鐘後就有熱乎乎的漢堡送上門了,大大節省了時間(時間就是金錢,你可以將時間用在更重要的開發工作上),並且味道也不差,何樂而不為呢?

       STL的algorithm庫確實給我們提供了這樣的便利,使用模板函式transform可以輕鬆解決這個問題,開發人員只需要提供一個函式物件,例如將char轉成大寫的toupper函式或者小寫的函式tolower函式。

transform原型:

[cpp] view plaincopyprint?
  1. template < class InputIterator, class OutputIterator, class UnaryOperator >  
  2.   OutputIterator transform ( InputIterator first1, InputIterator last1,  
  3.                              OutputIterator result, UnaryOperator op );  
  4. template < class InputIterator1, 
    class InputIterator2,  
  5.            class OutputIterator, class BinaryOperator >  
  6.   OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,  
  7.                              InputIterator2 first2, OutputIterator result,  
  8.                              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 plaincopyprint?
  1. #include <string>
  2. #include <algorithm>
  3. usingnamespace std;  
  4. int main()  
  5. {  
  6.     string strA = "[email protected]";  
  7.     string strB = "[email protected]";  
  8.     printf("Before transform:\n");  
  9.     printf("strA:%s \n", strA.c_str());  
  10.     printf("strB:%s \n\n", strB.c_str());  
  11.     transform(strA.begin(), strA.end(), strA.begin(), ::toupper);  
  12.     transform(strB.begin(), strB.end(), strB.begin(), ::toupper);  
  13.     printf("After transform to toupper:\n");  
  14.     printf("strA:%s \n", strA.c_str());  
  15.     printf("strB:%s \n\n", strB.c_str());  
  16.     transform(strA.begin(), strA.end(), strA.begin(), ::tolower);  
  17.     transform(strB.begin(), strB.end(), strB.begin(), ::tolower);  
  18.     printf("After transform to lower:\n");  
  19.     printf("strA:%s \n", strA.c_str());  
  20.     printf("strB:%s \n\n", strB.c_str());  
  21.     return 0;  
  22. }  
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string strA = "[email protected]";
    string strB = "[email protected]";
    printf("Before transform:\n");
    printf("strA:%s \n", strA.c_str());
    printf("strB:%s \n\n", strB.c_str());

    transform(strA.begin(), strA.end(), strA.begin(), ::toupper);
    transform(strB.begin(), strB.end(), strB.begin(), ::toupper);
    printf("After transform to toupper:\n");
    printf("strA:%s \n", strA.c_str());
    printf("strB:%s \n\n", strB.c_str());

    transform(strA.begin(), strA.end(), strA.begin(), ::tolower);
    transform(strB.begin(), strB.end(), strB.begin(), ::tolower);
    printf("After transform to lower:\n");
    printf("strA:%s \n", strA.c_str());
    printf("strB:%s \n\n", strB.c_str());
    return 0;
}

執行結果:

[cpp] view plaincopyprint?
  1. strA:[email protected]   
  2. strB:[email protected]   
  3. After transform to toupper:  
  4. strA:[email protected]   
  5. strB:[email protected]   
  6. After transform to lower:  
  7. strA:[email protected]   
  8. strB:[email protected]   
strA:[email protected] 
strB:[email protected] 

After transform to toupper:
strA:[email protected] 
strB:[email protected] 

After transform to lower:
strA:[email protected] 
strB:[email protected] 

最後補一句:STL algorithm功能實在是太強勁了,非常推薦。

相關推薦

C++ string大小寫轉換

    但在使用g++編譯時會報錯: 對 ‘transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char>

golang語言漸入佳境[24]-string-大小寫轉換類函式

string-大小寫轉換類函式 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 pa

c++字母大小寫轉換

今天突然有了點空閒時間,又剛好看到一個需要轉換字母大小寫的問題,於是就來這裡聊一下對於一個字母,要將其轉換為小寫或是大寫,首先要判斷其本身為大寫還是小寫.在這裡我們可以使用ascii進行判斷#include<iostream> int main(void){ c

string大小寫轉換

利用transform函式轉換,包含在algorithm標頭檔案中#include<iostream> #include<algorithm> using namespace std; int main(){ string s="Hello W

C++ string字元中英文字母大小寫轉換

c++中的string類物件並沒有自帶的方法進行字元大小寫轉換,進行大小寫轉換的方法很多,這裡我們提供一個通過algorithm中的transform函式對string物件進行字元的大小寫轉換。 #include <iostream> #include <string&g

水題 練習一下C++string大小寫轉換函式

題目描述: 寫出一個程式,接受一個由字母和數字組成的字串,和一個字元,然後輸出輸入字串中含有該字元的個數。不區分大小寫。 輸入描述: 輸入一個有字母和數字以及空格組成的字串,和一個字元。 輸出描述: 輸出輸入字串中含有該字元的個數。 輸入樣例: ABCDEF A 輸出

C++: string大小寫轉換

      將一個string轉換成大寫或者小寫,是專案中經常需要做的事情,但string類裡並沒有提供這個方法。自己寫個函式來實現,說起來挺簡單,但做起來總讓人覺得不方便。打個比方:早上起來想吃個漢堡,冰箱裡有生牛肉,有面粉,也有微波爐,是可以自己做的,但是實在是太費事,

C# String與Byte數組的轉換

pan style clas -h 數組 ets div system logs string轉byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); byte[] byteArray

Python 字符串操作(string替換、刪除、截取、復制、連接、比較、查找、包含、大小寫轉換、分割等)

brk 分割 掃描 char 去空格 之前 特殊符號 strip () 去空格及特殊符號 s.strip().lstrip().rstrip(‘,‘) 復制字符串 #strcpy(sStr1,sStr2) sStr1 = ‘strcpy‘ sStr2 = sStr1

C# 無視大小寫比價字符串以及字符串大小寫轉換

== 對比 pos body con 大小寫 gpo clas 大小寫轉換 //C#通過ToUpper()方法將字符串轉換成大寫,代碼如下: string sentence= "this is in upper case."; Console.WriteLine(s

C語言 字符串大小寫轉換 自定義函數

pos 字符串 轉換 strlen 大小寫 std per include tolower #include <stdio.h>#include <stdlib.h>#include <string.h>char * strtolower

C++ 十進制轉二進制 ASCII碼大小寫轉換

一個 names big 大小寫 按位與 inf 討論 字母轉 style 參考博客:<C++>十進制數轉換成二進制顯示 由於我要實現的功能局限於char類型,所以我根據參考寫了一個。 1 #include <iostream> 2 usi

C語言 大小寫字母轉換

inf pan () .cn com clas www 轉換 .com //凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/ 方法1: 1 #include<stdio.h> 2 #include<stdli

C++】int轉換string的兩種方法(to_string、字串流)轉載

int轉換成string的兩種方法 第一種是to_string函式,這是C++11新增的,使用非常方便,簡單查了下:C++11標準增加了全域性函式std::to_string,以及std::stoi/stol/stoll等等函式(這幾個就是string轉int,long,以及long lo

c++ string、int相互轉換

採用sstream中定義的字串流物件來實現 1> int轉string #include<sstream> int i = 12; ostringstream os; //構造一個輸出字串流,流內容為空 os << i; //向輸出字串流中輸出int

C語言(大小寫轉換

數字改成大寫 #include<stdio.h> //標頭檔案 int main () { int num; //定義變數 char big[10][4]={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"}; int i=0;

C++中std::stringC-String字元陣列的互相轉換

C語言中只有字元陣列這一說法,沒有C++專門封裝的字串類std::string。而字元陣列C-String以\0作為結束符。std::string其實還是儲存了C-String這個指標,只不過不同的編譯期對std::string中的儲存結構都做了不同的處理,這裡我們不討論std::str

Boost中string大小寫轉換用法

標頭檔案 boost/algorithm/string/case_conv.hpp 作用 主要有如下API to_lower_copy:將原來字串,轉換為小寫字串,並返回新的字串,原來字串不改變。 to_upper_copy:將原來字串,轉換為大寫字串,並返回新的字串,原來字串不

2Python 字串操作(string替換、刪除、擷取、複製、連線、比較、查詢、包含、大小寫轉換、分割等)

去空格及特殊符號 s.strip().lstrip().rstrip(',') 複製字串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 連線字串 #strcat(

C#資料型別轉換string到int型陣列

已知: string str = "1,2,3,4,5" 問: 如何根據上述字串產生一個int[]陣列?(int[] intLst = {1,2,3,4,5}) 答: 首先:string strLst = str.Split( ',' )  然後:int[] int