集是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。元素的值一旦添加到集合中就无法修改,尽管可以删除并添加该元素的修改后的值。
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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。