Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: All
Apr 7, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 <string>
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"
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date