C++ Programming Code Examples
C++ > Code Snippets Code Examples
Demonstrating remove() and replace() in vector
/* Demonstrating remove() and replace() in vector */
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<char> vectorObject;
vector<char>::iterator p, p_end;
int i;
for(i = 0; i <5; i++) {
vectorObject.push_back('A'+i);
vectorObject.push_back('A'+i);
vectorObject.push_back('A'+i);
}
cout << "Original contents of vectorObject:";
for(p = vectorObject.begin(); p < vectorObject.end(); p++)
cout << *p << " ";
cout << endl;
p_end = remove(vectorObject.begin(), vectorObject.end(), 'C'); // remove all C's
cout << "Sequence after removing all C's:";
for(p = vectorObject.begin(); p < p_end; p++)
cout << *p << " ";
cout << endl;
replace(vectorObject.begin(), vectorObject.end(), 'D', 'X'); // replace D's with digits X's
cout << "Sequence after replacement:";
for(p = vectorObject.begin(); p < p_end; p++)
cout << *p << " ";
cout << endl;
return 0;
}
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.
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, 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.
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.
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.
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.
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.
Remove value from range. It is defined in <algorithm> library. remove() function removes value from range. Transforms the range [first,last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range. This is the reference for algorithm remove. See remove for <cstdio>'s remove. Transforms the range [first,last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range. The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container): The removal is done by replacing the elements that compare equal to val by the next element that does not, and signaling the new size of the shortened range by returning an iterator to the element that should be considered its new past-the-end element.
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.
Replace value in range. Assigns new_value to all the elements in the range [first,last) that compare equal to old_value. C++ Algorithm replace() function is used to replace all value equal to old_value by the value new_value in the range [first, last). The function uses operator== to compare the individual elements to old_value. C++ Algorithm replace() function is used to replace all value equal to old_value by the value new_value in the range [first, last). This function examines each element in the range and replaces it if it matches a specified value.
'Sum of digits' means add all the digits of any number, we take any number like 358. Its sum of all digit is '3+5+8=16'. Using given code we can easily write c++ program. Enters any num
To convert "Hexadecimal" number to "binary" number in C++, you have to ask to the user to enter the hexadecimal number to convert it into binary number to display the equivalent
Find all 'factors' of an integer using for loop & if statement. Takes a positive integer from an user & displays all the factors of that number. In this program, an integer entered by user is
Operators declared on the type list. Reading a list from input. Writing a list on the output. Gets a list and returns its length. Getting an element and returns its positon in the list. If