C++ Programming Code Examples
C++ > Visual C++ 5.0 Standard C++ Library Code Examples
Algorithm min - Returns the minimum of the two items.
Algorithm min - Returns the minimum of the two items.
min
Header
<algorithm>
template<class T>
const T& _cpp_min(const T& x, const T& y)
Returns the minimum of the two items. _cpp_min returns a reference to the smaller of the x and y.
The non-predicate version uses operator< for comparison.
template<class T, BinaryPredicate>
const T& _cpp_min(const T& x, const T& y, BinaryPredicate pr)
Returns the minimum of the two items. _cpp_min returns a reference to the smaller of the x and y.
The predicate version uses the predicate function pr for comparison.
Microsoft Specific
NOTE: To avoid conflicts with max and min in WINDEF.H, use _MAX and _MIN. These macros evaluate to _cpp_max and _cpp_min
End Microsoft Specific
Samples
#pragma warning (disable: 4786)
#include <algorithm>
#include <iostream>
#include <string>
struct PhoneBook
{
PhoneBook(std::string s, long num)
{
name = s ;
number = num ;
}
friend std::ostream& operator<<(std::ostream&, const PhoneBook&) ;
long number ;
std::string name ;
} ;
std::ostream& operator<<(std::ostream& os, const PhoneBook& rpb)
{
os << rpb.name << "\t" << rpb.number << std::endl ;
return os ;
}
bool compare(const PhoneBook& r1, const PhoneBook& r2)
{
return (r2.name > r1.name) ;
}
int main()
{
//non-predicate version of _MAX
std::cout << "_MAX(10, 30) = " << std::_MAX(10, 30)
<< std::endl ;
//non-predicate version of _MIN
std::cout << "_MIN(10, 30) = " << std::_MIN(10, 30)
<< std::endl ;
PhoneBook s1("Tom", 8612345) ;
PhoneBook s2("Jack", 6782345) ;
//predicate version of _MAX
std::cout << "_MAX(s1, s2, max_PhoneBook) = " << std::_MAX(s1, s2, compare)
<< std::endl ;
//predicate version of _MAX
std::cout << "_MIN(s1, s2, min_PhoneBook) = " << std::_MIN(s1, s2, compare)
<< std::endl ;
return 0 ;
}
Program Output
_MAX(10, 30) = 30
_MIN(10, 30) = 10
_MAX(s1, s2, max_PhoneBook) = Tom 8612345
_MIN(s1, s2, min_PhoneBook) = Jack 6782345
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.
#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.
A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.
Return the smallest. Returns the smallest of a and b. If both are equivalent, a is returned. min() function is a library function of algorithm header, it is used to find the smallest value from given two values, it accepts two values and returns the smallest value and if both the values are the same it returns the first value. The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. The function uses operator< (or comp, if provided) to compare the values.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. If a function is defined as a friend function in C++ programming language, then the protected and private data of a class can be accessed using the function. By using the keyword friend compiler knows the given function is a friend function. For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.
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.
In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.
Class for addition, subtraction, multiplication and division for 'complex numbers'. Class has 4 functions to perform arithmetic operations. It takes 2 "complex numbers" input from user
Internal method to merge Two Roots. Deals with Deviant Cases & calls recursive merge1. Internal method to "merge two roots". And Assumes 'trees are not empty', and h1's root