C + + string

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<string>//Template TypeusingSTD::string;usingStd::endl;usingstd::cout;voidTest0 (void){//Stack Space    CharStr1[] ="Hello,world"; CharStr2[] ="Shengzhen"; intLen1 =sizeof(STR1); intLen2 =sizeof(STR2); cout<<"len1 ="<< len1 <<", len2 ="<< Len2 <<Endl;//strcat (str1, str2);//The array space represented by the first parameter must be sufficient, otherwise the string will be crossed after stitchingprintf ("%s\n", STR1); printf ("%s\n", STR2); printf ("%p\n", STR1); printf ("%p\n", str2);}voidTest1 (void){//Heap Space    Char*STR1 ="Hello,world"; Char*STR2 ="Shengzhen"; intLen = strlen (str1) +strlen (STR2); Char*ptr = (Char*) malloc (len +1); Memset (PTR,0, Len); intLen1 =sizeof(STR1); intLen2 =sizeof(STR2); cout<<"len1 ="<< len1 <<", len2 ="<< Len2 <<Endl;    strcpy (PTR, str1); Strcat (PTR, str2);//the array space represented by the first argument must be sufficient for the string to be crossed after stitching    Char* P1 = strstr (str1," World"); Char* P2 = (Char*) malloc (strlen (p1) +1);    strcpy (P2, p1); printf ("%s\n", p2); printf ("%s\n", PTR); printf ("%p\n", PTR); printf ("%s\n", STR1); printf ("%s\n", STR2); printf ("%p\n", STR1); printf ("%p\n", STR2);    Free (p2); Free (PTR);}voidtest2 () {//C + + string is a type    stringS1 ="Hello,world"; stringS2 ="Shengzhen"; stringS3 = S1 +S2; //you can think of string as an array to use     for(intIDX =0; IDX < s1.size (); ++idx) {cout<< S1[idx] <<" ; "<<idx <<Endl; } size_t Pos= S1.find (" World");//find from head to tail    stringSub = S1.SUBSTR (POS,5); //iterators    string:: Iterator it =S1.begin ();  for(; it! = S1.end (); + +it) {cout<< *it <<Endl; }//c++11     for(Auto &elem:s2) {//Auto keyword can be typed automatically pushed to thecout << Elem <<Endl; } cout<<"Sub ="<< sub<<Endl; cout<<"S1 ="<< S1 <<Endl; cout<<"s2 ="<< S2 <<Endl; S3.append ("Wangdao"); cout<<"s3 ="<< S3 <<Endl; strings4= S1 + s2 +"Lihui"+S3; cout<<"S4 ="&LT;&LT;S4 <<Endl;}intMainvoid) {test2 (); System ("Pause"); return 0;}

C + + string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.