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


C++ set::begin()、set::end()用法及代碼示例


集是一種關聯容器,其中每個元素都必須是唯一的,因為元素的值可以標識它。元素的值一旦添加到集合中就無法修改,盡管可以刪除並添加該元素的修改後的值。

set::begin()

begin()函數用於返回指向設置容器的第一個元素的迭代器。 begin()函數將雙向迭代器返回到容器的第一個元素。

用法:


setname.begin()
參數:
No parameters are passed.
返回:
This function returns a bidirectional
iterator pointing to the first element.

例子:

Input :myset{1, 2, 3, 4, 5};
         myset.begin();
Output:returns an iterator to the element 1

Input :myset{8, 7};
         myset.begin();
Output:returns an iterator to the element 8

錯誤和異常

1.它沒有異常拋出保證。
2.傳遞參數時顯示錯誤。

// INTEGER SET EXAMPLE 
// CPP program to illustrate 
// Implementation of begin() function 
#include <iostream> 
#include <set> 
using namespace std; 
   
int main() 
{ 
    // declaration of set container 
    set<int> myset{1, 2, 3, 4, 5}; 
      
    // using begin() to print set 
    for (auto it=myset.begin(); it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

1 2 3 4 5
// CHARACTER SET EXAMPLE 
// CPP program to illustrate 
// Implementation of begin() function 
#include <iostream> 
#include <set> 
using namespace std; 
   
int main() 
{ 
    // declaration of set container 
    set<char> myset{'a', 'c', 'g', 'z'}; 
      
    // using begin() to print set 
    for (auto it=myset.begin(); it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

a c g z
// STRING SET EXAMPLE 
// CPP program to illustrate 
// Implementation of begin() function 
#include <iostream> 
#include <set> 
#include <string> 
using namespace std; 
   
int main() 
{ 
    // declaration of set container 
    set<string> myset{"This", "is", "Geeksforgeeks"}; 
      
    // using begin() to print set 
    for (auto it=myset.begin(); it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

Geeksforgeeks This is 

時間複雜度:O(1)

set::end()

它返回一個迭代器,該迭代器指向set容器的最後一個元素。由於它不引用有效元素,因此它不能de-referenced end()函數返回雙向迭代器。

用法:

setname.end()
參數:
No parameters are passed.
返回:
This function returns a bidirectional
iterator pointing to next of the last element.

例子:


Input :myset{1, 2, 3, 4, 5};
         myset.end();
Output:returns an iterator to next of 5

錯誤和異常

1.它沒有異常拋出保證。
2.傳遞參數時顯示錯誤。

// INTEGER Example 
// CPP program to illustrate 
// Implementation of end() function 
#include <iostream> 
#include <set> 
using namespace std; 
   
int main() 
{ 
    // declaration of set container 
    set<int> myset{1, 2, 3, 4, 5}; 
      
    // using end() to print set 
    for (auto it=myset.begin(); it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

1 2 3 4 5
// CHARACTER SET EXAMPLE 
// CPP program to illustrate 
// Implementation of begin() function 
#include <iostream> 
#include <set> 
using namespace std; 
   
int main() 
{ 
    // declaration of set container 
    set<char> myset{'a', 'c', 'g', 'z'}; 
      
    // using begin() to print set 
    for (auto it=myset.begin(); it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

a c g z
// STRING SET EXAMPLE 
// CPP program to illustrate 
// Implementation of begin() function 
#include <iostream> 
#include <set> 
#include <string> 
using namespace std; 
   
int main() 
{ 
    // declaration of set container 
    set<string> myset{"This", "is", "Geeksforgeeks"}; 
      
    // using begin() to print set 
    for (auto it=myset.begin(); it != myset.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
}

輸出:

Geeksforgeeks This is 

時間複雜度:O(1)



相關用法


注:本文由純淨天空篩選整理自AyushSaxena大神的英文原創作品 set::begin() and set::end() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。