Begin用法

在前面章节中,我们已经对 C++ STL标准库提供的所有容器做了系统的讲解。读者可能已经注意到,无论是序列式容器还是关联式容器(包括哈希容器),不仅模板类内部提供有 begin() 和 end() 成员方法,C++ STL 标准库中还提供有同名且具有相同功能的 begin() 和 end() 函数。

首先需要说明的是,begin() 和 end() 是以函数模板的形式定义的,但它们的模板并没有位于某一个头文件中,而是很多头文件中都有它们的定义。

C++ STL 标准库中,包含 begin() 和 end() 函数模板的头文件包括:<iterator>, <array>, <deque>, <forward_list>, <list>, <map>, <regex>(正则表达式的头文件), <set>, <string>, <unordered_map>, <unordered_set> 以及 <vector>。

不仅如此,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 类型的迭代器
template <class Container>
    auto begin (Container& cont)
//② 传入 const 修饰的容器,begin() 函数返回的为 const 类型的迭代器
template <class Container>
    auto begin (const Container& cont)

其中,cont 表示指定的容器;同时,函数会返回一个有特定指向的迭代器,且此迭代器的类型也取决于 cont 容器。

以上 2 种格式的区别仅在与传入的容器是否有 const 修饰,即如果有,则通过该函数获得的迭代器也有 const 修饰(不能用于修改容器中存储的数据);反之就没有。

举个例子:

#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* begin (T(&arr)[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的音标和读音:
DJ音标发音: [ˈbeigin]
KK音标发音: [ˈbegɪn]

begin的词性:
NONE(无词性)

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.
贝京,梅纳切姆:(1913-1992) 俄裔以色列籍政治家。1943年至1948年,他在巴勒斯坦领导了伊尔干地下犹太复国主义运动。以色列建国后,作为政治领袖贝京以其对阿拉伯国家主张强硬路线的观点著名。但作为总理(1977-1983年),他努力解决阿以冲突问题。1978年他与埃及的安瓦·艾-萨达特共获诺贝尔和平奖

be.gin

begin的音标和读音:
DJ音标发音: [biˈgin]
KK音标发音: [bɪˈgɪn]

begin的词性:
v.(动词)

began[-g2n“] begun[-g?n“] beginningbegins

begin的词性:
v.intr.(不及物动词)

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的词性:
v.tr.(及物动词)

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
中古英语 biginnen

8. from Old English beginnan
源自 古英语 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
Begin

11. commence are equivalent in meaning, though commence is more formal:
commence 意思相同,但commence 更为正式:

用法与例句:

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:
Start 经常可以同begin 和 commence 互换, 但也可以暗指从特定一点出发,通常接在静止动作后:

用法与例句:

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:
Initiate 指在一过程中采取的第一步行动,而不考虑之后如何:

用法与例句:

The public hoped the government would initiate restrictions on imported goods.
公众希望政府能对进口货物采取限制。

14. Inaugurate often connotes a formal beginning:
Inaugurate 经常指正式的开始:

用法与例句:

“The exhibition inaugurated a new era of cultural relations between the Soviet Union and United States” (Serge Schmemann).
“这次展览开创了美苏两国文化关系的新纪元。” (塞格·施密曼)

Toplist

最新的帖子

標籤