C++ multiset begin() 函數用於返回引用多重集容器第一個元素的迭代器。
用法
iterator begin(); //until C++ 11
const_iterator begin() const; //until C++ 11
iterator begin() noexcept; //since C++ 11
const_iterator begin() const noexcept; //since C++ 11
參數
空
返回值
它返回一個指向多重集第一個元素的迭代器。
複雜度
恒定。
迭代器有效性
沒有變化。
數據競爭
容器被訪問。常量和非常量版本都不會修改容器。
異常安全
這個函數從不拋出異常。
例子1
讓我們看看 begin() 函數的簡單示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<string> mymultiset= {"Java", "C++", "SQL","C++"};
// show content:
cout<<"Contents of mymultiset are:"<<endl;
for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
cout << *it<< '\n';
return 0;
}
輸出:
Contents of mymultiset are: C++ C++ Java SQL
在上麵的例子中, begin() 函數用於返回一個指向 mymultiset 多集中第一個元素的迭代器。
例子2
讓我們看一個簡單的例子:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> c;
c.insert(5);
c.insert(2);
c.insert(4);
c.insert(2);
c.insert(0);
c.insert(5);
multiset<int>::iterator i = c.begin();
while (i != c.end())
cout << *i++ << " ";
cout << endl;
}
輸出:
0 2 2 4 5 5
例子3
讓我們看一個使用 while 循環遍曆多重集的簡單示例:
#include <iostream>
#include <set>
#include <string>
int main()
{
using namespace std;
multiset<string> mymultiset = { "Nikita","Deep","Priya","Suman","Aman","Nikita" };
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 Nikita Nikita Priya Suman
在上麵的例子中, begin() 函數用於返回一個指向 mymultiset 多集中第一個元素的迭代器。
示例 4
讓我們看一個簡單的例子:
#include <set>
#include <iostream>
int main( )
{
using namespace std;
multiset <int> s1;
multiset <int>::iterator s1_Iter;
s1.insert( 1 );
s1.insert( 2 );
s1.insert( 3 );
s1.insert( 3 );
s1_Iter = s1.begin( );
cout << "The first element of s1 is " << *s1_Iter << endl;
s1_Iter = s1.begin( );
s1.erase( s1_Iter );
s1_Iter = s1.begin( );
cout << "The first element of s1 is now " << *s1_Iter << endl;
}
輸出:
The first element of s1 is 1 The first element of s1 is now 2
在上麵的例子中, begin() 函數用於返回一個指向 mymultiset 多集中第一個元素的迭代器。
相關用法
- C++ multiset begin()、end()用法及代碼示例
- C++ multiset value_comp()用法及代碼示例
- C++ multiset emplace()用法及代碼示例
- 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 size()用法及代碼示例
- C++ multiset get_allocator()用法及代碼示例
- C++ multiset empty()用法及代碼示例
- C++ multiset rend()用法及代碼示例
- C++ multiset upper_bound()用法及代碼示例
- C++ multiset cbegin()、cend()用法及代碼示例
- C++ multiset equal_range()用法及代碼示例
- C++ multiset crend()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ multiset begin()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。