Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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.

 

 

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