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

Find first of algorithm locates, within the range [first1, last1), the first element

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
Find first of algorithm locates, within the range [first1, last1), the first element find_first_of Header <algorithm> template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) The algorithm locates, within the range [first1, last1), the first element of a sequence represented by the range [first2, last2). The non-predicate version uses the operator== for comparison. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pr) The algorithm locates, within the range [first1, last1), the first element of a sequence represented by the range [first2, last2). The predicate version uses the predicate function pr for comparison. Sample #pragma warning (disable:4786) #include <algorithm> #include <iostream> #include <cstring> #include <functional> int main() { char *input = "AABBCDEFAAEFFBB" ; char *pattern = "BB" ; int inputLen = strlen(input) ; int patternLen = strlen(pattern) ; char *pos ; //find first occurence of pattern in input //non-predicate version of find_first_of pos = std::find_first_of(input, input+inputLen, pattern, pattern+patternLen) ; std::cout << "non-predicate version of find_first_of" << std::endl ; std::cout << "first occurence of " << pattern << " in " << input << " is at position " << (pos - input) << std::endl ; //find first occurence of pattern in input //predicate version of find_first_of pos = std::find_first_of(input, input+inputLen, pattern, pattern+patternLen, std::equal_to<char>()) ; std::cout << "\n\npredicate version of find_first_of" << std::endl ; std::cout << "first occurence of " << pattern << " in " << input << " is at position " << (pos - input) << std::endl ; //find last occurence of pattern in input //non-predicate version of find_end pos = std::find_end(input, input+inputLen, pattern, pattern+patternLen) ; std::cout << "\n\nnon-predicate version of find_end" << std::endl ; std::cout << "last occurence of " << pattern << " in " << input << " is at position " << (pos - input) << std::endl ; //find last occurence of pattern in input //predicate version of find_end pos = std::find_end(input, input+inputLen, pattern, pattern+patternLen, std::equal_to<char>()) ; std::cout << "\n\npredicate version of find_end" << std::endl ; std::cout << "first occurence of " << pattern << " in " << input << " is at position " << (pos - input) << std::endl ; return 0 ; } Program Output non-predicate version of find_first_of first occurence of BB in AABBCDEFAAEFFBB is at position 2 predicate version of find_first_of first occurence of BB in AABBCDEFAAEFFBB is at position 2 non-predicate version of find_end last occurence of BB in AABBCDEFAAEFFBB is at position 13 predicate version of find_end first occurence of BB in AABBCDEFAAEFFBB is at position 13

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.

Find element from set in range. Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1. The elements in [first1,last1) are sequentially compared to each of the values in [first2,last2) using operator== (or pred, in version (2)), until a pair matches. std::find_first_of is used to compare elements between two containers. It compares all the elements in a range [first1,last1) with the elements in the range [first2,last2), and if any of the elements present in the second range is found in the first one , then it returns an iterator to that element. If there are more than one element common in both the ranges, then an iterator to the first common element present in the first container is returned. In case there is no match, then iterator pointing to last1 is returned.

Find last subsequence in range. Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last 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 last of such occurrences. For an algorithm that returns the first instead, see search.

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.

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.

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.

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

Get string length. Returns the length of the C string str. C++ strlen() is an inbuilt function that is used to calculate the length of the string. It is a beneficial method to find the length of the string. The strlen() function is defined under the string.h header file. The strlen() takes a null-terminated byte string str as its argument and returns its length. The length does not include a null character. If there is no null character in the string, the behavior of the function is undefined.




'Memory allocated' for node dynamically and inserts element at beginning. Inserts elemnet at last. Inserts element at position and delete node at particular position. Update value of a



Here, we use 'swapping' of the elements with the help of a Variable say temp of same type. That is, on found, start 'Swapping' with temp variable, place the first number in the temp &

To convert temperature from "Fahrenheit" to centigrade in C++, enter the "temperature" in Fahrenheit to convert it to centigrade to print equivalent temperature value in "centigrade"


Insert Element at a key. Search Element at a key. Remove Element at a key. Enter element to be inserted. Enter key at which element to be inserted. And Enter key of the element to