C++ Programming Code Examples
C++ > Code Snippets Code Examples
Demonstrate insert(), erase(), and replace().
/* Demonstrate insert(), erase(), and replace(). */
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("Hello World");
string str2("STL Power");
cout << "Initial strings:\n";
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << "\n\n";
// insert()
cout << "Insert str2 into str1:\n";
str1.insert(6, str2);
cout << str1 << "\n\n";
// erase()
cout << "Remove 9 characters from str1:\n";
str1.erase(6, 9);
cout << str1 <<"\n\n";
// replace
cout << "Replace 8 characters in str1 with str2:\n";
str1.replace(7, 8, str2);
cout << str1 << endl;
return 0;
}
Replace portion of string. Replaces the portion of the string that begins at character pos and spans len characters (or the part of the string in the range between [i1,i2)) by new contents. The replace() function is a part of the string functions which C++ provides. It helps in replacing a part of the string which will begin with a certain position which will act as a start and it will extend till a certain number of characters. This string will be replaced with the specified string. The string to be replaced with the start and end positions should be given to the replace() function as parameters that help us in replacing the string.
A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.
Insert into string. Inserts additional characters into the string right before the character indicated by pos (or p): insert() is used to insert characters in string at specified position. It supports various syntaxes to facilitate same, here we will describe them. Function returns the signatures returning a reference to string, return *this. Those returning an iterator, return an iterator pointing to the first character inserted.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Erase characters from string. Erases part of the string, reducing its length. erase() string function removes the characters as specified, reducing its length by one. • sequence (1) Erases the portion of the string value that begins at the character position pos and spans len characters (or until the end of the string, if either the content is too short or if len is string::npos. Notice that the default argument erases all characters in the string (like member function clear). • character (2) Erases the character pointed by p. • range (3) Erases the sequence of characters in the range [first,last).
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
"Switch statement" is multi-way decision that tests whether an expression 'matches' one of a number of "constant integer", and branches accordingly. 'Switch statement' that allows us
Addition, Subtraction, Multiplation, Result to decimal. Reverse second binary operand and Creating an array for addition. Addition of all columns without carry. Final calculations and
To swap two numbers in C++, ask to the user to enter the two number, and store both the number in the variable say num1 and num2. Now to swap both the number, first, make a