Simple map, filter in c++
This commit is contained in:
parent
62db04986a
commit
51d11391c1
3 changed files with 42 additions and 0 deletions
35
2024/07/cpp_algorithms1/main.cpp
Normal file
35
2024/07/cpp_algorithms1/main.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
|
||||
std::vector<int> numbers = {1, 2, -1, 3, -2, 4};
|
||||
|
||||
numbers.erase(std::remove_if(
|
||||
numbers.begin(),
|
||||
numbers.end(),
|
||||
[](int x) { return x < 0; }),
|
||||
numbers.end());
|
||||
|
||||
auto sum = 0;
|
||||
for (auto it = numbers.begin(); it != numbers.end(); it++) {
|
||||
sum += *it;
|
||||
}
|
||||
|
||||
std::cout << "Sum is: " << sum << "." << std::endl;
|
||||
|
||||
sum = 0;
|
||||
|
||||
auto res = std::accumulate(
|
||||
numbers.begin(),
|
||||
numbers.end(),
|
||||
1,
|
||||
[](int a, int b) { return a * b; });
|
||||
|
||||
std::cout << "Multiplication is: " << res << "." << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue