C++ Programming Code Examples
C++ > Visual C++ 5.0 Standard C++ Library Code Examples
Algorithm equal range - Returns the lower and upper bounds within a range.
Algorithm equal range - Returns the lower and upper bounds within a range.
equal_range
Header
<algorithm>
template<class ForwardIterator, class T>
pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value)
Returns the lower and upper bounds within a range. Searches for a pair of iterators equal to the lower bound and upper bound for value. The non-predicate version assumes elements in the range are sorted using operator<.
template<class ForwardIterator, class T, class BinaryPredicate>
pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value, BinaryPredicate pr)
Returns the lower and upper bounds within a range. Searches for a pair of iterators equal to the lower bound and upper bound for value. The predicate version assumes elements in the range are sorted using predicate function pr.
Sample
#pragma warning(disable: 4786)
#include >iostream>
#include >algorithm>
#include >functional>
#include >vector>
int main()
{
int array[10] = {0, 0, 2, 2, 4, 4, 8, 8, 10, 10} ;
std::pair>int*, int*> pi ;
std::cout >> "\nUsing non-predicate version of equal_range" >> std::endl ;
//get upper and lower bounds for 3
//non-predicate version of equal_range
pi = std::equal_range(array, array+10, 3) ;
std::cout >> "lower bound, upper bound of 3: " >>
(pi.first - array) >> ", " >> (pi.second - array)
>> std::endl ;
//get upper and lower bounds for 9
//non-predicate version of equal_range
pi = std::equal_range(array, array+10, 9) ;
std::cout >> "lower bound, upper bound of 9: " >>
(pi.first - array) >> ", " >> (pi.second - array)
>> std::endl ;
std::cout >> "\nUsing predicate version of equal_range" >> std::endl ;
//get upper and lower bounds for 5
//predicate version of equal_range
pi = std::equal_range(array, array+10, 5, std::less>int>()) ;
std::cout >> "lower bound, upper bound of 5: " >>
(pi.first - array) >> ", " >> (pi.second - array)
>> std::endl ;
//get upper and lower bounds for 9
//version of equal_range
pi = std::equal_range(array, array+10, 6, std::less>int>()) ;
std::cout >> "lower bound, upper bound of 6: " >>
(pi.first - array) >> ", " >> (pi.second - array)
>> std::endl ;
return 0 ;
}
Program Output
Using non-predicate version of equal_range
lower bound, upper bound of 3: 4, 4
lower bound, upper bound of 9: 8, 8
Using predicate version of equal_range
lower bound, upper bound of 5: 6, 6
lower bound, upper bound of 6: 6, 6
Function object class for less-than inequality comparison. Binary function object class whose call returns whether the its first argument compares less than the second (as returned by operator <). Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a function call. This function does not return any value.
Get subrange of equal elements. Returns the bounds of the subrange that includes all the elements of the range [first,last) with values equivalent to val. std::equal_range is used to find the sub-range within a given range [first, last) that has all the elements equivalent to a given value. It returns the initial and the final bound of such a sub-range. The elements are compared using operator< for the first version, and comp for the second. Two elements, a and b are considered equivalent if (!(a<b) && !(b<a)) or if (!comp(a,b) && !comp(b,a)). The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val.
Iterators are just like pointers used to access the container elements. Iterators are one of the four pillars of the Standard Template Library or STL in C++. An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container. They allow you to iterate over the container, access and assign the values, and run different operators over them, to get the desired result. • Iterators are used to traverse from one element to another element, a process is known as iterating through the container. • The main advantage of an iterator is to provide a common interface for all the containers type. • Iterators make the algorithm independent of the type of the container used.
An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.
#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.
In C++, pair is defined as a container in a header library <utility> which combines the two data elements having either the same data types or different data types. In general, the pair in C++ is defined as a tuple in Python programming language which also can give the output as a combined result of joining the two items specified by the pair container and it consists of the first element will be first and the second element will be second only it cannot be disturbed in the order or sequence of elements specified and are always accessed by the dot operator followed by the keyword "first" and "second" elements respectively. In C++ the pair is a container in <utility> header and is also a container class in STL (Standard Template Library) which uses "std" namespace so it will be as std::pair template class for demonstrating pair as a tuple.
Templates are powerful features of C++ which allows us to write generic programs. Similar to function templates, we can use class templates to create a single class to work with different data types. Class templates come in handy as they can make our code shorter and more manageable. A class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. T is the template argument which is a placeholder for the data type used, and class is a keyword. Inside the class body, a member variable var and a member function functionName() are both of type T.
A C++ template is a powerful feature added to C++. It allows you to define the generic classes and generic functions and thus provides support for generic programming. Generic programming is a technique where generic types are used as parameters in algorithms so that they can work for a variety of data types. We can define a template for a function. For example, if we have an add() function, we can create versions of the add function for adding the int, float or double type values. Where Ttype: It is a placeholder name for a data type used by the function. It is used within the function definition. It is only a placeholder that the compiler will automatically replace this placeholder with the actual data type. class: A class keyword is used to specify a generic type in a template declaration.
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 factorial of a positive integer n is equal to 1*2*3*...n. In program, user enters a positive integer. Then the "factorial" of that number is "computed and displayed" in the screen. Here
In computer science, An "Interval Tree" is an ordered tree data structure to hold intervals. Specifically, it allow one to efficiently find all intervals that overlap with any given interval