简体   繁体   English

如何访问集合中的结构值?

[英]How do I access a struct value that is in a set?

I am learning to code in c++ and I am learning to use sets currently, with the code (I will not use the specific code because of the length but will use an example) it wants me to use a struct and a set and with the things that needs to be done I need to be able to access and edit a variable in said struct while iterating through the set.我正在学习在 c++ 中编码,并且我正在学习使用集合,目前我正在学习使用代码(由于长度原因我不会使用特定代码,但会使用示例)它希望我使用结构和集合以及需要做的事情我需要能够在遍历集合时访问和编辑所述结构中的变量。

Here is an example of what I am trying to do:这是我正在尝试做的一个例子:

#include <iostream>
#include <set>

using namespace std;

struct myStruct
{
    int testVal;
    int exVal;
};

int main()
{
    set<myStruct> tst;
    myStruct struc;
    
    struc.testVal = 10;
    struc.exVal = 5;

    tst.insert(struc);
    
    struc.testVal = 1;
    struc.exVal = 7;
    
    tst.insert(struc);
    
    for (set<myStruct>::iterator it = tst.begin(); it != test.end(); it++)
    {
        if (*it.testVal >= *it.exVal)
        {
            //do something
        }
    }
    
    return 0;
}

but whenever I try to do something like this it always gives me a lot of errors.但是每当我尝试做这样的事情时,它总是会给我很多错误。 (This is an example so what is trying to be done may seem like a pointless reason to try to do this) One of the main errors is 'std::set<[struct name]>::iterator {aka struct std::_Rb_tree_const_iterator<[struct name]>' has no member named '[struct value name]' (这是一个示例,因此尝试执行此操作似乎是毫无意义的理由)主要错误之一是'std::set<[struct name]>::iterator {aka struct std::_Rb_tree_const_iterator<[struct name]>' has no member named '[struct value name]'

(anything in square brackets is not part of the error but dependent on what is in the code) (方括号中的任何内容都不是错误的一部分,但取决于代码中的内容)

in the case of this example I put here the error would say:在这个例子的情况下,我放在这里的错误会说:

error: 'std::set<myStruct>::iterator {aka struct std::_Rb_tree_const_iterator<myStruct>' has no member named 'testVal'

So how do I extract a value from a struct inside of a set using an iterator, and even possibly change it?那么如何使用迭代器从集合内的结构中提取值,甚至可能更改它?

std::set ensures it's elements are unique by having them in order. std::set通过按顺序排列来确保其元素是唯一的。 By default, that uses < .默认情况下,它使用< You don't have a < for myStruct , nor do you make a set with a different order.您没有myStruct< ,也没有以不同的顺序设置集合。

The simplest fix would be to add最简单的解决方法是添加

bool operator< (const myStruct & lhs, const myStruct & rhs) {
    return std::tie(lhs.testVal, lhs.exVal) < std::tie(rhs.testVal, rhs.exVal);
}

You may have to #include <tuple> for that.您可能必须为此#include <tuple>

even possibly change it?甚至可能改变它?

You can't modify elements of a set, because that might change the order.您不能修改集合的元素,因为这可能会改变顺序。 To change it you have to take it out of the set, and put the new value in.要更改它,您必须将其从集合中取出,并将新值放入。

auto node = tst.extract(it);
// modify node.value()
tst.insert(node);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM