c++ - read-only variable is not assignable问题
PHP中文网
PHP中文网 2017-04-17 14:25:34
0
3
894

不知道该如何叙述这个问题,请看代码:

#include <set>
#include <iostream>
using namespace std;

struct test {
    int data[3];
};

int main(void) {
    test t1;
    set<test> s;
    s.insert(t1);
    s.begin()->data[0] = 1;//此处赋值报错:read-only variable is not assignable

    return 0;
}

应该是set内部的问题吧?
求高人解释,非常感谢...

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
小葫芦

The elements in the set cannot be changed, they can only be added or deleted. http://www.cplusplus.com/refe...

Sets are containers that store unique elements following a specific order.

In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

set containers are generally slower than unordered_set containers to access inpidual elements by their key, but they allow the direct iteration on subsets based on their order.

Sets are typically implemented as binary search trees.
左手右手慢动作

It seems to mean: Read-only variables cannot be assigned. . .

洪涛

The iterator of set is read-only and does not allow modification (because the elements are sorted internally in set, changing the value of the elements at will may destroy its ordering);

To change the value of an element, you can only delete the old one first and then insert the new one

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template