template <class charT, class traits>
void swap(basic_regex<charT, traits>& lhs, basic_regex<charT, traits>& rhs);
概要
2 つの正規表現オブジェクトを交換する。
効果
lhs.swap(rhs)
戻り値
なし
例
#include <iostream>
#include <regex>
void print(const std::regex& re)
{
const char s[] = " abc 123 def ";
std::cmatch m;
std::regex_search(s, m, re);
std::cout << "ready:" << std::boolalpha << m.ready() << std::endl;
if (m.ready()) {
std::cout << "prefix:'" << m.prefix() << '\'' << std::endl;
for (std::size_t i = 0, n = m.size(); i < n; ++i) {
std::cout << i << ":'" << m.str(i) << "', position = " << m.position(i) << std::endl;
}
std::cout << "suffix:'" << m.suffix() << '\'' << std::endl;
}
std::cout << std::endl;
}
int main()
{
std::regex re1("\\w+");
std::regex re2("\\d+");
// swap 前
print(re1);
print(re2);
std::swap(re1, re2);
// swap 後
print(re1);
print(re2);
}
xxxxxxxxxx
#include <iostream>
#include <regex>
void print(const std::regex& re)
{
const char s[] = " abc 123 def ";
std::cmatch m;
std::regex_search(s, m, re);
std::cout << "ready:" << std::boolalpha << m.ready() << std::endl;
if (m.ready()) {
std::cout << "prefix:'" << m.prefix() << '\'' << std::endl;
for (std::size_t i = 0, n = m.size(); i < n; ++i) {
std::cout << i << ":'" << m.str(i) << "', position = " << m.position(i) << std::endl;
}
std::cout << "suffix:'" << m.suffix() << '\'' << std::endl;
}
std::cout << std::endl;
}
int main()
{
std::regex re1("\\w+");
std::regex re2("\\d+");
// swap 前
print(re1);
print(re2);
std::swap(re1, re2);
// swap 後
print(re1);
print(re2);
}
出力
ready:true
prefix:' '
0:'abc', position = 1
suffix:' 123 def '
ready:true
prefix:' abc '
0:'123', position = 5
suffix:' def '
ready:true
prefix:' abc '
0:'123', position = 5
suffix:' def '
ready:true
prefix:' '
0:'abc', position = 1
suffix:' 123 def '
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅, 3.1 ✅, 3.2 ✅, 3.3 ✅, 3.4 ✅, 3.5 ✅, 3.6 ✅
- GCC: 4.9.0 ✅, 4.9.1 ✅, 4.9.2 ✅, 5.0.0 ✅
- ICC: ??
- Visual C++: ??