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++ > Beginners Lab Assignments Code Examples

C++ Program to Implement Multiset in STL

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/* C++ Program to Implement Multiset in STL This C++ Program demonstrates implementation of Multiset in STL. */ #include <iostream> #include <set> #include <string> #include <cstdlib> using namespace std; int main() { multiset<int> ms; multiset<int>::iterator it, it1; int choice, item; while (1) { cout<<"\n---------------------"<<endl; cout<<"Multiset Implementation in Stl"<<endl; cout<<"\n---------------------"<<endl; cout<<"1.Insert Element into the Multiset"<<endl; cout<<"2.Delete Element from the Multiset"<<endl; cout<<"3.Find Element in a Multiset"<<endl; cout<<"4.Count Elements with a specific key"<<endl; cout<<"5.Size of the Multiset"<<endl; cout<<"6.Display Multiset"<<endl; cout<<"7.Exit"<<endl; cout<<"Enter your Choice: "; cin>>choice; switch(choice) { case 1: cout<<"Enter value to be inserted: "; cin>>item; if (ms.empty()) it1 = ms.insert(item); else it1 = ms.insert(it1, item); break; case 2: cout<<"Enter value to be deleted: "; cin>>item; ms.erase(item); break; case 3: cout<<"Enter element to find "; cin>>item; it = ms.find(item); if (it != ms.end()) cout<<"Element found"<<endl; else cout<<"Element not found"<<endl; break; case 4: cout<<"Enter element to be counted: "; cin>>item; cout<<item<<" appears "<<ms.count(item)<<" times."<<endl; break; case 5: cout<<"Size of the Multiset: "<<ms.size()<<endl; break; case 6: cout<<"Elements of the Multiset: "; for (it = ms.begin(); it != ms.end(); it++) cout<<*it<<" "; cout<<endl; break; case 7: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; }

Return container size. Returns the number of elements in the multiset container. C++ Multiset size() function is used to find the number of elements present in the multiset container. size() function returns the number of elements in the container, i.e. std::distance(begin(), end()). No parameter is required.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

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.

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 iterator to beginning. Returns an iterator referring to the first element in the multiset container. The multiset::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the multiset container. Since multiset always contains elements in an ordered way, begin() always points to the first element according to the sorting criterion. Because multiset containers keep their elements ordered at all times, begin points to the element that goes first following the container's sorting criterion. If the container is empty, the returned iterator value shall not be dereferenced. This function does not accept any parameter.

Insert element. Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. Internally, multiset containers keep all their elements sorted following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering. The relative ordering of equivalent elements is preserved, and newly inserted elements follow their equivalents already in the container. The parameters determine how many elements are inserted and to which values they are initialized: The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last). Notice that this is just a hint and does not force the new element to be inserted at that position within the multiset container (the elements in a multiset always follow a specific order).

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

Test whether container is empty. Returns whether the multiset container is empty (i.e. whether its size is 0). The multiset::empty() function is a built-in function in C++ STL which checks if the multiset is empty or not. It returns true if the multiset is empty, else it returns false. This function does not modify the container in any way. To clear the content of a multiset container, see multiset::clear.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

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.

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

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 predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

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.

Erase elements. Removes elements from the multiset container. The multiset::erase() is the STL function in C++ removes the specified element from multiset. This effectively reduces the container size by the number of elements removed, which are destroyed. The parameters determine the elements removed: Member types iterator and const_iterator are bidirectional iterator types that point to elements. The erase() function returns an iterator that point to the next element of the deleted element or returns the number of deleted elements.

Get iterator to element. Searches the container for an element equivalent to val and returns an iterator to it if found, otherwise it returns an iterator to multiset::end. The C++ multiset::find function is used to search the container for an element equivalent to the specified value and returns the iterator to it if found, else returns the iterator to multiset::end. Notice that this function returns an iterator to a single element (of the possibly multiple equivalent elements). To obtain the entire range of equivalent elements, see multiset::equal_range. Two elements of a multiset are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the elements are passed as arguments).

#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 multiset container. The multiset::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container. The past-the-end element is the theoretical element that would follow the last element in the multiset 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 multiset::begin to specify a range including all the elements in the container.

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.

Count elements with a specific key. Searches the container for elements equivalent to val and returns the number of matches. Two elements of a multiset are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the elements are passed as arguments). C++ Multiset count() function is used to return the number of elements found in the container. Since, the multiset container does not contain any duplicate element, this function actually returns 1 if the element with value val is present in the multiset container otherwise, it returns 0. Function returns the number of elements in the container that are equivalent to val.

Multiple-key set. Multisets are containers that store elements following a specific order, and where multiple elements can have equivalent values. Multisets are part of the C++ STL (Standard Template Library). Multisets are the associative containers like Set that stores sorted values (the value is itself the key, of type T), but unlike Set which store only unique keys, multiset can have duplicate keys. By default it uses < operator to compare the keys. The value of the elements in a multiset can be inserted or deleted but cannot be altered (The elements are always const). In a multiset, the value of an element also identifies it (the value is itself the key, of type T). The value of the elements in a multiset cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

This function reads n lines from a file. Returns the amount of bytes read. Try to open txt file. Try to locate the starting line. Set the starting position. Read the lines. Close the file. Return

C++ Program demonstrates the Removal of All the nodes which don't lie in any Path with Sum greater than or equal to K. Create a new 'Binary Tree node' with given data. Truncates

This is a C++ Program to find "shortest path". Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like "Prim's MST", we generate a SPT with given

Program to solve a matching problem. Given N men and N women, where each person has ranked all members of the opposite gender in order of preference, marry the men & women

Linear search is method for searching a value within an array. It 'sequentially' checks one by one of the arrays for the 'target element' until 'match is found' or until all the elements have