Created
December 18, 2025 19:21
-
-
Save nicolekorch/6e4ed5f402be7dfb2db91be883ef4580 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
с++