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 search - search checks whether the sequence in the second range

Algorithm search - search checks whether the sequence in the second range search Header <algorithm> template<class ForwardIterator1, class ForwardIterator2> ForwardIterator2 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) search checks whether the sequence in the second range [first2, last2), is a subset of the sequence in the range [first1, last1). If found, it returns an iterator i in the range [first1, last1) that represents start of the subsequence, otherwise last1 is returned. The non-predicate version uses operator== for comparison. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator2 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pr) search checks whether the sequence in the second range [first2, last2), is a subset of the sequence in the range [first1, last1). If found, it returns an iterator i in the range [first1, last1) that represents start of the subsequence, otherwise last1 is returned. The predicate version uses predicate function pr for comparison. Sample #pragma warning (disable :4786) #include <algorithm> #include <iostream> #include <string> #include <functional> int main() { int set1[5] = {10, 15, 20, 25, 30} ; int set2[2] = {20, 25} ; int* pos ; std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ; std::cout << "set1 = " ; std::copy(set1, set1+5, intOstreamIt) ; std::cout << std::endl ; std::cout << "set2 = " ; std::copy(set2, set2+2, intOstreamIt) ; std::cout << std::endl ; //non-predicate version of search pos = std::search(set1, set1+5, set2, set2+2) ; std::cout << "\n\nusing non-predicate version of search" << std::endl ; std::cout << "match found at offset " << (pos - set1) << std::endl ; //predicate version of search pos = std::search(set1, set1+5, set2, set2+2, std::equal_to<int>()) ; std::cout << "\n\nusing predicate version of search" << std::endl ; std::cout << "match found at offset " << (pos - set1) << std::endl ; return 0 ; } Program Output set1 = 10, 15, 20, 25, 30, set2 = 20, 25, using non-predicate version of search match found at offset 2 using predicate version of search match found at offset 2

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.

Search range for subsequence. Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2). This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end. The function shall not modify any of its arguments.

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.

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.

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.

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

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.

Function object class for equality comparison. Binary function object class whose call returns whether its two arguments compare equal (as returned by operator ==). equal_to is a function object class for equality comparison and binary function object class whose call returns whether its two arguments compare equal (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.










Function overloading & Operator overloading are examples of "Polymorphism". In The C++ programming In function overloading we can have more than one function by 'same name'