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

Sort all elements in the range [first, last) into ascending order.

Sort all elements in the range [first, last) into ascending order. sort Header <algorithm> template<class RandomAccessIterator> void sort(RandomAccessIterator first, RandomAccessIterator last) Sort all elements in the range [first, last) into ascending order. The non-predicate version uses operator< for comparison. template<class RandomAccessIterator, class BinaryPredicate> void sort(RandomAccessIterator first, RandomAccessIterator last, BinaryPredicate pr) Sort all elements in the range [first, last) into ascending order. The predicate version uses the predicate function pr for comparison. Sample #pragma warning(disable : 4786) #include <algorithm> #include <iostream> #include <functional> int main() { int array[6] = { 102, 425, 678, 34, 87, 69} ; std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ; std::cout << "using non-predicate sort" << std::endl ; std::cout << "\n\narray before sorting = " ; std::copy(array, array+6, intOstreamIt) ; std::cout << std::endl ; //sort non-predicate version std::sort(array, array+6) ; std::cout << "\n\narray after sorting = " ; std::copy(array, array+6, intOstreamIt) ; std::cout << std::endl ; std::random_shuffle(array, array+6) ; std::cout << "using predicate sort" << std::endl ; std::cout << "\n\narray after random_shuffle = " ; std::copy(array, array+6, intOstreamIt) ; std::cout << std::endl ; //sort predicate version std::sort(array, array+6, std::less<int>()) ; std::cout << "\n\narray after sorting = " ; std::copy(array, array+6, intOstreamIt) ; std::cout << std::endl ; return 0 ; } Program Output using non-predicate sort array before sorting = 102, 425, 678, 34, 87, 69, array after sorting = 34, 69, 87, 102, 425, 678, using predicate sort array after random_shuffle = 425, 102, 34, 87, 678, 69, array after sorting = 34, 69, 87, 102, 425, 678,

Function object class for less-than inequality comparison. Binary function object class whose call returns whether the its first argument compares less than the second (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. This function does not return any value.

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

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.

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.

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.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

Sort elements in range. Sorts the elements in the range [first,last) into ascending order. The elements are compared using operator< for the first version, and comp for the second. Equivalent elements are not guaranteed to keep their original relative order (see stable_sort). C++ Algorithm sort() function is used to sort the elements in the range [first, last) into ascending order. The elements are compared using operator < for the first version, and comp for the second version. std::sort() is a built-in function in C++'s Standard Template Library. The function takes in a beginning iterator, an ending iterator, and (by default) sorts the iterable in ascending order. The function can also be used for custom sorting by passing in a comparator function that returns a boolean.

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.

Randomly rearrange elements in range. Rearranges the elements in the range [first,last) randomly. The function swaps the value of each element with that of some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness. To specify a uniform random generator as those defined in <random>. This function does not return any value.





To find the largest number among the three numbers in C++, enter three numbers, now checking which one is the "largest number". After finding the largest number, we display