0

I have a random object as follows

struct myObject
{
    int numberOfSymbols;
    std::vector<int> symbolNumbers;
    std::vector<int> Pins;
    std::vector<std::string> pinNumList;
    std::map<std::string, std::string> pinNumNameMap;

}

std::vector<myObject> m_myObject;

And a function loads up this myObject vector with information. I need to do some sanity checking on that populated data as follows:

See what all objects have the same number of Pinsand pinNumNameMapand then do an action based on the result of that comparison.

I am not sure on how to group/find the objects that have the same number of pins because, sometimes this vector can grow as long as 50-100 objects. I am new to C++ coming from C#. In C# we use lambda expressoins/iterators etc to do this..not sure how do we do it in C++

rioki
  • 5,988
  • 5
  • 32
  • 55
Programmerzzz
  • 1,237
  • 21
  • 48

1 Answers1

1

You would need to define a custom operator== to loop over Pins and pinNumNameMap (incidentally, I just answered another question about comparing custom objects in a vector here).

Here is what I came up with, although it might not be exactly what you need depending on whether you need all the entries in Pins and pinNumNameMap to match exactly or if comparing the size of the containers is enough.

EDIT: I merged the two sections into one to illustrate the possible use.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <functional>

struct myObject
{
    int numberOfSymbols;
    std::list<int> symbolNumbers;
    std::list<int> Pins;
    std::list<std::string> pinNumList;
    std::map<std::string, std::string> pinNumNameMap;

    friend bool operator == (const myObject& _this, const myObject& _other)
    {
        // Check if entry sizes match
        if (_this.Pins.size() != _other.Pins.size() ||
            _this.pinNumNameMap.size() != _this.pinNumNameMap.size())
        {
            return false;
        }

        // Now check if the things inside the entries also match
        bool same(true);
        for (auto this_it = _this.Pins.begin(), other_it = _other.Pins.begin(); this_it != _this.Pins.end(); ++this_it, ++other_it)
        {
            same &= (*this_it == *other_it);
        }

        for (const auto& name : _this.pinNumNameMap)
        {
            same &= (_other.pinNumNameMap.find(name.first) != _other.pinNumNameMap.end() &&
                    _other.pinNumNameMap.at(name.first) == name.second);
        }
        return same;
    }

};

// std::reference_wrapper is used when you want to
// store references to objects in a container like
// std::vector (you can't store myObject&)
typedef std::reference_wrapper<myObject> ObjRef;

int main()
{

    std::vector<myObject> m_myObject;

    // This is your comparison object.
    // Use this to identify objectsin the original
    // container with matching Pins and pinNumNameMaps.
    myObject tmp;
    tmp.Pins = {1, 2, 3};
    tmp.pinNumNameMap =
    {
        {"pin 1", "name 1"},
        {"pin 2", "name 2"},
        {"pin 3", "name 3"}
    };

    std::vector<ObjRef> objectsWithCertainPins;

    for (auto& obj : m_myObject)
    {
        if (obj == tmp)
        {
            // Use std::reference_wrapper to avoid
            // copying large myObject instances
            objectsWithCertainPins.emplace_back(std::ref(obj));
        }
    }

    // Now you can iterate over objects in your
    // objectsWithCertainPins container.
    for (auto& obj : objectsWithCertainPins)
    {
        std::cout << "Pins:";
        for (const auto& p : obj.get().Pins)
        {
            std::cout << " " << p;
        }
        std::cout << "\n";
    }

    return 0;
}
cantordust
  • 1,542
  • 13
  • 17
  • ok.what if we want to compare each object in vector with all other objects and find those that have the same `pins` and `pinnumnamemap` – Programmerzzz Oct 25 '17 at 23:13