Define lvalue and rvalue value in C ++ function

UPDATE: I reviewed some place, and now the problem has somehow changed.

I am writing a C ++ class. How:

class qqq{
    map<int,int> core;
    //......
    int& operator[](int n){return core[n];};
};
int main(){
    qqq a;
    a[3]=7;a[5]=0;//Case a
    int b=a[3];//Case b
    return 0;
}

Although case A and case B call the same function (overloaded operator), case a is used as an lvalue, and case b is used as an rvalue.

For some reason, I want to have the effect that if 0 is passed in [5], remove node 5 in the kernel. How:

int& operator[](int n){
    if(CASE A && THE VALUE PASSED TO IT IS 0)
        core.erase(core.find(n));
    else
        return core[n];
}

Perhaps my description is inaccurate.

+4
source share
4 answers

, . int . garbage , , .

,

 class qqq{
    static int garbage;
    map<int,int> core;
    //......

    int& operator[](int n){
        if(CASE A && THE VALUE PASSED TO IT IS 0)
            return garbage;
        else
            return core[n];
    }
};

, , . , , , , , .

* *

, , []. , setter getters. :

int set(int index, int value){
    if( value == 0)
        core.erase(core.find(index));
    else
        return core[index];
}

int get(int index) {
 return core[index];
}

[] , , .

0

, .

, , operator[], , .get(key, default) .init(key), .setdefault(key, default) .. .

// This code is C++11 but it not essential to the problem.
// The current code calls copy constructors more than necessary.
#include <map>

#include <cassert>


template<class K, class V>
struct zero_map
{
    struct proxy
    {
        std::map<K, V> *container;
        K key;

        operator V()
        {
            auto it = container->find(key);
            if (it == container->end())
                return V();
            return *it;
        }
        void operator = (V value)
        {
            if (value == V())
            {
                container->erase(key);
            }
            else
            {
                // probably should use .insert() and conditionally assign
                (*container)[key] = value;
            }
        }
    };

    std::map<K, V> _inner;

    proxy operator[](K k)
    {
        return proxy{&_inner, k};
    }
};

int main()
{
    zero_map<int, int> foo;
    assert (foo._inner.size() == 0);
    foo[1] = 0;
    assert (foo._inner.size() == 0);
    foo[0] = 1;
    assert (foo._inner.size() == 1);
    foo[0] = 0;
    assert (foo._inner.size() == 0);
}
0

, -.

template<typename T, size_t BadIndex>
class Element{ // please use a more meaningful name
    public:
        Element(const size_t index): index(index){}

        operator T& (){return value;}
        operator T const&() const{return value;}

        T &operator =(const T &rhs){
            if(index != BadIndex)
                value = rhs;
            return value;
        }

        operator T const&() const{return value;}
        operator T&(){return value;}

    private:
        T value;
        const size_t index;
};

class qqq{
    public:
        std::map<int, Element<int, 5>> core;

        Element<int> &operator [](size_t index){
            auto itt = core.find(index);
            if(itt == core.end()){
                core.emplace(index, index);
                itt = core.find(index);
            }
            return (*itt).second;
        }
};

, 5 .

0

, , , , ++. operator[] : , . , , , , ref.

, :

int getCore(int i) {
    return core[i];
}

void setCore(int i, int newval) {
    if (newval == 0) {
        core.erase(core.find(i));
    }
    else {
        core[i] == newval;
}
-1
source

All Articles