1
0
Fork 0
mirror of https://github.com/dancojocaru2000/y3s2-cciot-hw.git synced 2025-02-22 17:09:35 +02:00

Solved Lab3 Slide6

This commit is contained in:
Kenneth Bruen 2022-03-13 18:09:04 +02:00
parent 7abc7d89dd
commit d96dc8247a
Signed by: kbruen
GPG key ID: CB77B9FE7F902176
3 changed files with 50 additions and 0 deletions

15
Lab3/README.md Normal file
View file

@ -0,0 +1,15 @@
# CCIoT Homework Lab 3
## Slide 6
### Problem
Try this out:
1. Make a vector of pairs (ints);
2. Sort the vector by the first element in the pair;
3. Replace the odd pairs with zero on the second element;
### Solution
See [slide6.cpp](./slide6.cpp).

34
Lab3/slide6.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
int main() {
// 1.
std::vector<std::pair<int, int>> v {{2, 8}, {9, 6}, {4, 7}, {22, 4}, {123, 321}, {1, 1}, {-7, 5}};
// 2.
std::sort(std::begin(v), std::end(v), [](const auto& e1, const auto& e2) {
return e1.first < e2.first;
});
// 3.
bool odd {true};
for (auto it {std::begin(v)}; it != std::end(v); ++it) {
if (odd) {
(*it).second = 0;
}
odd = !odd;
}
// Print the results
bool first {true};
for (const auto& item : v) {
if (first) first = false;
else std::cout << ", ";
std::cout << "(" << item.first << ", " << item.second << ")";
}
std::cout << std::endl;
return 0;
}

View file

@ -2,3 +2,4 @@
- [Lab1](./Lab1/) - [Lab1](./Lab1/)
- [Lab2](./Lab2/) - [Lab2](./Lab2/)
- [Lab3](./Lab3/)