當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ multimap end()用法及代碼示例

C++ 多映射 end() 函數用於返回多映射中最後一個條目旁邊的迭代器。

注意:-這是一個占位符。此位置不存在元素,嘗試訪問是未定義的行為。

用法

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

參數

返回值

它返回一個迭代器,它指向 multimap 的最後一個元素旁邊。

複雜度

恒定。

迭代器有效性

沒有變化。

數據競爭

訪問容器的常量和非常量版本都不會修改容器。

異常安全

此成員函數從不拋出異常。

例子1

讓我們看看 end() 函數的簡單示例:

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,string> mymultimap;
  mymultimap = { 
               {'a',"Java"},
               {'b', "C++"},
               {'b', "Python"},
               {'a', "Android"}
               };
  // show content:
  for (multimap<char,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  return 0;
}

輸出:

a => Java
a => Android
b => C++
b => Python

在上麵的示例中,end() 函數用於返回指向 mymultimap 多映射中最後一個元素旁邊的迭代器。

例子2

讓我們看一個使用 for-each 循環遍曆多重映射的簡單示例:

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
	  multimap<string, int> m;
  m= { 
     {"Room1", 100},
     {"Room2", 200},
     {"Room1", 300},
     {"Room1", 100} 
     };
	// Create a multimap iterator and point to beginning of multimap
	multimap<string, int>::iterator it = m.begin();
	// Iterate over a multimap using std::for_each and Lambda function	
		for_each(m.begin(), m.end(),
				[](pair<string, int> element){				   
					// Accessing KEY from element
					string word = element.first;
					// Accessing VALUE from element.
					int count = element.second;
					cout<<word<<" = "<<count<<endl;
		});
	return 0;
}

輸出:

Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200

在上麵的例子中,我們使用 STL 算法 std::for-each 來迭代多重映射。它將迭代每個 multimap 元素並調用我們提供的回調。

例子3

讓我們看一個使用 while 循環迭代 multimap 的簡單示例:

#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std; 
      multimap<int,string> mymultimap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 200, "Priya" },
                { 400, "Suman" },
                { 300, "Aman"  }}; 
    multimap<int, string>::const_iterator it; // declare an iterator
    it = mymultimap.begin(); // assign it to the start of the vector
    while (it != mymultimap.end()) // while it hasn't reach the end
    {
cout << it->first << " = " << it->second << "\n"; 
// print the value of the element it points to
++it; // and iterate to the next element
    }
    cout << endl;
    return 0;
}

輸出:

100 = Nikita
200 = Deep
200 = Priya
300 = Aman
400 = Suman

在上麵的示例中,end() 函數用於返回指向 mymultimap 多映射中最後一個元素旁邊的迭代器。

示例 4

讓我們看一個簡單的例子:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> mymultimap = {
                { 10, 10},
                { 20, 20 },
                { 10, 30 },
                { 20, 10} 
                };                         
  cout<<"Elements are:" <<endl;
    for (multimap<int,int>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first 
    << " * " 
    << it->second 
    << " = "
    <<it->first * it->second
    << '\n';
  return 0;
  }

輸出:

Elements are:
10 * 10 = 100
10 * 30 = 300
20 * 20 = 400
20 * 10 = 200

在上麵的示例中,end() 函數用於返回指向 mymultimap 多映射中最後一個元素旁邊的迭代器。





相關用法


注:本文由純淨天空篩選整理自 C++ multimap end() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。