设置自定义对象的映射的输出C++

发布于 2025-01-30 18:39:31 字数 2535 浏览 2 评论 0原文

因此,我有一个具有用户定义的键的地图,其中的值也是对象的集合。因此,我正在尝试编写一些打印功能,但我不知道该怎么做。 (我是地图和集合的新手)。 我的问题功能:

void print() const 
    {
        for (auto& itr : my_mmap)
        {
            std::cout << "Key for this set:" << itr.first << "\n\n";
            for (int i = 0; i< itr.second.size(); i++)
            {
                std::cout << itr.second[i] << std::endl;
            }
        }
    }

这是我的课程:

#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <utility>
#include <set>

class Enclosing {
private:
    class Key {
        int m_number;
        std::string m_name;
    public:
        Key(int num, std::string name) :m_number(num), m_name(std::move(name)) {};

        bool operator<(const Key& rhs) const {
            return std::tie(m_number, m_name) < std::tie(rhs.m_number, rhs.m_name);
        }

        friend std::ostream& operator<<(std::ostream& os, const Key& k) {
            return os << '{' << k.m_number << ',' << k.m_name << '}';
        }
    };

    class Nested {
        std::string m_str;
        double m_dbl;
        bool m_boolean;
    public:
        Nested(std::string str, double dbl, bool boolean) :m_str(std::move(str)), m_dbl(dbl), m_boolean(boolean) {};

        friend std::ostream& operator<<(std::ostream& os, const Nested& n) {
            return os << '{' << n.m_str << ',' << n.m_dbl << ',' << n.m_boolean << '}';
        }
    };

    std::multimap<Key, std::set<Nested>> my_mmap;

public:
    template <class... Args>
    void add_new_object_to_mmap(Args&&... args) {
        my_mmap.emplace(std::piecewise_construct, std::forward<Args>(args)...);
    }
/*
THAT PROBLEM FUNCTION
*/
    void print() const 
    {
        for (auto& itr : my_mmap)
        {
            std::cout << "Key for this set:" << itr.first << "\n\n";
            for (int i = 0; i< itr.second.size(); i++)
            {
                std::cout << itr.second[i] << std::endl;
            }
        }
    }
    static Enclosing& get_singleton() {
        static Enclosing instance;
        return instance;
    }
};

}

,所以问题是我遇到了一个错误“ no oterator” []“匹配这些操作数”。如何输出地图并以最佳方式设置?

So I have a map that has user defined keys and the values in it are sets of objects too. So I'm trying to write some print function but I have no idea how to do that. (I'm kind of new to maps and sets).
My problem function:

void print() const 
    {
        for (auto& itr : my_mmap)
        {
            std::cout << "Key for this set:" << itr.first << "\n\n";
            for (int i = 0; i< itr.second.size(); i++)
            {
                std::cout << itr.second[i] << std::endl;
            }
        }
    }

Here's my class:

#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <utility>
#include <set>

class Enclosing {
private:
    class Key {
        int m_number;
        std::string m_name;
    public:
        Key(int num, std::string name) :m_number(num), m_name(std::move(name)) {};

        bool operator<(const Key& rhs) const {
            return std::tie(m_number, m_name) < std::tie(rhs.m_number, rhs.m_name);
        }

        friend std::ostream& operator<<(std::ostream& os, const Key& k) {
            return os << '{' << k.m_number << ',' << k.m_name << '}';
        }
    };

    class Nested {
        std::string m_str;
        double m_dbl;
        bool m_boolean;
    public:
        Nested(std::string str, double dbl, bool boolean) :m_str(std::move(str)), m_dbl(dbl), m_boolean(boolean) {};

        friend std::ostream& operator<<(std::ostream& os, const Nested& n) {
            return os << '{' << n.m_str << ',' << n.m_dbl << ',' << n.m_boolean << '}';
        }
    };

    std::multimap<Key, std::set<Nested>> my_mmap;

public:
    template <class... Args>
    void add_new_object_to_mmap(Args&&... args) {
        my_mmap.emplace(std::piecewise_construct, std::forward<Args>(args)...);
    }
/*
THAT PROBLEM FUNCTION
*/
    void print() const 
    {
        for (auto& itr : my_mmap)
        {
            std::cout << "Key for this set:" << itr.first << "\n\n";
            for (int i = 0; i< itr.second.size(); i++)
            {
                std::cout << itr.second[i] << std::endl;
            }
        }
    }
    static Enclosing& get_singleton() {
        static Enclosing instance;
        return instance;
    }
};

}

So the problem is that I am getting an error "no operator "[]" match these operands". How can I output my map and set in the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2025-02-06 18:39:31

问题是我们不能在std :: Set上使用索引。因此,itr.second [i]是无效的,因为itr.secondstd :: Set

为了解决此问题,您可以使用基于范围的循环的基于范围的,如下所示:

for (const auto&elem:itr.second)
{
     std::cout << elem << std::endl;
}

The problem is that we cannot use indexing on a std::set. Thus itr.second[i] is not valid because itr.second is an std::set.

To solve this you can use a range-based for loop as shown below:

for (const auto&elem:itr.second)
{
     std::cout << elem << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文