#include <iostream> #include <string> using namespace std; intmain() { string str("12345678"); char ch[]="abcdefghijklmn"; //Define empty string a string a; //Constructor, copy all str to str_1; string str_1(str); //Constructor, starting from the second element of the string str, copying five elements and assigning them to str_2 string str_2(str,2,5); //Assign the first five elements of ch to str_3 string str_3(ch,5); //Assign a string consisting of five A's to str_4 string str_4(5,'A'); //Copy all the contents of str to str_5 string str_5(str.begin(),str.end()); cout<<str<<endl<<a<<endl<<str_1<<endl<<str_2<<endl<<str_3<<endl<<str_4<<endl<<str_5<<endl; return 0; }
C++ STL string constructor
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325592079&siteId=291194637
Recommended
Ranking