С++ Собеседования
1.07K subscribers
30 photos
2 links
по всем вопросам @haarrp
Download Telegram
@cppsobes - Разбираем задачи с собеседований C++
Regular expressions library (since C++11)

Библиотека регулярных выражений предоставляет класс, представляющий регулярные выражения, которые являются своего рода мини-языком, используемым для выполнения сопоставления шаблонов в строках.

#include <iostream>
#include <iterator>
#include <regex>
#include <string>

int main()
{
std::string s = "Some people, when confronted with a problem, think "
"\"I know, I'll use regular expressions.\" "
"Now they have two problems.";

std::regex self_regex("REGULAR EXPRESSIONS",
std::regex_constants::ECMAScript | std::regex_constants::icase);
if (std::regex_search(s, self_regex))
std::cout << "Text contains the phrase 'regular expressions'\n";

std::regex word_regex("(\\w+)");
auto words_begin =
std::sregex_iterator(s.begin(), s.end(), word_regex);
auto words_end = std::sregex_iterator();

std::cout << "Found "
<< std::distance(words_begin, words_end)
<< " words\n";

const int N = 6;
std::cout << "Words longer than " << N << " characters:\n";
for (std::sregex_iterator i = words_begin; i != words_end; ++i)
{
std::smatch match = *i;
std::string match_str = match.str();
if (match_str.size() > N)
std::cout << " " << match_str << '\n';
}

std::regex long_word_regex("(\\w{7,})");
std::string new_s = std::regex_replace(s, long_word_regex, "[$&]");
std::cout << new_s << '\n';
}

https://en.cppreference.com/w/cpp/regex

#cpp #programming

@cppsobes
Что выведет код?
Anonymous Quiz
6%
111
49%
222
7%
123
38%
121
Cколько байт памяти займут данные этого типа?
Anonymous Quiz
20%
32
24%
54
23%
30
33%
52
Что выведет эта программа?
Anonymous Quiz
27%
0
21%
1
52%
Неопределённое поведение
Ответ:
Anonymous Quiz
28%
1111
19%
1112
3%
1211
50%
1012
Что выведет программа?
Anonymous Quiz
7%
MCD0
16%
D0C0
17%
Ничего
23%
D0C1
31%
Что это?!;)
6%
M1D2