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++ > Code Snippets Code Examples

Use find the search an element in deque

/* Use find the search an element in deque */ #include <iostream> #include <cassert> #include <deque> #include <algorithm> // For find using namespace std; int main() { int x[5] = {1, 2, 3, 4, 5}; deque<int> deque1(&x[0], &x[5]); // Search for the first occurrence deque<int>::iterator where = find(deque1.begin(), deque1.end(), 1); cout << *where << endl; return 0; } /* 1 */

C++ find() function is part of the standard library function which tries to find the first occurrence of the specified range of element where the range starts with first range to last range and that iterator encounters the first element, compares for the value which must be equal after all possible comparisons and if no element is found it returns the last element. For making all the comparisons it makes use of the operator = for comparison. If find() function performs any unnecessary action it throws exceptions that are not required by the programmer. Find value in range. Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last. The function uses operator== to compare the individual elements to val.

Return iterator to beginning. Returns an iterator pointing to the first element in the deque container. Notice that, unlike member deque::front, which returns a reference to the first element, this function returns a random access iterator pointing to it. If the container is empty, the returned iterator value shall not be dereferenced. deque::begin() is an inbuilt function in C++ STL which is declared in header file. deque::begin() returns an iterator which is referencing to the first element of the deque container associated with the function. Both begin() and end() are used to iterate through the deque container. This function does not accept any parameter.

Return iterator to end. Returns an iterator referring to the past-the-end element in the deque container. The past-the-end element is the theoretical element that would follow the last element in the deque container. It does not point to any element, and thus shall not be dereferenced. Because the ranges used by functions of the standard library do not include the element pointed by their closing iterator, this function is often used in combination with deque::begin to specify a range including all the elements in the container. If the container is empty, this function returns the same as deque::begin. deque::end() is an inbuilt function in C++ STL which is declared in<deque> header file. deque::end() returns an iterator which is referencing next to the last element of the deque container associated with the function. Both begin() and end() are used to iterate through the deque container.

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.

deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). Specific libraries may implement deques in different ways, generally as some form of dynamic array. But in any case, they allow for the individual elements to be accessed directly through random access iterators, with storage handled automatically by expanding and contracting the container as needed. Therefore, they provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations: accessing elements in a deque by offsetting a pointer to another element causes undefined behavior.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

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.

#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 this C++ example, we have two functions with "same name" but different number of arguments. Based on how many parameters we pass during Function call "Determines"

Internal method to test if a positive number is prime. Not an efficient algorithm and Internal method to return a 'prime number' at least as large as n. Assumes "n > 0". Insert item x into


First we have to clear about what is transpose of a matrix. Its to replace the number of rows with number of colums, vice versa to make a new matrix. So for example if a matrix 'A' has