📜  在C++ STL中设置:: begin()和设置:: end()

📅  最后修改于: 2021-05-30 18:15:31             🧑  作者: Mango

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

设置::开始()

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

setname.begin()
Parameters :
No parameters are passed.
Returns :
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 7

错误和异常
1.它没有异常抛出保证。
2.传递参数时显示错误。

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


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


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


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


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


CPP
// STRING SET EXAMPLE
// CPP program to illustrate
// Implementation of begin() function
#include 
#include 
#include 
using namespace std;
 
int main()
{
    // declaration of set container
    set myset{ "This", "is",
                             "Geeksforgeeks" };
 
    // 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 
#include 
using namespace std;
 
int main()
{
    // declaration of set container
    set 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 
#include 
#include 
using namespace std;
 
int main()
{
    // declaration of set container
    set 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()函数将返回双向迭代器。
句法 :

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

CPP

// INTEGER Example
// CPP program to illustrate
// Implementation of end() function
#include 
#include 
using namespace std;
 
int main()
{
    // declaration of set container
    set 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 
#include 
using namespace std;
  
int main()
{
    // declaration of set container
    set 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 
#include 
#include 
using namespace std;
 
int main()
{
    // declaration of set container
    set 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)

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”