📜  C++ STL中的vector :: begin()和vector :: end()

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

向量与动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,并且容器自动处理其存储。

矢量::: begin()

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

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

例子:

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

Input  : myvector{"This", "is", "Geeksforgeeks"};
         myvector.begin();
Output : returns an iterator to the element This

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

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


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


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


CPP
// STRING VECTOR EXAMPLE
// CPP program to illustrate
// Implementation of end() function
#include 
#include 
#include 
using namespace std;
 
int main()
{
    // declaration of vector container
    vector myvector{ "computer",
                             "science", "portal" };
 
    // using end() to print vector
    for (auto it = myvector.begin();
         it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}


输出:

1 2 3 4 5

CPP

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

输出:

This is Geeksforgeeks

时间复杂度: O(1)

向量::: end()

end()函数用于返回指向向量容器最后一个元素旁边的迭代器。 end()函数返回双向迭代器
句法 :

vectorname.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to next to last element.

例子:

Input  : myvector{1, 2, 3, 4, 5};
         myvector.end();
Output : returns an iterator after 5

Input  : myvector{"computer", "science", "portal"};
         myvector.end();
Output : returns an iterator after portal

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

CPP

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

输出:

1 2 3 4 5

CPP

// STRING VECTOR EXAMPLE
// CPP program to illustrate
// Implementation of end() function
#include 
#include 
#include 
using namespace std;
 
int main()
{
    // declaration of vector container
    vector myvector{ "computer",
                             "science", "portal" };
 
    // using end() to print vector
    for (auto it = myvector.begin();
         it != myvector.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

输出:

computer science portal

时间复杂度: O(1)

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