名前空間
変種
操作

std::vector<T,Allocator>::begin, std::vector<T,Allocator>::cbegin

提供: cppreference.com
< cpp‎ | container‎ | vector
 
 
 
 
iterator begin();
(C++11未満)
iterator begin() noexcept;
(C++11以上)
const_iterator begin() const;
(C++11未満)
const_iterator begin() const noexcept;
(C++11以上)
const_iterator cbegin() const noexcept;
(C++11以上)

コンテナの最初の要素を指すイテレータを返します。

コンテナが空の場合は、返されたイテレータは end() と等しくなります。

range-begin-end.svg

目次

[編集] 引数

(なし)

[編集] 戻り値

最初の要素を指すイテレータ。

[編集] 計算量

一定。


[編集]

#include <iostream>
#include <vector>
#include <string>
 
int main()
{
	std::vector<int> ints {1, 2, 4, 8, 16};
	std::vector<std::string> fruits {"orange", "apple", "raspberry"};
	std::vector<char> empty;
 
	// Sums all integers in the vector ints (if any), printing only the result.
	int sum = 0;
	for (auto it = ints.cbegin(); it != ints.cend(); it++)
		sum += *it;
	std::cout << "Sum of ints: " << sum << "\n";
 
	// Prints the first fruit in the vector fruits, without checking if there is one.
	std::cout << "First fruit: " << *fruits.begin() << "\n";
 
	if (empty.begin() == empty.end())
		std::cout << "vector 'empty' is indeed empty.\n";
}

出力:

Sum of ints: 31
First fruit: orange
vector 'empty' is indeed empty.

[編集] 関連項目

終��を指すイテレータを返します
(パブリックメンバ関数) [edit]