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++ > Code Snippets Code Examples

Set operatrions: insert, size, begin, end

/* Set operatrions: insert, size, begin, end */ #include <iostream> #include <string> #include <set> using namespace std; int main() { set<string> setObject; setObject.insert( "a" ); setObject.insert( "b" ); setObject.insert( "c" ); setObject.insert( "d" ); setObject.insert( "c" ); // attempting a duplicate cout << setObject.size() << endl;; typedef set<string>::const_iterator CI; for (CI iter = setObject.begin(); iter != setObject.end(); iter++) cout << *iter << " "; setObject.erase( "b" ); cout << setObject.size() << endl; for ( CI iter = setObject.begin(); iter != setObject.end(); iter++ ) cout << *iter << " "; return 0; }

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.

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

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 iterator to end. Returns an iterator referring to the past-the-end element in the set container. end() function returns an iterator pointing to past the last element of the set container. Since it does not refer to a valid element, it cannot de-referenced end() function returns a bidirectional iterator. The past-the-end element is the theoretical element that would follow the last element in the set 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 set::begin to specify a range including all the elements in the container. If the container is empty, this function returns the same as set::begin.

Insert element. Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. Because elements in a set are unique, the insertion operation checks whether each inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value). The C++ set::insert function is used to insert new elements in the container. This results into increasing the set size by the number of elements inserted. As the elements in a set are unique, therefore the insertion operation first checks if the inserted element is unique to the set then the element is inserted. For a similar container allowing for duplicate elements, see multiset.

Sets are part of the C++ STL (Standard Template Library). Sets are the associative containers that stores sorted key, in which each key is unique and it can be inserted or deleted but cannot be altered. Sets are containers that store unique elements following a specific order. In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container. Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

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.

Return container size. Returns the number of elements in the set container. set::size() function is a predefined function, it is used to get the size of a set, it returns the total number of elements of the set container. Sets are containers that store unique elements following a specific order. Internally, the elements in a set are always sorted. Sets are typically implemented as binary search trees. This function does not accept any parameter.

Erase elements. Removes from the set container either a single element or a range of elements ([first,last)). C++ set erase() function is used to remove either a single element associated with given key or a range of elements ([first, last)) from the set container. Hence, the size will be reduced by the number of elements removed. This effectively reduces the container size by the number of elements removed, which are destroyed.

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.

Return iterator to beginning. Returns an iterator referring to the first element in the set container. Because set containers keep their elements ordered at all times, begin points to the element that goes first following the container's sorting criterion. The C++ set::begin function returns the iterator pointing to the first element of the set. Please note that, Set is an ordered data container which implies all its elements are ordered all the time. If the container is empty, the returned iterator value shall not be dereferenced. This function does not accept any parameter.

Program ask to the user to enter any two 3*3 array elements to subtract them i.e., Matrix1 - Matrix2, then display the "subtracted" result of the two matrices ( Matrix3 ) on the screen:

Takes an "arithmetic operator" (+, -, *, /) and two operands from an user and performs the operation on those two operands depending upon the operator entered by user. Program

To convert the uppercase string to lowercase string in 'C++', ask to enter a string, now start converting Lowercase to Uppercase by using ASCII values. To convert Lowercase String to