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++ > Visual C++ 5.0 Standard C++ Library Code Examples

Multiset erase - Removes from the multiset container

Multiset erase - Removes from the multiset container erase Header <set> iterator erase(iterator it) Removes from the multiset container, the element positioned at it. iterator erase(iterator first, iterator last) Removes from the multiset container, the elements in the range [first, last). Both of the above return an iterator that designates the first element remaining beyond any elements removed or end() if no such element exists. size_type erase(const Key& key) Removes the multiset elements which match key, and returns the number of elements removed. If none of the elements match key, the function returns 0. Sample #pragma warning(disable: 4786) #include <set> #include <iostream> int main() { //default constructor std::multiset<int> c1 ; int ai[] = {0, 0, 1, 1, 2, 2, 3, 4} ; //construct from a range std::multiset<int> c2(ai, ai + 8) ; //copy constructor std::multiset<int> c3(c2) ; std::multiset<int>::iterator Iter ; std::multiset<int>::reverse_iterator RevIter ; //empty if(c1.empty()) { std::cout << "multiset c1 is empty" << std::endl ; } else { std::cout << "multiset c1 is not empty" << std::endl ; } //begin, end std::cout << "c2 (using begin, end) = " ; for(Iter = c2.begin(); Iter != c2.end(); Iter++) { std::cout << *Iter << " " ; } std::cout << std::endl ; //rbegin, rend std::cout << "c2 (using rbegin, rend) = " ; for(RevIter = c2.rbegin(); RevIter != c2.rend(); RevIter++) { std::cout << *RevIter << " " ; } std::cout << std::endl ; //insert c1.insert(ai, ai+4) ; c1.insert(1) ; //count std::cout << "Number elements in c1 that match 1 = " << c1.count(1) << std::endl ; //find std::multiset<int>::const_iterator constIter = c1.find(3) ; if(constIter != c1.end()) { std::cout << "c1 contains element 3, *constIter = " << *constIter << std::endl ; } //max_size std::cout << "c1.max_size() = " << c1.max_size() << std::endl ; //size std::cout << "c1.size() = " << c1.size() << std::endl ; //swap c1.insert(4) ; c2.swap(c1) ; std::cout << "The last element of c2 = " << *(c2.rbegin()) << std::endl ; //clear c1.clear() ; std::cout << "After calling c1.clear(), c1.size() = " << c1.size() << std::endl ; //get_allocator std::multiset<int>::allocator_type a1 = c1.get_allocator() ; //key_compare std::multiset<int>::key_compare kc = c2.key_comp() ; bool result = kc(2, 3) ; if(result == true) { std::cout << "kc is function object used by c2. kc(2,3) = true" << std::endl ; } else { std::cout << "kc is function object used by c2. kc(2,3) = false" << std::endl ; } //value_comp std::multiset<int>::value_compare vc = c2.value_comp() ; result = vc(10, 4) ; if(result == true) { std::cout << "vc is function object used by c2. vc(10,4) = true" << std::endl ; } else { std::cout << "vc is function object used by c2. vc(10,4) = false" << std::endl ; } //upper_bound std::cout << "* (c2.upper_bound(3)) = " << *(c2.upper_bound(3)) << std::endl ; //lower_bound std::cout << "* (c2.lower_bound(3)) = " << *(c2.lower_bound(3)) << std::endl ; //equal_range std::pair<std::multiset<int>::const_iterator, std::multiset<int>::const_iterator> pr1 = c2.equal_range(3) ; std::cout << "*(pr1.first) = " << *(pr1.first) << "\t" << "*(pr1.second) = " << *(pr1.second) << std::endl ; //erase if(c3.erase(1) != 0) { std::cout << "c3 does not contain 1 any more" << std::endl ; } else { std::cout << "No elements in c3 match key 1" << std::endl ; } if((c2.erase(c2.begin())) != c2.end()) { std::cout << "c2 does not contain 0 any more" << std::endl ; } else { std::cout << "No elements in c2 match key 0" << std::endl ; } c3.erase(c3.begin(), c3.end()) ; std::cout << "after c3.erase(c3.begin(), c3.end()), c3.size() = " << c3.size() << std::endl ; return 0 ; } Program Output multiset c1 is empty c2 (using begin, end) = 0 0 1 1 2 2 3 4 c2 (using rbegin, rend) = 4 3 2 2 1 1 0 0 Number elements in c1 that match 1 = 3 c1.max_size() = 1073741823 c1.size() = 5 The last element of c2 = 4 After calling c1.clear(), c1.size() = 0 kc is function object used by c2. kc(2,3) = true vc is function object used by c2. vc(10,4) = false * (c2.upper_bound(3)) = 4 * (c2.lower_bound(3)) = 4 *(pr1.first) = 4 *(pr1.second) = 4 c3 does not contain 1 any more c2 does not contain 0 any more after c3.erase(c3.begin(), c3.end()), c3.size() = 0

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

This class reverses the direction in which a bidirectional or random-access iterator iterates through a range. A copy of the original iterator (the base iterator) is kept internally and used to reflect the operations performed on the reverse_iterator: whenever the reverse_iterator is incremented, its base iterator is decreased, and vice versa. A copy of the base iterator with the current state can be obtained at any time by calling member base. Notice however that when an iterator is reversed, the reversed version does not point to the same element in the range, but to the one preceding it. This is so, in order to arrange for the past-the-end element of a range: An iterator pointing to a past-the-end element in a range, when reversed, is pointing to the last element (not past it) of the range (this would be the first element of the reversed range). And if an iterator to the first element in a range is reversed, the reversed iterator points to the element before the first element

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.

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.

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.

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

Clear content. Removes all elements from the multiset container (which are destroyed), leaving the container with a size of 0. multiset::clear() function is an inbuilt function in C++ STL, which is defined in <set> header file. This is used to clear the whole multiset container. clear() removes all the elements from the elements of the multiset container and makes the size of the multiset container as 0. Function does not accept any parameter. Function does not returns anything.

In C++, pair is defined as a container in a header library <utility> which combines the two data elements having either the same data types or different data types. In general, the pair in C++ is defined as a tuple in Python programming language which also can give the output as a combined result of joining the two items specified by the pair container and it consists of the first element will be first and the second element will be second only it cannot be disturbed in the order or sequence of elements specified and are always accessed by the dot operator followed by the keyword "first" and "second" elements respectively. In C++ the pair is a container in <utility> header and is also a container class in STL (Standard Template Library) which uses "std" namespace so it will be as std::pair template class for demonstrating pair as a tuple.

#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 comparison object. Returns a copy of the comparison object used by the container. C++ Multiset key_comp() function is used to return a copy of the comparison object which is used by the multiset container to compare keys. By default, this is a less object, which returns the same as operator<. This object determines the order of the elements in the container: it is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.

Get allocator. Returns a copy of the allocator object associated with the multiset. The multiset::get_allocator() method in C++ STL is a built-in function in C++ STL which returns a copy of the allocator object associated with the multiset. Function does not take any parameter. Function returns the allocator.

Return reverse iterator to reverse beginning. Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning). multiset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the multiset container. Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container. rbegin points to the element preceding the one that would be pointed to by member end. The function does not take any parameter. Function returns a reverse iterator to the reverse beginning of the sequence container.

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.

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.

Return maximum size. The multiset::max_size() is a built-in function in C++ STL which returns the maximum number of elements a multiset container can hold. This is the maximum potential size the container can reach due to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it can still fail to allocate storage at any point before that size is reached. This function does not accept any parameters.

Swap content. Exchanges the content of the container by the content of x, which is another multiset of the same type. Sizes may differ. After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this. All iterators, references and pointers remain valid for the swapped objects. Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function. Whether the internal container allocators are swapped is not defined, unless in the case the appropriate allocator trait indicates explicitly that they shall propagate. The internal comparison objects are always exchanged, using swap.

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 end. multiset::rend() in an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the multiset container. Returns a reverse iterator pointing to the theoretical element right before the first element in the multiset container (which is considered its reverse end). The range between multiset::rbegin and multiset::rend contains all the elements of the container, in reverse order. The function does not accepts 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.

Return comparison object. Returns a copy of the comparison object used by the container. C++ Multiset value_comp() function returns a comparison object. This function is used to compare two elements to check whether the key of the first one goes before the second. By default, this is a less object, which returns the same as operator<. This object determines the order of the elements in the container: it is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise.

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.

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.

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.

Return iterator to lower bound. Returns an iterator pointing to the first element in the container which is not considered to go before val (i.e., either it is equivalent or goes after). The C++ multiset::lower_bound function returns an iterator pointing to the first element in the multiset container which is not considered to go before the specified value (it could be either same or goes after the specified value). If all elements of the multiset are considered to go before the specified value, then the iterator points to multiset::end. The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(element,val) would return false. If the multiset class is instantiated with the default comparison type (less), the function returns an iterator to the first element that is not less than val.

Return iterator to upper bound. Returns an iterator pointing to the first element in the container which is considered to go after val. The C++ multiset::upper_bound function returns an iterator pointing to the first element in the multiset container which is considered to go after the specified value. If all elements of the multiset are considered to go before the specified value, then the iterator points to multiset::end. The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(val,element) would return true. If the multiset class is instantiated with the default comparison type (less), the function returns an iterator to the first element that is greater than val.

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

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.

Get range of equal elements. Returns the bounds of a range that includes all the elements in the container that are equivalent to val. The C++ multiset::equal_range function returns the bounds of a range which includes all elements in the multiset container that are equivalent to specified value. It returns a pair, with pair::first member as the lower_bound of the range, and pair::second member as the upper_bound of the range. This contains all elements in the range [pair::first, pair::second). If no matches are found, the range returned has a length of zero, with both iterators pointing to the first element that is considered to go after val according to the container's internal comparison object (key_comp). 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++ Program to compute the 'coefficients' of the DFT (Discrete Fourier Transform) directly. In math, the discrete Fourier transform (DFT) converts finite list of equally spaced samples




The problem takes E edges as input and then outputs whehter vertex cover of size K of the graph exists or not. 'Vertex Cover of a Graph' is, a set of vertices S, such that for every edge




This algorithm takes the input of the number of 'vertex and edges'. Then it takes the input of connected vertex pairs. Print the incidence list representation of the graph. And for each


To 'print smiling face' in C++, first you have to ask how many smiling face user want to print on screen to print required number of smiling face on the screen. So to print smiling face on