String Begin Function - C++ Programming Tutorial
C++ Course / Strings / String Begin Function

String Begin Function

BLUF: Mastering String Begin Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Begin Function

C++ is renowned for its efficiency. Learn how String Begin Function enables low-level control and high-performance computing in the tutorial below.

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:

Example

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.

Example

#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.

Example

#include<iostream>
using namespace std;
int main()
{
	string str ="B language";
	*str.begin()='C';
	cout<<str;
	return 0;
}

Output:

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.

Example

#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:

Output

123456

In this instance, a collection of whole numbers has been traversed utilizing the start function.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience