集:C++中的起始()和SET::Enter()

设置 是一种关联容器,其中每个元素必须是唯一的,因为元素的值标识它。元素的值一旦添加到集合中就不能修改,尽管可以删除并添加该元素的修改值。

null
set::begin()

函数用于返回指向集合容器第一个元素的迭代器。begin()函数 返回一个双向迭代器 到容器的第一个元素。 语法:

setname.begin()Parameters :No parameters are passed.Returns :This function returns a bidirectionaliterator pointing to the first element.

例如:

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

错误和异常 1.它有一个无例外的抛出保证。 2.传递参数时显示错误。

CPP

// 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

CPP

// 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 

CPP

// 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()

它返回一个迭代器,指向集合容器的最后一个元素。由于它没有引用有效的元素,因此无法取消引用end()函数返回双向迭代器。 语法:

setname.end()Parameters :No parameters are passed.Returns :This function returns a bidirectionaliterator 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.传递参数时显示错误。

CPP

// 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

CPP

// 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

CPP

// 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)

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享