C++ Programming Code Examples
C++ > Code Snippets Code Examples
Right-rotate a sequence in vector
/* Right-rotate a sequence in vector */
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vectorObject;
vector<int>::iterator p;
int i;
for(i = 0; i <10; i++)
vectorObject.push_back(i);
cout << "Original ordering: ";
for(p=vectorObject.begin(); p<vectorObject.end(); p++)
cout << *p << " ";
cout << endl;
// rotate right two positions using reverse iterators
rotate(vectorObject.rbegin(), vectorObject.rbegin()+2, vectorObject.rend());
cout << "Order after two right rotates: ";
for(p=vectorObject.begin(); p<vectorObject.end(); p++)
cout << *p << " ";
cout << endl;
return 0;
}
Rotate left the elements in range. Rotates the order of the elements in the range [first,last), in such a way that the element pointed by middle becomes the new first element. rotate() function is a library function of algorithm header, it is used to rotate left the elements of a sequence within a given range, it accepts the range (start, end) and a middle point, it rotates the elements in such way that the element pointed by the middle iterator becomes the new first element. ForwardIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable. Function returns an iterator pointing to the element that now contains the value previously pointed by first.
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.
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.
#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 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).
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.
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.
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 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.
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.
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.
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 C++ coding, we take a 'For Loop' which will start from 1 up to number that has entered to check whether it is Prime Number or not with in for loop we set an 'IF' condition and placed
Number of bits in the Bloom filter. Number of bits set per Mapping in Filter. Table of "8-bit" CRC32 remainders. Bloom filter array of M/8 bytes. Number of bytes in Bloom filter. Main
Program ask to the user to enter any number in Hexadecimal to convert it into octal, then display the result on the screen: Here first we will convert the entered hexadecimal number