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 push front - Inserts object x at the front of the deque container.

Deque push front - Inserts object x at the front of the deque container. push_front Header <deque> void push_front(const T& x) Inserts object x at the front of the deque container. Inserting the element invalidates all iterators to the existing elements of the deque container. Sample #include <deque> #include <iostream> int main() { //default constructor std::deque<int> c1 ; //create deque with 10 copies of 4 std::deque<int> c2(10, 4) ; //copy constructor std::deque<int> c3(c2) ; int ai[] = {0, 1, 2, 3, 4, 5} ; int i ; std::deque<int> c4 ; //get_allocator std::deque<int>::allocator_type a1 = c4.get_allocator() ; //push_back for(i = 0; i < 5; i++) c4.push_back(ai[i]) ; //range copy constructor std::deque<int> c5(c4.begin(), c4.end()) ; //begin, end std::cout << "c4 (using begin, end) = " ; std::deque<int>::iterator Iter ; for(Iter = c4.begin(); Iter != c4.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; //rbegin, rend std::cout << "c4 (using rbegin, rend) = " ; std::deque<int>::reverse_iterator RevIter ; for(RevIter = c4.rbegin(); RevIter != c4.rend(); RevIter++) std::cout << *RevIter << ", " ; std::cout << std::endl ; //assign c2.assign(c5.begin(), c5.begin() + 3) ; c1.assign(10, 4) ; //at std::cout << "third element in c1 = " << c1.at(3) << std::endl ; //operator[] std::cout << "c4[3] = " << c4[3] << std::endl ; //back std::cout << "last element in c2 = " << c2.back() << std::endl ; //front std::cout << "first element in c2 = " << c2.front() << std::endl ; //size std::cout << "number of elements in c2 = " << c2.size() << std::endl ; //max_size std::cout << "max number of elements c2 can hold using current allocator = " << c2.max_size() << std::endl ; //erase c3.erase(c3.begin(), c3.begin() + 4) ; //clear c2.clear() ; //empty if (c2.empty() == true) std::cout << "c2 is now empty" << std::endl ; //resize c2.resize(10, 30) ; std::cout << "number of elements in c2 = " << c2.size() << std::endl ; std::cout << "last element in c2 = " << c2.back() << std::endl ; std::cout << "first element in c2 = " << c2.front() << std::endl ; //push_front c2.push_front(25) ; std::cout << "first element in c2 = " << c2.front() << std::endl ; //pop_back c2.pop_back() ; std::cout << "last element in c2 = " << c2.back() << std::endl ; //pop_front c2.pop_front() ; std::cout << "first element in c2 = " << c2.front() << std::endl ; //swap c3.swap(c2) ; std::cout << "number of elements in c3 = " << c3.size() << std::endl ; std::cout << "last element in c3 = " << c3.back() << std::endl ; std::cout << "first element in c3 = " << c3.front() << std::endl ; //insert c1.insert(c1.begin(), 20) ; c1.insert(c1.begin()+1, 4, 10) ; c1.insert(c1.begin()+2, c5.begin(), c5.end()) ; std::cout << "c1 = " ; for(Iter = c1.begin(); Iter != c1.end(); Iter++) std::cout << *Iter << ", " ; std::cout << std::endl ; return 0 ; } Program Output c4 (using begin, end) = 0, 1, 2, 3, 4, c4 (using rbegin, rend) = 4, 3, 2, 1, 0, third element in c1 = 4 c4[3] = 3 last element in c2 = 2 first element in c2 = 0 number of elements in c2 = 3 max number of elements c2 can hold using current allocator = 1073741823 c2 is now empty number of elements in c2 = 10 last element in c2 = 30 first element in c2 = 30 first element in c2 = 25 last element in c2 = 30 first element in c2 = 30 number of elements in c3 = 9 last element in c3 = 30 first element in c3 = 30 c1 = 20, 10, 0, 1, 2, 3, 4, 10, 10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,

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

Return reverse iterator to reverse end. Returns a reverse iterator pointing to the theoretical element preceding the first element in the deque container (which is considered its reverse end). The deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). The range between deque::rbegin and deque::rend contains all the elements of the deque container (in reverse order). This function does not accept any parameter. Function returns a reverse iterator to the reverse end of the sequence container.

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.

Access element. Returns a reference to the element at position n in the deque container object. The function automatically checks whether n is within the bounds of valid elements in the container, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds. Returns a reference to the element at position n in the deque container object. The difference between this member function and member operator function operator[] is that deque::at signals if the requested position is out of range by throwing an out_of_range exception.

Access element. Returns a reference to the element at position n in the deque container. This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at() function throws an out-of-range exception when the position is not in the bounds of the size of deque, while this operator causes undefined behavior. A similar member function, deque::at, has the same behavior as this operator function, except that deque::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception. Function returns the element at the specified position in the container.

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.

Test whether container is empty. Returns whether the deque container is empty (i.e. whether its size is 0). empty() function is used to check if the deque container is empty or not. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file. The function either returns true or false, depending on whether the deque is empty or not. This function does not modify the container in any way. To clear the content of a deque container, see deque::clear. Function takes no parameter. Function returns true if the container size is 0, false otherwise.

Access first element. Returns a reference to the first element in the deque container. The C++ function std::deque::front() returns a reference to the first element of the deque. Unlike member deque::begin, which returns an iterator to this same element, this function returns a direct reference. Calling this function on an empty container causes undefined behavior. No parameter is required. Function returns a reference to the first element in the deque container.

Function returns the maximum number of elements that the deque container can hold. The deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. This is the maximum potential size the container can reach due to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it can still fail to allocate storage at any point before that size is reached. It does not contain any parameter. Function returns the maximum number of elements a deque container can hold as content.

Clear content. Removes all elements from the deque (which are destroyed), leaving the container with a size of 0. The C++ function std::deque::clear() destroys the deque by removing all elements from the deque and sets size of deque to zero. Erases all elements from the container. After this call, size() returns zero. Invalidates any references, pointers, or iterators referring to contained elements. Any past-the-end iterators are also invalidated. This function does not accept any parameter. This function does not return any value.

Insert elements. The deque container is extended by inserting new elements before the element at the specified position. This effectively increases the container size by the amount of elements inserted. C++ Deque insert() function inserts new element just before the specified position pos and the size of the container increases by the number of elements are inserted. Insertion of an element can be done either from front or from the back. Double-ended queues are designed to be efficient performing insertions (and removals) from either the end or the beginning of the sequence. Insertions on other positions are usually less efficient than in list or forward_list containers. The parameters determine how many elements are inserted and to which values they are initialized:

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.

Change size. Resizes the container so that it contains n elements. C++ Deque resize() function changes the size of the deque container to the size given in the argument. If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them). If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized. Notice that this function changes the actual content of the container by inserting or erasing elements from it.

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.

This class reverses the direction in which a bidirectional or random-access iterator iterates through a range. A copy of the original iterator (the base iterator) is kept internally and used to reflect the operations performed on the reverse_iterator: whenever the reverse_iterator is incremented, its base iterator is decreased, and vice versa. A copy of the base iterator with the current state can be obtained at any time by calling member base. Notice however that when an iterator is reversed, the reversed version does not point to the same element in the range, but to the one preceding it. This is so, in order to arrange for the past-the-end element of a range: An iterator pointing to a past-the-end element in a range, when reversed, is pointing to the last element (not past it) of the range (this would be the first element of the reversed range). And if an iterator to the first element in a range is reversed, the reversed iterator points to the element before the first element

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.

Assign container content. Assigns new contents to the deque container, replacing its current contents, and modifying its size accordingly. The deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. In the range version (1), the new contents are elements constructed from each of the elements in the range between first and last, in the same order. In the fill version (2), the new contents are n elements, each initialized to a copy of val. In the initializer list version (3), the new contents are copies of the values passed as initializer list, in the same order.

Delete last element. Removes the last element in the deque container, effectively reducing the container size by one. pop_back() function is used to pop or remove elements from a deque from the back. The value is removed from the deque from the end, and the container size is decreased by 1. This destroys the removed element. This function does not accept any parameter. This function does not return any value.

Access last element. Returns a reference to the last element in the container. The C++ function std::deque::back() returns a reference to the last element of the deque. Calling this function on empty causes undefined behavior. Unlike member deque::end, which returns an iterator just past this element, this function returns a direct reference. Calling this function on an empty container causes undefined behavior. It does not contain any parameter.

Erase elements. Removes from the deque container either a single element (position) or a range of elements ([first,last)). C++ Deque erase() function removes the element from the specified position or range and this effectively reduces the size of the deque by the number of elements removed. This effectively reduces the container size by the number of elements removed, which are destroyed. Double-ended queues are designed to be efficient removing (and inserting) elements at either the end or the beginning of the sequence. Removals on other positions are usually less efficient than in list or forward_list containers.

Return reverse iterator to reverse beginning. Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning). Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. rbegin points to the element right before the one that would be pointed to by member end. Notice that unlike member deque::back, which returns a reference to this same element, this function returns a reverse random access iterator. This function does not accept any parameter. Function returns a reverse iterator to the reverse beginning of the sequence container.

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.

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.

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.

Return size. Returns the number of elements in the deque container. deque::size() is an inbuilt function in C++ STL which is declared in header file. deque::size() returns the size of the deque container associated with the function. If the container has no elements then the function returns 0. size() function is used to return the size of the deque container or the number of elements in the deque container. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file. The function either returns a number demonstrating the total elements the deque holds at that instance.

Insert element at beginning. Inserts a new element at the beginning of the deque container, right before its current first element. The content of val is copied (or moved) to the inserted element. This effectively increases the container size by one. deque::push_front() is an inbuilt function in C++ STL which is declared in header file. deque::push_front() is used to push/insert an element at the front or at the beginning of the deque container making the pushed/inserted element as the first element of the deque. This function accepts one argument, that is, the element which is to be pushed/inserted at the beginning.

Get allocator. Returns a copy of the allocator object associated with the deque object. public member function std::deque::get_allocator gets allocator. deque::get_allocator() is a built in function in C++ STL which is used to get allocator of container deque. This function does not accept any parameter. Function returns the allocator. Member type allocator_type is the type of the allocator used by the container, defined in deque as an alias of its second template parameter (Alloc).

Delete first element. Removes the first element in the deque container, effectively reducing its size by one. This destroys the removed element. The C++ deque::pop_front function is used to delete the first element of the deque. Every deletion of element results into reducing the container size by one unless the deque is empty. Removes the first element of the container. If there are no elements in the container, the behavior is undefined. Iterators and references to the erased element are invalidated. If the element is the last element in the container, the past-the-end iterator is also invalidated. Other references and iterators are not affected. This function does not accept any parameter. This function does not return any value.

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.