◐ Shell
clean mode source ↗

std::vector<T,Allocator>::begin, std::vector<T,Allocator>::cbegin - cppreference.com

提供: cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>

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() と等しくなります。

引数

(なし)

戻り値

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

計算量

一定。

#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.

関連項目