在C++ STL中的unOrdEdMultIMAP EMPTY()函数

这个 无序的_multimap::empty() 是C++中的一个内置函数,它返回布尔值。如果无序的_multimap容器为空,则返回true。否则,返回false。

null

语法:

unordered_multimap_name.empty()

参数: 该函数不接受任何参数。

返回值: 它返回一个布尔值,表示无序的_多重映射是否为空。

以下程序说明了上述功能:

项目1:

// C++ program to illustrate the
// unordered_multimap::empty() function
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap< int , int > sample;
// inserts key and element
sample.insert({ 1, 2 });
sample.insert({ 1, 2 });
sample.insert({ 2, 3 });
sample.insert({ 3, 4 });
sample.insert({ 5, 6 });
// if not empty then print the elements
if (sample.empty() == false ) {
cout << "Key and Elements: " ;
for ( auto it = sample.begin(); it != sample.end(); it++) {
cout << "{" << it->first << ":" << it->second << "} " ;
}
}
// container is erased completely
sample.clear();
if (sample.empty() == true )
cout << "Container is empty" ;
return 0;
}


输出:

Key and Elements: {5:6} {3:4} {2:3} {1:2} {1:2} 
Container is empty

项目2:

// C++ program to illustrate the
// unordered_multimap::empty()
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
// declaration
unordered_multimap< char , char > sample;
// inserts element
sample.insert({ 'a' , 'b' });
sample.insert({ 'a' , 'b' });
sample.insert({ 'g' , 'd' });
sample.insert({ 'r' , 'e' });
sample.insert({ 'g' , 'd' });
// if not empty then print the elements
if (sample.empty() == false ) {
cout << "Key and elements: " ;
for ( auto it = sample.begin(); it != sample.end(); it++) {
cout << "{" << it->first << ":" << it->second << "} " ;
}
}
// container is erased completely
sample.clear();
if (sample.empty() == true )
cout << "Container is empty" ;
return 0;
}


输出:

Key and elements: {r:e} {g:d} {g:d} {a:b} {a:b} 
Container is empty

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享