在前面章节中,我们已经对 C++ STL标准库提供的所有容器做了系统的讲解。读者可能已经注意到,无论是序列式容器还是关联式容器(包括哈希容器),不仅模板类内部提供有 begin() 和 end() 成员方法,C++ STL 标准库中还提供有同名且具有相同功能的 begin() 和 end() 函数。 首先需要说明的是,begin() 和 end() 是以函数模板的形式定义的,但它们的模板并没有位于某一个头文件中,而是很多头文件中都有它们的定义。
不仅如此,begin() 和 end() 都位于 std 命名空间中。因此,在使用这 2 个函数之前,程序中应引入容纳它们函数模板的头文件以及 std 命名空间。 在实际的使用场景中,begin() 和 end() 函数往往会一起使用的。根据作用对象的不同,begin() 和 end() 函数可细分为以下 2 个功能。 1) begin()和end()参数为容器当将某个具体容器(比如 cont)作为参数分别传给 begin() 和 end() 函数时,其中 begin() 底层会执行 cont.begin() 语句,而 end() 底层会执行 cont.end() 语句,它们最终会将得到的迭代器作为函数的返回值反馈回来。 当作用对象为容器时,end() 和 begin() 函数的语法格式是完全一样的,这里以 begin() 函数为例,有以下 2 种格式: //① 非 const 修改的容器作为参数,begin() 函数返回的为非
const 类型的迭代器 其中,cont 表示指定的容器;同时,函数会返回一个有特定指向的迭代器,且此迭代器的类型也取决于 cont 容器。
举个例子: #include <iostream> // std::cout #include <vector> // std::vector, std::begin, std::end using namespace std; int main() { //创建并初始化 vector 容器 std::vector<int> myvector{ 1,2,3,4,5 }; //调用 begin() 和 end() 函数遍历 myvector 容器 for (auto it = begin(myvector); it != end(myvector); ++it) cout << *it << ' '; return 0; } 程序执行结果为: 1 2 3 4 5 程序第 8 行中,begin(myvector) 等同于执行 myvector.begin(),而 end(myvector) 也等同于执行 myvector.end()。 2) begin()和end()参数为数组除了可以将指定容器作为参数传给 begin() 和 end() 之外,还可以指定数组作为参数传给它们。 将指定数组传给 begin() 函数,其会返回一个指向该数组首个元素的指针;将指定数组传给 end() 函数,其会返回一个指向数组中最后一个元素之后位置的指针。 同样,数组作为参数时,end() 函数的语法格式和 begin() 函数也完全一样,这里仅给出了 begin() 函数的语法格式: template <class T, size_t N> 其中 T 为数组中存储元素的类型,N 为数组的长度;(&arr)[N] 表示以引用的方式传递数组作为参数。 举个例子: #include <iostream> // std::cout #include <vector> // std::vector, std::begin, std::end using namespace std; int main() { //定义一个普通数组 int arr[] = { 1,2,3,4,5 }; //创建一个空 vector 容器 vector<int> myvector; //将数组中的元素添加到 myvector 容器中存储 for (int *it = begin(arr); it != end(arr); ++it) myvector.push_back(*it); //输出 myvector 容器中存储的元素 for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << *it << ' '; return 0; } 程序执行结果为: 1 2 3 4 5 注意程序中第 10 行,这里用整数指针 it 接收 begin(arr) 的返回值,同时该循环会一直循环到 it 指向 arr 数组中最后一个元素之后的位置。 Be.gin begin的音标和读音: begin的词性: 1. Russian-born Israeli politician. He led (1943-1948) Irgun, the Zionist underground movement in Palestine. After the establishment of Israel, Begin became a political leader known for his hard-line views on the Arabs, but as prime minister (1977-1983) he strove to resolve the Arab-Israel conflict. He shared the 1978 Nobel Peace Prize with Anwar el-Sadat of
Egypt. be.gin begin的音标和读音: begin的词性: began[-g2n“] begun[-g?n“] beginning, begins begin的词性: 1. To take the first step in performing an action;
start. 2. To come into being: 用法与例句: when life began. 3. To do or accomplish in the least degree: 用法与例句: Those measures do not even begin to address the problem. begin的词性: 4. To take the first step in doing; start: 用法与例句: began
work. 5. To cause to come into being; originate. 6. To come first in: 用法与例句: The numeral 1 begins the sequence. begin的词源: 7. Middle English biginnen 8. from Old English beginnan 【近义词】 begin, commence, start, initiate, inaugurate 9. These verbs are compared as they denote coming or putting into operation, being, or motion or setting about taking the first step, as in a
procedure. 10. Begin and 11. commence are equivalent in meaning, though commence is more formal: 用法与例句: began the race; 用法与例句: a play that begins at eight o'clock; 用法与例句: commenced her career as a scientist; 用法与例句: festivities that commenced with the national anthem. 12. Start is often interchangeable with begin and commence but can also imply setting out from a specific point, frequently following inaction: 用法与例句: Stand and visit with me for a few minutes until the train starts. 用法与例句: The telephone started
ringing. 13. Initiate applies to the act of taking the first steps in a process, without reference to what follows: 用法与例句: The public hoped the government would initiate restrictions on imported goods. 14. Inaugurate often connotes a formal beginning: 用法与例句: “The exhibition inaugurated a new era of cultural
relations between the Soviet Union and United States” (Serge Schmemann). |