在此利用STL的transform配合toupper/tolower,完成std::string轉換大(小)寫的功能,也看到Generics的威力,一個transform function,可以適用於任何型別,且只要自己提供Algorithm,就可完成任何Transform的動作。
string s1; // 初始化一个空字符串 string s2 = s1; // 初始化s2,并用s1初始化 string s3(s2); // 作用同上 string s4 = "hello world"; // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中 string s5("hello world"); // 作用同上 string s6(6,'a'); // 初始化s6为:...
2.tolower()函数: 程序代码: #include<iostream> using namespace std; int main(){ char a[] = "woAiX"; for(int i=0;i<5;i++){ a[i]=tolower(a[i]); } printf("%s",a); return 0; } 运行结果:
这是字符串匹配中经常需要做的事情,然而C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写(toupper)和小写(tolower)的功能而已。 但我们可以利用STL的transform配合toupper/tolower,完成std::string转换大(小)写的功能,也看到 模版编程 的威力了,一个transform函数,可以...
#include <string> int main() { std::string line; // empty string while(std::getline(std::cin, line)) { 1. 2. 3. 4. 5. 6. 7. // read line at time until end-of-file std::cout << line << std::endl; // write s to the output ...
2、string类也可以自己手写两个转化为大写和小写transform()方法,如下所示: #include <iostream> #include <algorithm> #include <cstring> using namespace std; void mytolower(string& s){ int len=s.size(); for(int i=0;i<len;i++){
1、tolower()函数。inttolower(intc);2、toupper()函数 inttoupper(intc);例:include<stdio.h> include<ctype.h> intmain(void){ charch='A';ch=tolower(ch);printf("ch=%c\n",ch);ch='b';ch=toupper(ch);printf("ch=%c\n",ch);return0;} ...
1 #include <string> 2 using namespace std; string对象的输入方式: cin\getline 1 #include <iostream> 2 #include <string> 3 4 int main() 5 { 6 string s1, s2; 7 cin >> s1; 8 getline(cin, s2); 9 10 return 0; 11 } 二、C字符串相关操作 ...
1.string可以直接进行比较: 像是>, >=, <, <=, ==, != 这些字符,都可以直接使用。 使用原理就是从前往后,每个字符都比较其ASCII码值的大小,直至上面的字符成立或者比较完了也不成立。 与string.h的strcmp函数类似,不过更方便 例如: #include<iostream> #include<string> using namespace std; int main...
{string[i] = tolower(string[i]); } printf("%s\n",string);system("pause"); } c++程序例:include <iostream> include <string> include <cctype> using namespace std;int main(){ string str= "THIS IS A STRING";for (int i=0; i <str.size(); i++) { str[i] = to...