devxlogo

Various Forms of Initializing std::string

Class std::string can be initialized in five forms: by a C-string, by part of a C-string, by another string object, by a sequence of characters, or by part of another string object. For example:

 #include using namespace std;void f(){  const char text[] = "hello world";  string s1 = text;  // initialization of string object with a C-style string  string s2(s1);  // initialize with another string object  string s3(&text[0], &text[5]);   // initialize by part of a C-string  string s4(10, 0); // by a sequence of 10 characters  string s5 ( s2.begin(), s2.find(' ')); //  by part of another string object; s5 = "hello"}

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.