C++

Objectives :

  • File Name : string_insert_list_insert1.cpp
  • Example of  insert operations , with list::iteration and string objects, side by side. Also noted the splicing properties of string::insert function.
    • string::insert :  temp = str1.insert(0,str2);
    • list::insert  : Lstr1.insert(it,1,"Green Revolution");
    • list::iteration : Lstr1.begin(); it !=Lstr1.end()
       
  • Code: string_insert_list_insert1.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step: 3 Runtime Views:

Step: Brief Discussion:

Operation " temp = str1.insert(0,str2); " : The red lines shows the insertion of string "str2" . before ("0" position) of another string, namely "str1). The expected output was "Good World Hello World" . The string "temp" is a temporary space holder.


Operation temp.insert(0, str3,5, string::npos);  :Here temp string has been emptied, and the insert function will bypass a section of string "str3" , starting from 0 to 5, copy the rest of the string (i.e. "Space" and add to temp string.


Operation "  list <string> Lstr1(strarray, strarray+3); " : The list object, "Lstr1" will receive the contents of a string-array "strarray", which would have three strings, "Hello World Good World Cloud Space" respectively. .

Operation " Lstr1.insert(it,1,"Green Revolution"); " : In this case insert function will append "1" item after a series of strings saved with iteration, "it". Now, the Lstr1 will have four elements in a series , namely ""Hello World Good World Cloud Space Green Revolution".