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

Deque operatorlt - Returns true if d1 is lexicographically less than d2

Deque operatorlt - Returns true if d1 is lexicographically less than d2 operator< Header <deque> template<class T, class A> bool operator<(const deque<T, A>&d1, const deque<T, A>&d2) ; Returns true if d1 is lexicographically less than d2, returns false otherwise. The function returns the result of: lexicographical_compare(d1.begin(), d1.end(), d2.begin()) The operator may be defined in terms of another operator. Basically, the two sequences are compared lexicographically . Lexicographical comparison is a dictionary-like comparison in which two words are compared for ordering. Assuming the sequences are represented by [first1, last1) and [first2, last2), the comparison works as follows: traverse the sequences comparing corresponding pair of elements e1 and e2: * if e1 < e2, return true, else * if e2 < e2, return false, otherwise * continue to next corresponding pair of elements. If the first sequence gets exhausted, but the second is not, return true, otherwise return false. Sample #include <deque> #include <iostream> int main() { std::deque<int> c1, c2, c3, c4 ; int i ; for (i = 0; i < 10; i++) { c1.push_back(i) ; c2.push_back(i*i) ; c3.push_back(i*i*i) ; c4.push_back(i) ; } if (c1 == c4) std::cout << "c1 == c4" << std::endl ; if (c2 != c3) std::cout << "c2 != c3" << std::endl ; if(c2 < c3) std::cout << "c2 < c3" << std::endl ; if(c3 > c2) std::cout << "c3 > c2" << std::endl ; c4.push_back(29) ; if (c1 <= c4) std::cout << "after c4.push_back(29), c1 <= c4" << std::endl ; if (c3 >= c2) std::cout << "c3 >= c2" << std::endl ; std::swap(c3, c2) ; std::cout << "after swapping c3 with c2, " ; if (c3 >= c2) std::cout << "c3 >= c2" << std::endl ; else std::cout << "c3 < c2" << std::endl ; return 0 ; } Program Output c1 == c4 c2 != c3 c2 < c3 c3 > c2 after c4.push_back(29), c1 <= c4 c3 >= c2 after swapping c3 with c2, c3 < c2

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.

Lexicographical less-than comparison. Returns true if the range [first1,last1) compares lexicographically less than the range [first2,last2). The C++ function std::algorithm::lexicographical_compare() tests whether one range is lexicographically less than another or not. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries; It involves comparing sequentially the elements that have the same position in both ranges against each other until one element is not equivalent to the other. The result of comparing these first non-matching elements is the result of the lexicographical comparison. If both sequences compare equal until one of them ends, the shorter sequence is lexicographically less than the longer one.

Add element at the end. Adds a new element at the end of the deque container, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one. push_back() function is used to push elements into a deque from the back. The new value is inserted into the deque at the end, before the current last element and the container size is increased by 1. 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.

Swap content. Exchanges the content of the container by the content of x, which is another deque object containing elements of the same type. Sizes may differ. After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this. All iterators, references and pointers remain valid for the swapped objects. Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function. This function does not return any value.

Relational operators for deque. Performs the appropriate comparison operation between the deque containers lhs and rhs. The equality comparison (operator==) is performed by first comparing sizes, and if they match, the elements are compared sequentially using operator==, stopping at the first mismatch (as if using algorithm equal). The less-than comparison (operator<) behaves as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< in a reciprocal manner (i.e., checking both a<b and b<a) and stopping at the first occurrence. Function returns true if the condition holds, and false otherwise.

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.

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.

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.

Exchange values of two objects. Exchanges the values of a and b. C++ Utility swap() function swaps or say interchanges the values of two containers under reference. The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables. This function does not return any value.

Equality operation on deques. The C++ <deque> operator== function is used to check whether two deques are equal or not. It returns true if two deques are equal, else returns false. operator== compares elements of deques sequentially and stops comparison after first mismatch. The C++ function std::deque::operator== tests whether two deque are same or not. Function returns true if the contents of lhs are equal to the contents of rhs, else returns false.


To print "Pascal Triangle" in C++, you have to enter the Number of Line. So to Print "Pascal Triangle", you have to use three For Loops as shown here in the C++ Programming samples





C++ Program finds vertex connectivity of a graph. A vertex in an undirected connected graph is an articulation point iff removing it disconnects the graph. 'Articulation points'




C++ Program to find the trnasitive closure of a given graph. In mathematics, the transitive closure of a binary relation R on a set X is the "transitive relation" R+ on set X such that R+