Updated April 15, 2023
Introduction to C++ end()
In C++ we mostly use the end() functions with the iteration over list of the elements and it will be used to print end of the attributes. This function is a c++ system defined and it can be used with any type list of array iteration. For example, if we have lists of the student’s name and we want to print each student’s name and here we can see the name of the student is a string, in the same way, can do the iteration over any integer value and we can print the end value of the list. For example, if we have marks list then we can print each end mark with iteration.
Syntax:
Below is a simple syntax for the function end in the c++. We can see from the below syntax that we are using end() function with the variable value. Where value is an array of lists. It can contain anything from string to integer type of list inside it. This is the most important feature of it that can be used any type of combination of the list(integer, string, float).
Please see the below syntax for better understanding.
values.end() //value can be any list of the array.
How end() Functions Work in C++?
Before discussing the end function, we need to understand the importance of it. Suppose we have some array of numbers and we want to print them all then we will use iteration the list of the array and using the cout<< index value and the output will be like 10964589. Here the list was like {10,96,45,89} so the output looks like a single number. Now to solve this look if we will use the end function here like cout<< index <<endl, not the new output will be printed in the better format with understandable to external uses also. It always returns the last element of the iteration of the list value. We will see more in the example sections.
Examples to Implement C++ end()
Below is some of the examples where we are printing the end of the list of the array. We have taken a various types of list types like string, float, and integer types and printing the value of them. In the examples, we have taken iterator for performing and using the function end()
Example #1
Code:
//Importing the package vector
#include <vector>
//Importing the package iostream to handle io works
#include <iostream>
//Using the namespace std
using namespace std;
int main(void) {
//creating a list of number of integer type
vector<float> values = {10, 9, 8, 7, 6};
cout<< "The number list without end function is";
for (auto n = values.begin(); n != values.end(); ++n)
//printing each number without using end function
cout<< *n ;
return 9;
}
Output:
Example #2
Code:
//Importing the package vector
#include <vector>
//Importing the package iostream to handle io works
#include <iostream>
//Using the namespace std
using namespace std;
int main(void) {
//creating a list of number of integer type
vector<int> values = {10, 9, 8, 7, 6};
cout<< "The Integer list is";
//Iteration over the list of the integer value
for (auto i = values.begin(); i != values.end(); ++i)
//printing each number end value
cout<< *i<<endl;
return 9;
}
Output:
Example #3
Code:
//Importing the package vector
#include <vector>
//Importing the package iostream to handle io works
#include <iostream>
//Using the namespace std
using namespace std;
int main(void) {
//Creating the list of uses for iteration
vector<string> values = {"Ranjan", "Ajay", "Vijay", "Sujit", "Akash"};
cout<< "The user list is";
for (auto i = values.begin(); i != values.end(); ++i)
//Printing the each user endvalue by iteration and by using end function
cout<< *i<<endl;
return 9;
}
Output:
Example #4
Code:
//Importing the package vector
#include <vector>
//Importing the package iostream to handle io works
#include <iostream>
//Using the namespace std
using namespace std;
int main(void) {
//Creating the list of float number of the marks
vector<float> values = {10.4, 9.4, 8.7, 7.0, 6.6};
cout<< "The float list is";
for (auto f = values.begin(); f != values.end(); ++f)
//Printing each float value of the marks
cout<< *f <<endl;
return 9.6;
}
Output:
Example #5
Code:
// Importing important packages for the program
using namespace std;
#include <bits/stdc++.h>
int main()
{
//Creating the list of marks with float type
list<float> marks;
// Here we are setting all the marks to marks variable
marks.push_back(103.3);
marks.push_back(201.4);
marks.push_back(188.5);
marks.push_back(134.3);
//Printing directly the end value of the list
list<float>::iterator m = marks.end();
//Here we are printing the last marks of variable
cout<< "The last marks from the variable is : " << *m <<endl;
//Iteration over the lists of the marks
for (auto mtr = marks.begin();
mtr != marks.end(); mtr++) {
//Here we are printing each value with the end of the marks list
cout<< *mtr<< " ";
}
return 9;
}
Output:
Advantages of C++ end()
There are many advantages of the use of the end function in the c++, the most important advantage of them are given below.
1. Help for printing the end value of the iterator and because it is a system-defined function so it is well optimized for uses.
2. Reduce the extra code to perform the same task with code written by us.
3. It always accesses the element but never modify the elements
4. Always gives better performance compared to any other existing ways.
Conclusion
From this tutorial, we learned the basics of the end function in c++. We learned about the working and the syntax of the end function. We Show some of the important examples of it for understand it’s work. We also focus on some of the advantages of the uses of the end() function.
Recommended Article
This is a guide to the C++ end(). Here we discuss the Introduction of C++ end() and its different Advantages along with Examples and Code Implementation. You can also go through our suggested articles to learn more –