Skip to content

Instantly share code, notes, and snippets.

@nicolekorch
Created December 18, 2025 19:21
Show Gist options
  • Select an option

  • Save nicolekorch/6e4ed5f402be7dfb2db91be883ef4580 to your computer and use it in GitHub Desktop.

Select an option

Save nicolekorch/6e4ed5f402be7dfb2db91be883ef4580 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <unordered_map>
int main() {
std::vector<std::vector<int>> matrix = {{1,2,3},{4,5,6},{7,8,9}};
int evenCount2D = std::accumulate(matrix.begin(), matrix.end(), 0,
[](int acc, const std::vector<int>& row) {
return acc + std::count_if(row.begin(), row.end(),
[](int x){ return x % 2 == 0; });
});
std::cout << evenCount2D << std::endl;
std::vector<int> v = {1,2,3,4,5,6};
std::vector<int> evens;
std::copy_if(v.begin(), v.end(), std::back_inserter(evens),
[](int x){ return x % 2 == 0; });
for(int x : evens) std::cout << x << " ";
std::cout << std::endl;
v.erase(std::remove_if(v.begin(), v.end(),
[](int x){ return x < 0; }), v.end());
for(int x : v) std::cout << x << " ";
std::cout << std::endl;
std::vector<std::string> words = {"meet","sea","son","night"};
char c = 's';
int countByChar = std::count_if(words.begin(), words.end(),
[c](const std::string& s){
return !s.empty() && s[0] == c;
});
std::cout << countByChar << std::endl;
std::unordered_map<char,int> result;
std::for_each(words.begin(), words.end(),
[&result](const std::string& s){
if(!s.empty()) result[s[0]]++;
});
for(const auto& [k,v] : result)
std::cout << k << " - " << v << std::endl;
}
@nicolekorch
Copy link
Author

с++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment