std::sub_match<BidirIt>::swap
來自 cppreference.com
void swap( sub_match& s ) noexcept(/* 參見下方 */); |
(C++11 起) | |
交換兩個子匹配物件的內容。等價於
this->pair<BidirIt, BidirIt>::swap(s);
std::swap(matched, s.matched);
目錄 |
[編輯] 引數
s | - | 要交換的 sub_match |
型別要求 | ||
-BidirIt 必須滿足 LegacySwappable 的要求。 |
[編輯] 異常
noexcept 規範:
noexcept(std::is_nothrow_swappable_v<BidirIt>)
[編輯] 示例
執行此程式碼
#include <cassert> #include <iostream> #include <regex> int main() { const char* s = "Quick red cat"; std::sub_match<const char*> x, y; x.first = &s[0]; x.second = &s[5]; x.matched = false; y.first = &s[012]; y.second = &s[13]; y.matched = true; std::cout << "Before swap:\n"; std::cout << "x.str() = [" << x.str() << "]\n"; std::cout << "y.str() = [" << y.str() << "]\n"; assert(!x.matched and y.matched); x.swap(y); std::cout << "After swap:\n"; std::cout << "x.str() = [" << x.str() << "]\n"; std::cout << "y.str() = [" << y.str() << "]\n"; assert(x.matched and !y.matched); }
輸出
Before swap: x.str() = [] y.str() = [cat] After swap: x.str() = [cat] y.str() = []
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3204 | C++11 | std::sub_match 使用繼承的 std::pair::swap(pair&) 導致切片 |
添加了 std::sub_match::swap(sub_match&) |