C++ Programming Code Examples
C++ > Code Snippets Code Examples
Accessing a Vector Through an Iterator
/* Accessing a Vector Through an Iterator */
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<char> vectorObject(10);
vector<char>::iterator p; // create an iterator
int i;
p = vectorObject.begin();
i = 0;
while(p != vectorObject.end()) {
*p = i + 'a';
p++;
i++;
}
cout << "Original contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << "\n\n";
// change contents of vector
p = vectorObject.begin();
while(p != vectorObject.end()) {
*p = toupper(*p);
p++;
}
// display contents of vector
cout << "Modified Contents:\n";
p = vectorObject.begin();
while(p != vectorObject.end()) {
cout << *p << " ";
p++;
}
cout << endl;
return 0;
}
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.
Convert lowercase letter to uppercase. The toupper() function in C++ converts a given character to uppercase. It is defined in the cctype header file. Converts c to its uppercase equivalent if c is a lowercase letter and has an uppercase equivalent. If no such conversion is possible, the value returned is c unchanged. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, a lowercase letter is any of: a b c d e f g h i j k l m n o p q r s t u v w x y z, which translate respectively to: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z. The toupper() function takes the following parameter: c - a character, casted to int type or EOF. Function returns the uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.
#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 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.
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.
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.
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.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
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.
There are different "Preprocessor Directives" that perform different tasks. Lets categorize preprocessor directives. Inclusion directives: "#include": specifies the files to be included,
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
In this, Merge rhs into the priority queue. rhs becomes empty. rhs must be different from this and Internal method to merge two roots. Deals with "deviant cases" and calls recursive
To sort strings in alphabetical order, you have to ask to the user to enter the two string, now start comparing the strings, and if found then make a t variable of same type, and place the