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 equal - Compares two sequences to see if they match.

Algorithm equal - Compares two sequences to see if they match. equal Header <algorithm> template<class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) Compares two sequences to see if they match. equal compares the sequence [first1, last1) with a sequence of the same size starting at first2. It returns true if every corresponsing pair of elements match. The non-predicate version uses operator== for comparison. template<class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, Predicate pr) Compares two sequences to see if they match. equal compares the sequence [first1, last1) with a sequence of the same size starting at first2. It returns true if every corresponsing pair of elements match. The predicate version uses the predicate function pr for comparison. Sample #pragma warning (disable :4786) #include <algorithm> #include <iostream> #include <string> struct Employee { Employee(std::string name, long id) { m_name = name ; m_id = id ; } Employee() { } friend std::ostream& operator<<(std::ostream& os, const Employee& re) ; std::string m_name ; long m_id ; } ; std::ostream& operator<<(std::ostream& os, const Employee& re) { os << re.m_name << "\t" << re.m_id ; return os ; } bool compare(const Employee& e1, const Employee& e2) { return ((e1.m_name == e2.m_name) && (e1.m_id == e1.m_id)) ; } int main() { int set1[] = {10, 20, 35, 45, 50} ; int set2[] = {10, 20, 35, 45, 50} ; std::ostream_iterator<int> intostreamit(std::cout, ", ") ; bool result ; std::cout << "set1 = " ; std::copy(set1, set1+5, intostreamit) ; std::cout << std::endl ; std::cout << "set2 = " ; std::copy(set2, set2+5, intostreamit) ; std::cout << std::endl ; //is set1 == set2? //non-predicate version of equal result = std::equal(set1, set1+5, set2) ; if (result) std::cout << "set1 and set2 are equal" << std::endl ; else std::cout << "set1 and set2 are not equal" << std::endl ; Employee emp1[5], emp2[5] ; emp1[0] = Employee("Tim", 123456) ; emp1[1] = Employee("Jack", 123457) ; emp1[2] = Employee("Henry", 123458) ; emp1[3] = Employee("Lucy", 123459) ; emp1[4] = Employee("Liz", 123460) ; emp2[0] = Employee("Tim", 123456) ; emp2[1] = Employee("Jack", 123457) ; emp2[2] = Employee("Henry", 123458) ; emp2[3] = Employee("Lucy", 123459) ; emp2[4] = Employee("Liz", 123460) ; int i ; std::cout << "\nemp1 = " << std::endl; for(i = 0; i < 5; i++) std::cout << emp1[i] << std::endl ; std::cout << std::endl ; std::cout << "emp2 = " << std::endl; for(i = 0; i < 5; i++) std::cout << emp2[i] << std::endl ; std::cout << std::endl ; //is emp1 == emp2? //predicate version of equal result = std::equal(emp1, emp1+5, emp2, compare) ; if (result) std::cout << "emp1 and emp2 are equal" << std::endl ; else std::cout << "emp1 and emp2 are not equal" << std::endl ; return 0 ; } Program Output set1 = 10, 20, 35, 45, 50, set2 = 10, 20, 35, 45, 50, set1 and set2 are equal emp1 = Tim 123456 Jack 123457 Henry 123458 Lucy 123459 Liz 123460 emp2 = Tim 123456 Jack 123457 Henry 123458 Lucy 123459 Liz 123460 emp1 and emp2 are equal

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

#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 computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

copy() function is used to copy items from one iterator to another iterator with a specific range. We can define the start and end position of the source and it will copy all items in this rage to a different destination. To use copy() function, we need to include <bits/stdc+.h> or header file. It copies all the elements pointed by first and last. first element is included in the output but last is not. output is the start position of the final result iterator. It returns one iterator to the end of the destination range where elements have been copied. Function returns an iterator to the end of the destination range where elements have been copied.

Test whether the elements in two ranges are equal. Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match. C++ Algorithm equal()function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching. The first range is from [first1,last1) and the second starts from first2. The elements are compared using operator== (or pred, in version (2)). The function shall not modify any of its arguments.

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.

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.

Ostream iterators are output iterators that write sequentially to an output stream (such as cout). They are constructed from a basic_ostream object, to which they become associated, so that whenever an assignment operator (=) is used on the ostream_iterator (dereferenced or not) it inserts a new element into the stream. Optionally, a delimiter can be specified on construction. This delimiter is written to the stream after each element is inserted.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

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.

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

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.

Lets see a sample without break statement, then we will discuss switch case with break 'Switch Case' statement is mostly used with 'break statement' even though the break is

"Multilevel inheritance" represents a type of inheritance when a "Derived" class is a base class for another class. In c++ deriving a class from a derived class is known as 'multi-level'

Direct Recursion: When function calls itself, it is called "Direct recursion". Indirect recursion: When function calls "another function" & that function calls the calling function, then this is

Converts any string to Lowercase. Converts a string to "uppercase". Appends a string at the end of another. Appends first 'n' characters of a string at the end of another. Copies a string