C++ Programming Code Examples
C++ > Visual C++ 5.0 Standard C++ Library Code Examples
Map operatorgteq - Returns true if !(m1 < m2).
Map operatorgteq - Returns true if !(m1 < m2).
operator>=
Header
<map>
template<class T, class A>
bool operator>=(const map<T, A>&m1, const map<T, A>&m2) ;
Returns true if !(m1 < m2).
Sample
#include <map>
#include <iostream>
#include <utility>
int main()
{
std::map<int, int> c1, c2, c3, c4 ;
int i ;
typedef std::pair<int, int> INT_PAIR ;
for (i = 0; i < 10; i++)
{
c1.insert(INT_PAIR(i, i)) ;
c2.insert(INT_PAIR(i*i, i*i)) ;
c3.insert(INT_PAIR(i*i*i, i*i*i)) ;
c4.insert(INT_PAIR(i, i)) ;
}
if (c1 == c4)
std::cout << "c1 == c4" << std::endl ;
if (c2 != c3)
std::cout << "c2 != c3" << std::endl ;
if(c2 < c3)
std::cout << "c2 < c3" << std::endl ;
if(c3 > c2)
std::cout << "c3 > c2" << std::endl ;
c4.insert(INT_PAIR(29, 29)) ;
if (c1 <= c4)
std::cout << "after c4.insert(INT_PAIR(29,29)), c1 <= c4" << std::endl ;
if (c3 >= c2)
std::cout << "c3 >= c2" << std::endl ;
std::swap(c3, c2) ;
std::cout << "after swapping c3 with c2, " ;
if (c3 >= c2)
std::cout << "c3 >= c2" << std::endl ;
else
std::cout << "c3 < c2" << std::endl ;
return 0 ;
}
Program Output
c1 == c4
c2 != c3
c2 < c3
c3 > c2
after c4.insert(INT_PAIR(29,29)), c1 <= c4
c3 >= c2
after swapping c3 with c2, c3 < c2
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.
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.
Insert elements. Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of 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). For a similar container allowing for duplicate elements, see multimap. An alternative way to insert elements in a map is by using member function map::operator[].
Relational operators for map. Performs the appropriate comparison operation between the map containers lhs and rhs. The equality comparison (operator==) is performed by first comparing sizes, and if they match, the elements are compared sequentially using operator==, stopping at the first mismatch (as if using algorithm equal). The less-than comparison (operator<) behaves as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< in a reciprocal manner (i.e., checking both a<b and b<a) and stopping at the first occurrence.
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. Maps are part of the C++ STL (Standard Template Library). Maps are the associative containers that store sorted key-value pair, in which each key is unique and it can be inserted or deleted but cannot be altered. Values associated with keys can be changed. The key values are good for sorting and identifying elements uniquely. The mapped values are for storing content associated with the key. The two may differ in types, but the member type combines them via a pair type that combines both.
#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.
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.
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.
Exchange values of two objects. Exchanges the values of a and b. C++ Utility swap() function swaps or say interchanges the values of two containers under reference. The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables. This function does not return any value.