c++ string vector類


//string對象的初始化 
#include <iostream> #include <string> //typedef std::basic_string<char> string; 
using namespace std; typedef string String; int main() { // with no arguments 
    string s1;          //默認構造函數:沒有參數或參數有默認值 
    String s2("hello"); //普通構造函數 String就是string 
    s1 = "Anatoliy";    //賦值運算符 
    String s3(s1);   //拷貝構造函數 string s3 =s1; 
    cout << "s1 is: " << s1 << endl; cout << "s2 is: " << s2 << endl; cout << "s3 is: " << s2 << endl; // first argument C string // second number of characters 
    string s4("this is a C_sting", 10); cout << "s4 is: " << s4 << endl; // 1 - C++ string // 2 - start position // 3 - number of characters 
    string s5(s4, 6, 4); // copy word from s3 cout << "s5 is: " << s5 << endl; // 1 - number characters // 2 - character itself 
    string s6(15, '*'); cout << "s6 is: " << s6 << endl; // 1 - start iterator(迭代器 ) // 2 - end iterator(迭代器 ) 
    string s7(s4.begin(), s4.end() - 5); cout << "s7 is: " << s7 << endl; // 通過=初始化string對象 
    string s8 = "Anatoliy"; cout << "s8 is: " << s8 << endl; string s9 = s1 + "hello"+ s2;    //s1 + "hello"+ s2的結果是string類型的對象(變量) 
    cout << "s9 is: " << s9 << endl; return 0; }

s1 is: Anatoliy
s2 is: hello
s3 is: Anatoliy
s4 is: this is aC
s5 is: s aC
s6 is: ***************
s7 is: this
s8 is: Anatoliy
s9 is: Anatoliyhellohello

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM