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参数
空
返回值
它返回一个指向多重集最后一个元素旁边的迭代器。
复杂度
恒定。
迭代器有效性
没有变化。
数据竞争
同时访问多重集的元素是安全的。
容器不会被访问,非常量和常量版本都不会修改容器。
异常安全
此成员函数从不抛出异常。
例子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 多集中最后一个元素旁边的迭代器。
例子2
让我们看一个使用 for-each 循环迭代多重集的简单示例:
#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 循环遍历多重集的简单示例:
#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 多集中最后一个元素旁边的迭代器。
示例 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 多集中最后一个元素旁边的迭代器。
相关用法
- C++ multiset emplace()用法及代码示例
- C++ multiset empty()用法及代码示例
- C++ multiset equal_range()用法及代码示例
- C++ multiset emplace_hint()用法及代码示例
- C++ multiset erase()用法及代码示例
- C++ multiset begin()、end()用法及代码示例
- C++ multiset value_comp()用法及代码示例
- C++ multiset lower_bound()用法及代码示例
- C++ multiset crbegin()、crend()用法及代码示例
- C++ multiset insert()用法及代码示例
- C++ multiset max_size()用法及代码示例
- C++ multiset crbegin()用法及代码示例
- C++ multiset rbegin()、rend()用法及代码示例
- C++ multiset count()用法及代码示例
- C++ multiset find()用法及代码示例
- C++ multiset cbegin()用法及代码示例
- C++ multiset begin()用法及代码示例
- C++ multiset size()用法及代码示例
- C++ multiset get_allocator()用法及代码示例
- C++ multiset rend()用法及代码示例
注:本文由纯净天空筛选整理自 C++ multiset end()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
