Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Visual C++ 5.0 Standard C++ Library Code Examples

Algorithm max - Returns the maximum of the two items.

Algorithm max - Returns the maximum of the two items. max Header <algorithm> template<class T> const T& _cpp_max(const T& x, const T& y) Returns the maximum of the two items. _cpp_max returns a reference to the larger of the x and y. The non-predicate version uses operator< for comparison. template<class T, BinaryPredicate> const T& _cpp_max(const T& x, const T& y, BinaryPredicate pr) Returns the maximum of the two items. _cpp_max returns a reference to the larger 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

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

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.

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

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

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.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

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.

This is a C++ Program to find "shortest path". Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like "Prim's MST", we generate a SPT with given

We create a queue for BFS. Mark the current node as visited and enqueue it. It will be used to get all adjacent vertices of a vertex. Get all adjacent vertices of the dequeued vertex s. If

This algorithm represents a given graph using 'Adjacency List'. This method of representing graphs isn't efficient. The time complexity of this algorithm is O(v*e). Print the 'adjacency'