- 0
- 0
- 约3.54千字
- 约 13页
- 2021-11-30 发布于安徽
- 举报
Chapter 15 String * * The C++ “string” Class A C++ library class defined in string With more features than C-string functions * Constructing a String A string object with empty string: string newString; A string object from a string literal or an char array: string newString(stringLiteral); * Appending a String string s1(Welcome); s1.append( to C++); // appends to C++ to s1 cout s1 endl; //Welcome to C++ string s2(Welcome); s2.append( to C and C++, 0, 5); cout s2 endl; //Welcome C an string s3(Welcome); s3.append( to C and C++, 5); cout s3 endl; //Welcome to C string s4(Welcome); s4.append(4, G); cout s4 endl; //WelcomeGGGG * Assigning a String You can use several overloaded functions to assign new contents to a string. For example, see the following code: string s1(Welcome); s1.assign(Dallas); // assigns Dallas to s1 cout s1 endl; // s1 now becomes Dallas string s2(Welcome); s2.assign(Dallas, Texas, 0, 5); // assigns Dalla to s2 cout s2 endl; // s2 now becomes Dalla string s3(Welcome); s3.assign(Dallas, Texas, 5); // assigns Dalla to s3 cout s3 endl; // s3 now becomes Dalla string s4(Welcome); s4.assign(4, G); // assigns GGGG to s4 cout s4 endl; // s4 now becomes GGGG * Functions at, clear, erase, and empty You can use the at(index) function to retrieve a character at a specified index, clear() to clear the string, erase(index, n) to delete part of the string, and empty() to test if a string is empty. For example, see the following code: string s1(Welcome); cout s1.at(3) endl; // s1.at(3) returns c cout s1.erase(2, 3) endl; // s1 is now Weme s1.clear(); // s1 is now empty cout s1.empty() endl; // s1.empty returns 1 (means true) * Comparing Strings Often, in a program, you need to compare the contents of two strings. You can use the compare function. This function works in the same way as the C-string strcmp function and returns a value greater than 0, 0, or less than 0. For example, see the following code: string s1(Wel
您可能关注的文档
最近下载
- 企业研发活动统计107-1、107-2报表版整理版(2021年-2022年).pdf VIP
- XX中学校2026年春季学期安全工作计划与应急演练安排表.docx VIP
- 园林植物配置与应用PPT课件.pptx VIP
- DB33/1006-2017 浙江省住宅设计标准.docx VIP
- HGT 4684-2014 液氯泄漏的处理处置方法.docx VIP
- IMT_2030(6G)推进组-通信行业:6G可信内生安全架构研究报告.docx VIP
- 亨格瑞成本与管理会计(第16版) 课件 Chapter 1 The Manager and Management Accounting .pptx
- 人教版高中化学选择性必修第2册 第三章 晶体结构与性质 第三节 金属晶体与离子晶体(一).ppt VIP
- 2025年全媒体运营师全媒体5G传播应用与传统媒体传输方式对比专题试卷及解析.pdf VIP
- IMT_2030(6G)推进组-通信行业:6G通信感知一体化评估方法研究报告.docx VIP
原创力文档

文档评论(0)