#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 ="<<S4 <<Endl;}intMainvoid) {test2 (); System ("Pause"); return 0;}
C + + string