查看“C++ string”的源代码
←
C++ string
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
===int转string=== std::string str = std::to_string(num); ===string比较=== str1.compare(str2); ===basic_string::find=== 向前搜索字符串,搜索与指定字符序列匹配的第一个子字符串。 <pre> // basic_string_find.cpp // compile with: /EHsc #include <string> #include <iostream> int main( ) { using namespace std; // The first member function // searches for a single character in a string string str1 ( "Hello Everyone" ); cout << "The original string str1 is: " << str1 << endl; basic_string <char>::size_type indexCh1a, indexCh1b; indexCh1a = str1.find ( "e" , 3 ); if (indexCh1a != string::npos ) cout << "The index of the 1st 'e' found after the 3rd" << " position in str1 is: " << indexCh1a << endl; else cout << "The character 'e' was not found in str1 ." << endl; indexCh1b = str1.find ( "x" ); if (indexCh1b != string::npos ) cout << "The index of the 'x' found in str1 is: " << indexCh1b << endl << endl; else cout << "The Character 'x' was not found in str1." << endl << endl; // The second member function searches a string // for a substring as specified by a C-string string str2 ( "Let me make this perfectly clear." ); cout << "The original string str2 is: " << str2 << endl; basic_string <char>::size_type indexCh2a, indexCh2b; const char *cstr2 = "perfect"; indexCh2a = str2.find ( cstr2 , 5 ); if ( indexCh2a != string::npos ) cout << "The index of the 1st element of 'perfect' " << "after\n the 5th position in str2 is: " << indexCh2a << endl; else cout << "The substring 'perfect' was not found in str2 ." << endl; const char *cstr2b = "imperfectly"; indexCh2b = str2.find ( cstr2b , 0 ); if (indexCh2b != string::npos ) cout << "The index of the 1st element of 'imperfect' " << "after\n the 5th position in str3 is: " << indexCh2b << endl; else cout << "The substring 'imperfect' was not found in str2 ." << endl << endl; // The third member function searches a string // for a substring as specified by a C-string string str3 ( "This is a sample string for this program" ); cout << "The original string str3 is: " << str3 << endl; basic_string <char>::size_type indexCh3a, indexCh3b; const char *cstr3a = "sample"; indexCh3a = str3.find ( cstr3a ); if ( indexCh3a != string::npos ) cout << "The index of the 1st element of sample " << "in str3 is: " << indexCh3a << endl; else cout << "The substring 'sample' was not found in str3 ." << endl; const char *cstr3b = "for"; indexCh3b = str3.find ( cstr3b , indexCh3a + 1 , 2 ); if (indexCh3b != string::npos ) cout << "The index of the next occurrence of 'for' is in " << "str3 begins at: " << indexCh3b << endl << endl; else cout << "There is no next occurrence of 'for' in str3 ." << endl << endl; // The fourth member function searches a string // for a substring as specified by a string string str4 ( "clearly this perfectly unclear." ); cout << "The original string str4 is: " << str4 << endl; basic_string <char>::size_type indexCh4a, indexCh4b; string str4a ( "clear" ); indexCh4a = str4.find ( str4a , 5 ); if ( indexCh4a != string::npos ) cout << "The index of the 1st element of 'clear' " << "after\n the 5th position in str4 is: " << indexCh4a << endl; else cout << "The substring 'clear' was not found in str4 ." << endl; string str4b ( "clear" ); indexCh4b = str4.find ( str4b ); if (indexCh4b != string::npos ) cout << "The index of the 1st element of 'clear' " << "in str4 is: " << indexCh4b << endl; else cout << "The substring 'clear' was not found in str4 ." << endl << endl; } </pre> <pre> The original string str1 is: Hello Everyone The index of the 1st 'e' found after the 3rd position in str1 is: 8 The Character 'x' was not found in str1. The original string str2 is: Let me make this perfectly clear. The index of the 1st element of 'perfect' after the 5th position in str2 is: 17 The substring 'imperfect' was not found in str2 . The original string str3 is: This is a sample string for this program The index of the 1st element of sample in str3 is: 10 The index of the next occurrence of 'for' is in str3 begins at: 24 The original string str4 is: clearly this perfectly unclear. The index of the 1st element of 'clear' after the 5th position in str4 is: 25 The index of the 1st element of 'clear' in str4 is: 0 </pre> ===basic_string::length=== ===basic_string::size=== 返回字符串中元素的当前数目。 <pre> // basic_string_length.cpp // compile with: /EHsc #include <string> #include <iostream> int main( ) { using namespace std; string str1 ("Hello world"); cout << "The original string str1 is: " << str1 << endl; // The size and length member functions differ in name only basic_string <char>::size_type sizeStr1, lenStr1; sizeStr1 = str1.size ( ); lenStr1 = str1.length ( ); basic_string <char>::size_type capStr1, max_sizeStr1; capStr1 = str1.capacity ( ); max_sizeStr1 = str1.max_size ( ); // Compare size, length, capacity & max_size of a string cout << "The current size of original string str1 is: " << sizeStr1 << "." << endl; cout << "The current length of original string str1 is: " << lenStr1 << "." << endl; cout << "The capacity of original string str1 is: " << capStr1 << "." << endl; cout << "The max_size of original string str1 is: " << max_sizeStr1 << "." << endl << endl; str1.erase ( 6, 5 ); cout << "The modified string str1 is: " << str1 << endl; sizeStr1 = str1.size ( ); lenStr1 = str1.length ( ); capStr1 = str1.capacity ( ); max_sizeStr1 = str1.max_size ( ); // Compare size, length, capacity & max_size of a string // after erasing part of the original string cout << "The current size of modified string str1 is: " << sizeStr1 << "." << endl; cout << "The current length of modified string str1 is: " << lenStr1 << "." << endl; cout << "The capacity of modified string str1 is: " << capStr1 << "." << endl; cout << "The max_size of modified string str1 is: " << max_sizeStr1 << "." << endl; } </pre> ===basic_string::substr=== 从字符串起始处的指定位置复制最多某个数目的字符的子字符串。 <pre> #include <string> #include <iostream> int main() { using namespace std; string str1("Heterological paradoxes are persistent."); cout << "The original string str1 is: \n " << str1 << endl << endl; basic_string <char> str2 = str1.substr(6, 7); cout << "The substring str1 copied is: " << str2 << endl << endl; basic_string <char> str3 = str1.substr(); cout << "The default substring str3 is: \n " << str3 << "\n which is the entire original string." << endl; } </pre> <pre> The original string str1 is: Heterological paradoxes are persistent. The substring str1 copied is: logical The default substring str3 is: Heterological paradoxes are persistent. which is the entire original string. </pre> ===basic_string::clear=== 清除字符串中的全部元素。 <pre> #include <string> #include <iostream> int main() { using namespace std; string str1("Hello world"), str2; basic_string <char>::iterator str_Iter; cout << "The original string str1 is: "; for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++) cout << *str_Iter; cout << endl; str1.clear(); cout << "The modified string str1 is: "; for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++) cout << *str_Iter; cout << endl; //For an empty string, begin is equivalent to end if (str1.begin() == str1.end()) cout << "Nothing printed above because " << "the string str1 is empty." << endl; else cout << "The string str1 is not empty." << endl; } </pre> <pre> The original string str1 is: Hello world The modified string str1 is: Nothing printed above because the string str1 is empty. </pre> ===basic_string::erase=== 从字符串中的指定位置删除一个或一系列元素。 <pre> #include <string> #include <iostream> int main() { using namespace std; // The 1st member function using a range demarcated // by iterators string str1("Hello world"); basic_string <char>::iterator str1_Iter; cout << "The original string object str1 is: " << str1 << "." << endl; str1_Iter = str1.erase(str1.begin() + 3, str1.end() - 1); cout << "The first element after those removed is: " << *str1_Iter << "." << endl; cout << "The modified string object str1 is: " << str1 << "." << endl << endl; // The 2nd member function erasing a char pointed to // by an iterator string str2("Hello World"); basic_string <char>::iterator str2_Iter; cout << "The original string object str2 is: " << str2 << "." << endl; str2_Iter = str2.erase(str2.begin() + 5); cout << "The first element after those removed is: " << *str2_Iter << "." << endl; cout << "The modified string object str2 is: " << str2 << "." << endl << endl; // The 3rd member function erasing a number of chars // after a char string str3("Hello computer"), str3m; basic_string <char>::iterator str3_Iter; cout << "The original string object str3 is: " << str3 << "." << endl; str3m = str3.erase(6, 8); cout << "The modified string object str3m is: " << str3m << "." << endl; } </pre> <pre> The original string object str1 is: Hello world. The first element after those removed is: d. The modified string object str1 is: Held. The original string object str2 is: Hello World. The first element after those removed is: W. The modified string object str2 is: HelloWorld. The original string object str3 is: Hello computer. The modified string object str3m is: Hello . </pre> ==<string> 函数== #getline #stod #stof #stoi #stol #stold #stoll #stoul #stoull #swap #to_string #to_wstring ===getline=== 将字符串从输入流中一行一行地提取出来。 以下代码演示两种模式下的 getline():第一种使用默认分隔符(换行),第二种使用空格作为分隔符。 文件尾字符(键盘上的 CTRL-Z)用于控制 while 循环的终止。 此值会将 cin 的内部状态标志设置为 eofbit,后者必须使用 basic_ios::clear() 进行消除,这样第二个 while 循环才能正确运行。 <pre> // compile with: /EHsc /W4 #include <string> #include <iostream> #include <vector> using namespace std; int main() { string str; vector<string> v1; cout << "Enter a sentence, press ENTER between sentences. (Ctrl-Z to stop): " << endl; // Loop until end-of-file (Ctrl-Z) is input, store each sentence in a vector. // Default delimiter is the newline character. while (getline(cin, str)) { v1.push_back(str); } cout << "The following input was stored with newline delimiter:" << endl; for (const auto& p : v1) { cout << p << endl; } cin.clear(); vector<string> v2; // Now try it with a whitespace delimiter while (getline(cin, str, ' ')) { v2.push_back(str); } cout << "The following input was stored with whitespace as delimiter:" << endl; for (const auto& p : v2) { cout << p << endl; } } </pre>
返回至“
C++ string
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
基础知识
正则表达式
Markdown
分布式
项目管理
系统集成项目管理基础知识
云原生
Docker
云原生安全
云原生词汇表
十二因素应用
Kubernetes
音频处理
音频合成
Edge-tts
CMS系统
Docsify
VuePress
Mediawiki
自动生成
Marp
CI/CD
GitLab
设计
颜色
平面设计
AI
数字人
操作系统
GNU/Linux
数据库
Mysql
工具
链入页面
相关更改
特殊页面
页面信息