C++ String begin
This function provides a reference to the initial element.
Syntax
Consider a string 's' and iterator 'it'. The syntax is as follows:
iterator it = s.begin();
Parameter
This function does not contain any parameter.
Return value
This function does not return any value.
Example 1
Let's see the simple example of begin function.
#include<iostream>
using namespace std;
int main()
{
string str="Hello";
cout<<*str.begin();
return 0;
}
Output:
This illustration demonstrates how to retrieve the initial character of a string by utilizing the begin function.
Example 2
Let's see the another example.
#include<iostream>
using namespace std;
int main()
{
string str ="B language";
*str.begin()='C';
cout<<str;
return 0;
}
Output:
C language
This example demonstrates how to change the initial character of a string by utilizing the begin function.
Example 3
Let's explore a basic example of an array class where we can utilize the begin function.
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<int,6> myarray={1,2,3,4,5,6};
for (int i=myarray.begin();i<myarray.end();i++)
{
cout<<*myarray;
}
return 0;
}
Output:
123456
In this instance, a collection of whole numbers has been traversed utilizing the start function.