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

Vector rend - Returns a const reverse iterator value

Vector rend - Returns a const reverse iterator value rend Header <vector> const_reverse_iterator rend() const Returns a const reverse iterator value, which points to just before the first element in the vector container. The value returned by rend() should not be dereferenced. rend() is primarily used to return a value that can be tested to see if a reverse iterator has reached the end of its container. reverse_iterator rend() Returns a reverse iterator value, which points to just before the first element in the vector container. The value returned by rend() should not be dereferenced. rend() is primarily used to return a value that can be tested to see if a reverse iterator has reached the end of its container. Sample #include <vector> #include <iostream> int main() { //default constructor std::vector<int> c1 ; //create vector with 10 copies of 4 std::vector<int> c2(10, 4) ; //copy constructor std::vector<int> c3(c2) ; int ai[] = {0, 1, 2, 3, 4, 5} ; int i ; //range copy constructor std::vector<int> c4(ai, ai+5) ; std::vector<int> c5 ; //push_back for(i = 0; i < 5; i++) c5.push_back(ai[i]) ; //get_allocator std::vector<int>::allocator_type a1 = c4.get_allocator() ; //begin, end std::cout << "c4 (using begin, end) = " ; std::vector<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::vector<int>::reverse_iterator RevIter ; for(RevIter = c4.rbegin(); RevIter != c4.rend(); RevIter++) std::cout << *RevIter << ", " ; std::cout << std::endl ; //assign c2.assign(ai, ai+4) ; 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 ; //reserve, capacity c2.reserve(70) ; std::cout << "current capacity of c2 = " << c2.capacity() << std::endl ; //pop_back c2.pop_back() ; std::cout << "last element in c2 = " << c2.back() << 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 = 3 first element in c2 = 0 number of elements in c2 = 4 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 current capacity of c2 = 70 last 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,

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.

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

Access last element. back() function returns a reference to the last element in the vector. vector::back() is a library function of "vector" header, it is used to access the last element from the vector, it returns a reference to the last element of the vector. Unlike member vector::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. This function does not accept any parameter. Function returns a reference to the last element in the vector.

Access element. Returns a reference to the element at position n in the vector container. A similar member function, vector::at, has the same behavior as this operator function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception. Portable programs should never call this function with an argument n that is out of range, since this causes undefined behavior. Function returns the element at the specified position in the vector.

The C++ vector has many member functions. Two of them are clear() and erase(). clear() "removes" all the elements of the vector. erase() "removes" a single element or a range of elements. vector::clear() is a library function of "vector" header, it is used to remove/clear all elements of the vector, it makes the 0 sized vector after removing all elements. The C++ vector::clear function is used to clear all elements of the vector. This function makes the vector empty with a size of zero. The function does not accept any parameter. The function does not return any type.

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

Erase elements. Removes from the vector either a single element (position) or a range of elements ([first,last)). This effectively reduces the container size by the number of elements removed, which are destroyed. Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to relocate all the elements after the segment erased to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).

Return iterator to end. Returns an iterator referring to the past-the-end element in the vector container. The past-the-end element is the theoretical element that would follow the last element in the vector. 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 vector::begin to specify a range including all the elements in the container. If the container is empty, this function returns the same as vector::begin. This function does not accept any parameter.

Get allocator. Returns a copy of the allocator object associated with the vector. The C++ function std::vector::get_allocator() returns an allocator associated with vector. public member function std::vector::get_allocator gets allocator. No parameter is required. Function returns the allocator.

Swap content. Exchanges the content of the container by the content of x, which is another vector object 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.

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 vector. This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity. vector::size() is a library function of "vector" header, it is used to get the size of a vector, it returns the total number of elements in the vector. The dynamic array can be created by using a vector in C++. One or more elements can be inserted into or removed from the vector at the run time that increases or decreases the size of the vector. The size or length of the vector can be counted using any loop or the built-in function named size(). This function does not accept any parameter.

Assign vector content. Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly. The C++ function std::vector::assign() assign new values to the vector elements by replacing old ones. It modifies size of vector if necessary. If memory allocation happens allocation is allocated by internal allocator. This function does not return any value.

Return reverse iterator to reverse end. Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (which is considered its reverse end). The C++ vector::rend function returns the reverse iterator pointing to the element preceding the first element (reversed past-the-last element) of the vector. A reverse iterator iterates in backward direction and increasing it results into moving to the beginning of the vector container. Similarly, decreasing a reverse iterator results into moving to the end of the vector container. The range between vector::rbegin and vector::rend contains all the elements of the vector (in reverse order).

Access first element. Returns a reference to the first element in the vector. The C++ function std::vector::front() returns a reference to the first element of the vector. Unlike member vector::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. This function does not accept any parameter. Function returns a reference to the first element in the vector container.

Insert elements. The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted. This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity. Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list). The parameters determine how many elements are inserted and to which values they are initialized:

Return reverse iterator to reverse beginning. Returns a reverse iterator pointing to the last element in the vector (i.e., its reverse beginning). vector::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. 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 vector::back, which returns a reference to this same element, this function returns a reverse random access iterator.

Add element at the end. Adds a new element at the end of the vector, 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, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity. push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1. 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.

Change size. Resizes the container so that it contains n elements. The vector is a very useful class of C++ for creating the dynamic array. The size of the vector can be changed at any time to solve any programming problem. Many built-in functions exist in C++ for doing the different types of tasks in a vector container. The resize() function is one of them. It is used to change the size of the vector. The vector size can be increased or decreased by using this function. This function does not return any value. If a reallocation happens, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).

Return iterator to beginning. Returns an iterator pointing to the first element in the vector. Notice that, unlike member vector::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. The C++ function std::vector::begin() returns a random access iterator pointing to the first element of the vector. This function does not accept any parameter.

Access element. Returns a reference to the element at position n in the vector. The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater than, or equal to, its size). This is in contrast with member operator[], that does not check against bounds. 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.

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library. To use vectors, we need to include the vector header file in our program. The vector class provides various methods to perform different operations on vectors. Add Elements to a Vector: To add a single element into a vector, we use the push_back() function. It inserts an element into the end of the vector. Access Elements of a Vector: In C++, we use the index number to access the vector elements. Here, we use the at() function to access the element from the specified index.

Return maximum size. Returns the maximum number of elements that the vector can hold. The vector::max_size() is a built-in function in C++ STL which returns the maximum number of elements that can be held by the vector container. 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.

Test whether vector is empty. Returns whether the vector is empty (i.e. whether its size is 0). This function does not modify the container in any way. To clear the content of a vector, see vector::clear. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() is a library function of "vector" header, it is used to check whether a given vector is an empty vector or not, it returns a true if the vector size is 0, otherwise it returns false.

Request a change in capacity. Requests that the vector capacity be at least enough to contain n elements. The C++ function std::vector::reserve() requests to reserve vector capacity be at least enough to contain n elements. Reallocation happens if there is need of more space. If n is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater). In all other cases, the function call does not cause a reallocation and the vector capacity is not affected. This function has no effect on the vector size and cannot alter its elements.

Return size of allocated storage capacity. Returns the size of the storage space currently allocated for the vector, expressed in terms of elements. The vector::capacity() function is a built-in function which returns the size of the storage space currently allocated for the vector, expressed in terms of elements. This capacity is not necessarily equal to the vector size. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion. Notice that this capacity does not suppose a limit on the size of the vector. When this capacity is exhausted and more is needed, it is automatically expanded by the container (reallocating it storage space). The theoretical limit on the size of a vector is given by member max_size.

Delete last element. Removes the last element in the vector, effectively reducing the container size by one. The C++ vector::pop_back function is used to delete the last element of the vector. Every deletion of element results into reducing the container size by one unless the vector is empty. This destroys the removed element. This function does not accept any parameter. This function does not return any value.


In graph theory, an "edge coloring" of a graph is an assignment of colors to the edges of the graph so that no two adjacent edges have the same color. Any "2 edges" connected to same







In 'C++ program', user is asked to enter three numbers. Then program finds out the largest number among three numbers and displays it with proper message. Program can be used in