string insert() in C++

insert(): It inserts one character into the given string at the specified position.

This function returns an iterator that referencing to newly inserted char.

 Parameters: Two parameters are required for this function.

__p – Iterator referencing position in the string to insert at.

 __c – The character to insert into the string

Syntax:

str.insert(__p, __c)

For Example:

str = "abc"

str.insert(str.end(),'d') = > It insert character 'd' at the end of the given string.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "abc";

    str.insert(str.end(), 'd');

    cout << str << "\n";
    
    return 0;
}


No comments:

Post a Comment