What & How & Why

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
上一修订版两侧同时换到之后的修订记录
cs:programming:cpp:courses:cpp_basic_deep:chpt_3 [2024/04/18 04:26] – [Vector] codingharecs:programming:cpp:courses:cpp_basic_deep:chpt_3 [2024/04/18 04:47] – [std::String] codinghare
行 468: 行 468:
 </code> </code>
   * size() 返回的是 ''size_type''   * size() 返回的是 ''size_type''
-====std::String====+====std::string==== 
 +  * 特化自 ''std::basic_string<char>'' 
 +  * 可复制,可改变字符串长度 
 +===string 的使用方法=== 
 +<code cpp> 
 +//需要引入头文件 
 +#include <string> 
 +//初始化 
 +std::string myStr = "helleworld"; 
 + 
 +//使用多次复制目标字符作为初始值 
 +//结果是 'aaa' 
 +std::string myStr2(3, 'a'); 
 +//拷贝初始化 
 +std::string myStr3 = myStr; 
 +std::string myStr4(myStr); 
 + 
 +//比较尺寸size & empty 
 +//比较 == > <,比较规则与 vector 同,按字符为单位比较 
 +//赋值 
 +std::string newStr; 
 +newStr = myStr; 
 +//拼接 
 +newStr = myStr + myStr2; 
 +newStr = myStr + "hellWorld"; 
 +//string + 的重载左边要求对象类型是 std::string,所以 c-string 不能放到加号左边 
 +//error 
 +newStr = "hello" + myStr; 
 + 
 +</code>