当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ vector::begin()、vector::end()用法及代码示例


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

vector::begin()

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

用法:


vectorname.begin()
参数:
No parameters are passed.
返回:
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.传递参数时显示错误。

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

输出:

1 2 3 4 5
// STRING VECTOR EXAMPLE 
// CPP program to illustrate 
// Implementation of begin() function 
#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    // declaration of vector container 
    vector<string> 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)

vector::end()

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

用法:

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

// INTEGER VECTOR EXAMPLE 
// CPP program to illustrate 
// Implementation of end() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    // declaration of vector container 
    vector<int> 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
// STRING VECTOR EXAMPLE 
// CPP program to illustrate 
// Implementation of end() function 
#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    // declaration of vector container 
    vector<string> 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)



相关用法


注:本文由纯净天空筛选整理自AyushSaxena大神的英文原创作品 vector::begin() and vector::end() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。