Defining and assigning string objects - C++ Data Type

C++ examples for Data Type:string

Description

Defining and assigning string objects

Demo Code

#include <iostream>
#include <string>
using namespace std;
int main()//from w  w w .  j ava  2 s.c  o m
{
   string s1("Man");
   string s2 = "test test";
   string s3;
   s3 = s1;                         //assign
   cout << "s3 = " << s3 << endl;
   s3 = "Neither " + s1 + " nor ";
   s3 += s2;
   cout << "s3 = " << s3 << endl;
   s1.swap(s2);                     //swap s1 and s2
   cout << s1 << " nor " << s2 << endl;
   return 0;
}

Result


Related Tutorials