C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset end()

C++ multiset end()

C++ multiset end()函数用于返回迭代器,该迭代器位于 multiset 的最后一个条目旁边。
注意: 注意:-这是一个占位符。此位置没有元素,尝试访问是未定义的行为。

语法

      iterator end();                            //until C++ 11
const_iterator end() const;                //until C++ 11
      iterator end() noexcept;              //since C++ 11
const_iterator end() const noexcept;  //since C++ 11

参数

返回值

它返回一个指向最后一个迭代器的迭代器

复杂度

常量。

迭代器有效性

不变。

数据竞争

同时访问 multiset 的元素是安全的。
无论是非常量访问还是非访问容器,都不能访问容器。 const版本会修改容器。

异常安全

此成员函数从不会抛出异常。

示例1

让我们看一下end()函数的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<string> mymultiset = {"Java", "C++", "Java", "C++"};
  // show content:
  for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';
  return 0;
}
输出:
C++
C++
Java
Java
在上面的示例中,使用end()函数返回指向mymultiset multiset 中的最后一个元素旁边的迭代器。

示例2

让我们看一个简单的示例,使用for-each循环遍历 multiset :
#include <iostream>
#include <set>
using namespace std;
int main()
{
  multiset<int> c;
  c.insert(5);
  c.insert(2);
  c.insert(1);
  c.insert(1);
  c.insert(0);
  c.insert(0);
  c.insert(2);
  multiset<int>::iterator i = c.begin();
  while (i != c.end())
    cout << *i++ << " ";
  cout << endl;
}
输出:
0 0 1 1 2 2 5

示例3

让我们看一个使用while循环遍历 multiset 的简单示例:
#include <iostream>
#include <set>
#include <string>
int main()
{
    using namespace std;
 
      multiset<string> mymultiset = { "Nikita","Deep","Deep","Sonu","Aman" };
 cout<<"Elements of mymultiset are: "<<endl;
    multiset<string>::const_iterator it; // declare an iterator
    it = mymultiset.begin(); // assign it to the start of the multiset
    while (it != mymultiset.end()) // while it hasn't reach the end
    {
cout << *it << "\n"; 
// print the value of the element it points to
++it; // and iterate to the next element
    }
 
    cout << endl;
}
输出:
Elements of mymultiset are: 
Aman
Deep
Deep
Nikita
Sonu
在上面的示例中,使用end()函数返回指向mymultiset multiset 中的最后一个元素旁边的迭代器。

示例4

让我们看一个简单的例子:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
    int val;
    multiset<int> c = {10,20,10,20,40,50};
 
    cout<<"Enter value to find: ";
    cin>>val;
    auto result = c.find(val);  
    
    //find until end of the multiset elements
    if (result != c.end()) {  
        cout << "Element found: "<< *result; 
        cout << endl;  
    } else {  
        cout << "Element not found." << endl;  
    }  
    
  return 0;
}
输出:
Enter value to find: 60
Element not found.
Enter value to find: 20
Element found: 20
在上面的示例中,使用end()函数返回指向mymultiset multiset 中最后一个元素旁边的迭代器。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4