Zeyd-RJEB / snippets

0 stars 0 forks source link

Ob #9

Open Zeyd-RJEB opened 1 month ago

Zeyd-RJEB commented 1 month ago
#include <vector>
#include <unordered_map>
#include <ranges>
#include <algorithm>  // for std::transform
#include <utility>    // for std::pair
#include <string>     // assuming your ID is a string, adjust if necessary

struct MyObject {
    std::string id;
    // other members...
};

int main() {
    std::vector<MyObject> objects = { /* fill with your objects */ };

    auto key_value_pairs = objects | std::views::transform([](const MyObject& obj) {
        return std::pair{obj.id, obj};
    });

    std::unordered_map<std::string, MyObject> object_map = key_value_pairs | ranges::to<std::unordered_map>();

    // Now object_map is an unordered_map with id as key and MyObject as value
}
Zeyd-RJEB commented 1 month ago
#include <variant>
#include <iostream>
#include <stdexcept>

// Define shape structures
struct Circle {
    double radius;
};

struct Rectangle {
    double width;
    double height;
};

struct Triangle {
    double base;
    double height;
};

// Use std::variant to hold any of the shape types
using Shape = std::variant<Circle, Rectangle, Triangle>;

// Implement Visitor for Area Calculation
struct AreaCalculator {
    double operator()(const Circle& circle) const {
        return 3.14159 * circle.radius * circle.radius;
    }

    double operator()(const Rectangle& rectangle) const {
        return rectangle.width * rectangle.height;
    }

    double operator()(const Triangle& triangle) const {
        return 0.5 * triangle.base * triangle.height;
    }
};

// Perform the calculation using std::visit
double calculateArea(const Shape& shape) {
    return std::visit(AreaCalculator{}, shape);
}

// Usage example
int main() {
    Shape circle = Circle{5.0};
    Shape rectangle = Rectangle{4.0, 6.0};
    Shape triangle = Triangle{3.0, 4.0};

    std::cout << "Circle area: " << calculateArea(circle) << std::endl;
    std::cout << "Rectangle area: " << calculateArea(rectangle) << std::endl;
    std::cout << "Triangle area: " << calculateArea(triangle) << std::endl;

    return 0;
}
Zeyd-RJEB commented 2 weeks ago

grep '35=D' fix.log | awk -F '|' '{ for(i=1;i<=NF;i++) { if ($i ~ /11=/) print $i }}' | sed 's/11=//' | grep -Fxv -f <(grep '35=8' fix.log | grep '150=F' | awk -F '|' '{ for(i=1;i<=NF;i++) { if ($i ~ /11=/) print $i }}' | sed 's/11=//')