C++03規格時点で存在したコンテナライブラリに対するC++11規格での変更点をまとめています。
概要 †
当文書では、一般にコンテナと呼ばれる機能を提供するヘッダファイル群と、コンテナとほぼ同等の特徴を持つ <string> について、C++11規格での変更点をまとめている。
C++11規格で新規追加されたヘッダファイルについては言及しない。
コンテナクラスは一部を除いてある程度共通のインタフェースを備えており、例えばメンバ関数 begin
によって先頭要素を指すイテレータを取得できる。
その関係上C++11規格での変更にも共通点が多いため、それらは共通の変更点としてまとめている。
コンテナライブラリ自体の変更ではないが、コンテナの仕組みを用いるC++11規格の言語機能として範囲for文(Range-based for)がある。
あるコンテナクラス型(あるいは固定長配列型)のインスタンス c
に対し、下記のようなコードで全要素を走査することができる。
| for (const auto& value : c)
{
}
|
共通の変更点 †
<queue> と <stack> を除くコンテナライブラリ(<string> 含む)に共通する変更点は次の通り。
ここに記載した内容は個々のヘッダファイルの項には記載しない。
各クラスにムーブコンストラクタおよびムーブ代入演算子が定義された。
各クラスに std::initializer_list<value_type>
を受け取るコンストラクタおよび代入演算子が定義された。
これにより、 std::vector<int> v = { 1, 2, 3 };
といった記述によるインスタンスの初期化などが可能になった。
各クラスに、第2引数で const allocator_type&
を受け取るコピーコンストラクタおよびムーブコンストラクタが定義された。
各クラスに下記のメンバ関数が定義された。
| const_iterator cbegin() const noexcept; const_iterator cend() const noexcept; const_reverse_iterator crbegin() const noexcept; const_reverse_iterator crend() const noexcept;
|
各クラスの下記のメンバ関数に条件式なしの noexcept
指定が付与され、例外を投げないことが保証された。
begin
end
rbegin
rend
empty
size
max_size
clear
get_allocator
各クラスのメンバ型 reference
および const_reference
の定義が下記のように変更され、アロケータ型に依存しなくなった。
| typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
|
| typedef value_type& reference;
typedef const value_type& const_reference;
|
フリー関数 begin
および end
が定義された。
コンテナクラス型のインスタンス c
において、 std::begin(c), std::end(c);
はそれぞれ c.begin(), c.end();
と等しい。
多くの処理系では <iterator> そのものか、あるいはそれに相当するヘッダファイルをインクルードしていると思われる。
<deque> †
deque クラステンプレート †
<list> †
list クラステンプレート †
下記のメンバ関数が定義された。
| template<class ...Args>
iterator emplace(const_iterator pos, Args&&... args);
|
| template<class ...Args>
void emplace_front(Args&&... args);
|
| template<class ...Args>
void emplace_back(Args&&... args);
|
下記のメンバ関数オーバロードが定義された。
| void assign(initializer_list<value_type> values);
void push_front(value_type&& value);
void push_back(value_type&& value);
iterator insert(const_iterator pos, value_type&& value);
iterator insert(const_iterator pos, initializer_list<value_type> values);
void resize(size_type num);
void splice(const_iterator pos, list&& src);
void splice(const_iterator pos, list&& src, const_iterator spos);
void splice(const_iterator pos, list&& src, const_iterator sfirst, const_iterator slast);
void merge(list&& src);
template<class Compare> void merge(list&& src, Compare comp);
|
メンバ関数 erase
および splice
の定義が変更され、引数の iterator
がすべて const_iterator
になった。
メンバ関数 resize
の定義が変更され、デフォルト引数が削除された。
代わりに第2引数なしのオーバロードが定義された。
メンバ関数 reverse
に条件式なしの noexcept
指定が付与され、例外を投げないことが保証された。
<map> †
map クラステンプレート †
multimap クラステンプレート †
<queue> †
コンテナ共通の変更点は該当しない。
queue クラステンプレート †
priority_queue クラステンプレート †
下記のメンバ関数が定義された。
| template<class ...Args>
void emplace(Args&&... args);
|
| void swap(priority_queue& other) noexcept(noexcept(std::swap(c, other.c)));
|
下記のメンバ関数オーバロードが定義された。
| void push(value_type&& value);
|
下記のメンバ型が定義された。
| typedef container_type::reference reference; typedef container_type::const_reference const_reference;
|
コンストラクタの定義が変更された。
| explicit priority_queue(
const Compare& comp = Compare(),
const container_type& src = container_type());
template<class InputIterator> priority_queue(
InputIterator first,
InputIterator last,
const Compare& comp = Compare(),
const container_type& src = container_type());
|
| priority_queue(const Compare& comp, const container_type& src);
template<class InputIterator> priority_queue(
InputIterator first,
InputIterator last,
const Compare& comp,
const container_type& src);
explicit priority_queue(
const Compare& comp = Compare(),
container_type&& src = container_type());
template<class InputIterator> priority_queue(
InputIterator first,
InputIterator last,
const Compare& comp = Compare(),
container_type&& src = container_type());
template<class Alloc> explicit priority_queue(const Alloc& alloc);
template<class Alloc> priority_queue(const Compare& comp, const Alloc& alloc);
template<class Alloc> priority_queue(
const Compare& comp,
const container_type& src,
const Alloc& alloc);
template<class Alloc> priority_queue(
const Compare& comp,
container_type&& src,
const Alloc& alloc);
template<class Alloc> priority_queue(const priority_queue& src, const Alloc& alloc);
template<class Alloc> priority_queue(priority_queue&& src, const Alloc& alloc);
|
メンバ関数 top
の定義が変更された。
| const value_type& top() const;
|
| const_reference top() const;
|
<queue> その他 †
<set> †
set クラステンプレート †
multiset クラステンプレート †
<stack> †
コンテナ共通の変更点は該当しない。
stack クラステンプレート †
下記のメンバ関数が定義された。
| template<class ...Args>
void emplace(Args&&... args);
|
| void swap(stack& other) noexcept(noexcept(std::swap(c, other.c)));
|
下記のメンバ関数オーバロードが定義された。
| void push(value_type&& value);
|
下記のメンバ型が定義された。
| typedef container_type::reference reference; typedef container_type::const_reference const_reference;
|
コンストラクタの定義が変更された。
| explicit stack(const container_type& src = container_type());
|
| explicit stack(const container_type& src);
explicit stack(container_type&& src = container_type());
template<class Alloc> explicit stack(const Alloc& alloc);
template<class Alloc> stack(const container_type& src, const Alloc& alloc);
template<class Alloc> stack(container_type&& src, const Alloc& alloc);
template<class Alloc> stack(const stack& src, const Alloc& alloc);
template<class Alloc> stack(stack&& src, const Alloc& alloc);
|
メンバ関数 top
の定義が変更された。
| value_type& top();
const value_type& top() const;
|
| reference top();
const_reference top() const;
|
<stack> その他 †
<string> †
basic_string クラステンプレート †
下記のメンバ関数が定義された。
| value_type& front();
const value_type& front() const;
|
| value_type& back();
const value_type& back() const;
|
下記のメンバ関数オーバロードが定義された。
| basic_string& append(initializer_list<value_type> values);
basic_string& assign(basic_string&& src);
basic_string& assign(initializer_list<value_type> values);
basic_string& insert(const_iterator pos, initializer_list<value_type> values);
basic_string& replace(
const_iterator first,
const_iterator last,
initializer_list<value_type> values);
basic_string& operator+=(initializer_list<value_type> values);
|
下記のメンバ型の定義が変更された。
| typedef allocator_type::pointer pointer;
typedef allocator_type::const_pointer const_pointer;
typedef allocator_type::difference_type difference_type;
typedef allocator_type::size_type size_type;
|
| typedef allocator_traits<allocator_type>::pointer pointer;
typedef allocator_traits<allocator_type>::const_pointer const_pointer;
typedef allocator_traits<allocator_type>::difference_type difference_type;
typedef allocator_traits<allocator_type>::size_type size_type;
|
メンバ関数 insert
の定義が一部変更された。
| void insert(iterator pos, size_type num, value_type value);
iterator insert(iterator pos, value_type value);
template<class InputIterator> void insert(
iterator pos,
InputIterator first,
InputIterator last);
|
| iterator insert(const_iterator pos, size_type num, value_type value);
iterator insert(const_iterator pos, value_type value);
template<class InputIterator> iterator insert(
const_iterator pos,
InputIterator first,
InputIterator last);
|
メンバ関数 erase
および replace
の定義が一部変更され、引数の iterator
がすべて const_iterator
になった。
下記のメンバ関数に条件式なしの noexcept 指定が付与され、例外を投げないことが保証された。
c_str
data
capacity
length
compare
(const basic_string&
を第1引数にとるオーバロードのみ)
find
(const value_type*
を引数にとるオーバロードを除く)
rfind
(const value_type*
を引数にとるオーバロードを除く)
find_first_of
(const value_type*
を引数にとるオーバロードを除く)
find_last_of
(const value_type*
を引数にとるオーバロードを除く)
find_first_not_of
(const value_type*
を引数にとるオーバロードを除く)
find_last_not_of
(const value_type*
を引数にとるオーバロードを除く)
char_traits クラステンプレート †
<string> その他 †
下記の型が定義された。
| typedef basic_string<char16_t> u16string;
typedef basic_string<char32_t> u32string;
|
下記のフリー関数が定義された。
| int stoi(const string& src, size_t* index = 0, int base = 10);
int stoi(const wstring& src, size_t* index = 0, int base = 10);
long stol(const string& src, size_t* index = 0, int base = 10);
long stol(const wstring& src, size_t* index = 0, int base = 10);
long long stoll(const string& src, size_t* index = 0, int base = 10);
long long stoll(const wstring& src, size_t* index = 0, int base = 10);
unsigned long stoul(const string& src, size_t* index = 0, int base = 10);
unsigned long stoul(const wstring& src, size_t* index = 0, int base = 10);
unsigned long long stoull(const string& src, size_t* index = 0, int base = 10);
unsigned long long stoull(const wstring& src, size_t* index = 0, int base = 10);
|
| float stof(const string& src, size_t* index = 0);
float stof(const wstring& src, size_t* index = 0);
double stod(const string& src, size_t* index = 0);
double stod(const wstring& src, size_t* index = 0);
long double stold(const string& src, size_t* index = 0);
long double stold(const wstring& src, size_t* index = 0);
|
| string to_string(int value);
string to_string(long value);
string to_string(long long value);
string to_string(unsigned int value);
string to_string(unsigned long value);
string to_string(unsigned long long value);
string to_string(float value);
string to_string(double value);
string to_string(long double value);
wstring to_wstring(int value);
wstring to_wstring(long value);
wstring to_wstring(long long value);
wstring to_wstring(unsigned int value);
wstring to_wstring(unsigned long value);
wstring to_wstring(unsigned long long value);
wstring to_wstring(float value);
wstring to_wstring(double value);
wstring to_wstring(long double value);
|
フリー関数 getline
の basic_istream&&
を引数にとるオーバロードが定義された。
basic_istream&
を引数にとるオーバロードに対応する形で定義されている。
フリー関数 operator+
の basic_string&&
を引数にとるオーバロードが定義された。
const basic_string&
を引数にとるオーバロードに対応する形で定義されている。
クラステンプレート hash
(<functional>)の特殊化バージョンが定義された。
| template<> struct hash<string>;
template<> struct hash<wstring>;
template<> struct hash<u16string>;
template<> struct hash<u32string>;
|
下記のフリー関数に条件式なしの noexcept 指定が付与され、例外を投げないことが保証された。
operator==
(const basic_string&
を第1および第2引数にとるオーバロードのみ)
operator!=
(const basic_string&
を第1および第2引数にとるオーバロードのみ)
operator<
(const basic_string&
を第1および第2引数にとるオーバロードのみ)
operator<=
(const basic_string&
を第1および第2引数にとるオーバロードのみ)
operator>
(const basic_string&
を第1および第2引数にとるオーバロードのみ)
operator>=
(const basic_string&
を第1および第2引数にとるオーバロードのみ)
<vector> †
vector クラステンプレート †
下記のメンバ関数が定義された。
| value_type* data() noexcept;
const value_type* data() const noexcept;
|
| template<class ...Args>
iterator emplace(const_iterator pos, Args&&... args);
|
| template<class ...Args>
void emplace_back(Args&&... args);
|
下記のメンバ関数オーバロードが定義された。
| void assign(initializer_list<value_type> values);
void push_back(value_type&& value);
iterator insert(const_iterator pos, value_type&& value);
iterator insert(const_iterator pos, initializer_list<value_type> values);
void resize(size_type num);
|
メンバ関数 erase
の定義が変更され、引数の iterator
がすべて const_iterator
になった。
メンバ関数 resize
の定義が変更され、デフォルト引数が削除された。
代わりに第2引数なしのオーバロードが定義された。
下記のメンバ関数に条件式なしの noexcept 指定が付与され、例外を投げないことが保証された。
capacity
vector<bool>::flip
vector<bool>::swap
(静的メンバ関数版のみ)
<vector> その他 †