【问题标题】:set list in c++C++中的设置列表
【发布时间】:2011-11-21 17:00:37
【问题描述】:

我在 C++ 中创建了一个集合列表 并充满了元素

std::set<Unit*> myUnits; 

for(std::set<Unit*>::iterator i = myUnits.begin(); i != myUnits.end(); i++) {   
    if() {}
}

所以我想在 if 中检查 setlist 的每个元素,if 必须放入什么?

【问题讨论】:

  • 例如在 myUnits 中我有一些字符串,我想找出有多少字符串与特定名称相同。
  • *i 是当前正在调查的项目。
  • 一般来说,我怎样才能在集合集合中访问我的 c++。我有点困惑。
  • 所以类似于 myUnits(i)???
  • 使用std::set 的指针似乎有点奇怪。您是否需要按内存地址对内容进行排序而没有重复?如果您只需要一个通用的对象容器,那么std::vector&lt;Unit&gt; 可能是更好的选择。

标签: c++ set stdset


【解决方案1】:

你可能想要这样的东西:

(*i)->stringVal

【讨论】:

    【解决方案2】:

    Unit* pUnit = *i; 会给你一个指向Unit 对象的指针。顺便说一句,容器的正确术语是“set”,而不是“setlist”。

    【讨论】:

    • 是的,我将它与 java 中的设置列表混淆了。例如,我如何访问 myUnits 的第一个元素??
    • 如果您询问元素的顺序,它们(默认情况下)根据它们的&lt; 运算符排序(如果您愿意,可以定义自己的该运算符版本)。
    【解决方案3】:

    我不知道你想要什么,但我们假设Unit 类有一个bool Unit::check() 方法。然后你必须写:

    if (i->check()) {...}
    

    编辑: 对不起,我没有意识到你有一套指针...... 我不确定这是否是您真正想要的,因为该集合将比较指针地址而不是单元的内容,以定义它们是否相等。 这是一个小代码示例,向您展示如何使用带有 Unit-objects 和指向 Unit-objects 的指针的集合:

    class Unit
    {
    public:
        Unit(unsigned int id, bool c)
        {
        this->id = id; // should be unique
        checked = c;
        }
    
        bool check() const
        {
        return checked;
        }
    
        unsigned int getId() const
        {
        return id;
        }
    
        bool operator<(const Unit &u) const // this is needed for the set<Unit>, otherwise two Units can't be compared
        {
        return this->id < u.id;
        }
    
    private:
        bool checked;
        unsigned int id;
    };
    
    void setTest()
    {
        set<Unit> myUnits;
    
        Unit u1(1,true);
        Unit u2(2,false);
        Unit u3(2,true);
    
        myUnits.insert(u1);
        myUnits.insert(u2);
        myUnits.insert(u3);
    
       cout << "set<Unit>:" << endl;
       for (std::set<Unit>::iterator it = myUnits.begin(); it != myUnits.end(); ++it)
       {
            if (it->check()) // you can access the Unit-object stored in the set like this...
            {
                cout << "Unit " << it->getId() << ": checked" << endl;
            }
            else
            {
                            // ... or like this
                Unit u = *it;
                cout << "Unit " << u.getId() << ": check failed" << endl;
            }
        }
    
        set<Unit*> myUnitPtrs;
    
        myUnitPtrs.insert(&u1);
        myUnitPtrs.insert(&u2);
        myUnitPtrs.insert(&u3);
    
        cout << "set<Unit*>:" << endl;
        for (std::set<Unit*>::iterator it = myUnitPtrs.begin(); it != myUnitPtrs.end(); ++it)
        {
            if ((*it)->check()) // you can access a Unit-Pointer like this ...
            {
                cout << "Unit " << (*it)->getId() << ": checked" << endl;
            }
            else
            {
                Unit *u = *it; // ... or like this
                cout << "Unit " << u->getId() << ": check failed" << endl;
            }
        }
    }
    

    输出应该是:

    set<Unit>:
    Unit 1: checked
    Unit 2: check failed // inserting u3 doesn't change the set as a Unit with id 2 is already present in the set
    set<Unit*>:
    Unit 1: checked
    Unit 2: check failed
    Unit 2: checked // now there's two Units with id 2, because u2 and u3 have different adresses
    

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2010-11-28
      • 2019-02-25
      相关资源
      最近更新 更多