Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created December 17, 2025 20:24
Show Gist options
  • Select an option

  • Save vlaleli/cb84a991c3e57f1661b194a51ea870b4 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/cb84a991c3e57f1661b194a51ea870b4 to your computer and use it in GitHub Desktop.
#include "LambdaTasks.h"
#include <algorithm>
#include <iostream>
int countEvenInList(const std::list<int>& lst) {
auto lambda = [](int x) {
return x % 2 == 0;
};
return std::count_if(lst.begin(), lst.end(), lambda);
}
void printEvenVector(const std::vector<int>& v) {
std::for_each(v.begin(), v.end(), [](int x) {
if (x % 2 == 0)
std::cout << x << " ";
});
std::cout << std::endl;
}
void removeNegative(std::vector<int>& v) {
v.erase(
std::remove_if(v.begin(), v.end(), [](int x) {
return x < 0;
}),
v.end()
);
}
int countStringsByLetter(const std::vector<std::string>& v, char letter) {
return std::count_if(v.begin(), v.end(), [letter](const std::string& s) {
return !s.empty() && s[0] == letter;
});
}
std::map<char, int> countStringsByAllLetters(const std::vector<std::string>& v) {
std::map<char, int> result;
std::for_each(v.begin(), v.end(), [&result](const std::string& s) {
if (!s.empty())
result[s[0]]++;
});
return result;
}
#ifndef LAMBDATASKS_H
#define LAMBDATASKS_H
#include <list>
#include <vector>
#include <string>
#include <map>
int countEvenInList(const std::list<int>& lst);
void printEvenVector(const std::vector<int>& v);
void removeNegative(std::vector<int>& v);
int countStringsByLetter(const std::vector<std::string>& v, char letter);
std::map<char, int> countStringsByAllLetters(const std::vector<std::string>& v);
#endif
#include <iostream>
#include <list>
#include <vector>
#include <string>
#include "LambdaTasks.h"
int main() {
std::list<int> lst = {1, 2, 4, 7, 9, 10};
std::cout << "Кількість парних елементів двозв’язного списку: "
<< countEvenInList(lst) << std::endl;
std::vector<int> vec = {3, 6, 8, 11, 14};
std::cout << "Парні елементи вектора: ";
printEvenVector(vec);
std::vector<int> nums = {5, -2, 7, -9, 0, 4};
removeNegative(nums);
std::cout << "Вектор без від’ємних елементів: ";
for (int x : nums)
std::cout << x << " ";
std::cout << std::endl;
std::vector<std::string> words = {"meet", "sea", "son", "night"};
std::cout << "Кількість рядків, що починаються з літери 's': "
<< countStringsByLetter(words, 's') << std::endl;
std::cout << "Кількість рядків за першою літерою:" << std::endl;
auto result = countStringsByAllLetters(words);
for (const auto& p : result)
std::cout << p.first << " - " << p.second << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment