need some kind of operator.. c++

后端 未结 6 1519
我寻月下人不归
我寻月下人不归 2021-01-13 22:06

I want to store a sequence of string in a queue. This seems pretty simple if i use the member function push()

queue test;
string s0(\"s0\"), s1(         


        
6条回答
  •  囚心锁ツ
    2021-01-13 22:29

    First off, your example of 'implicitly' enqueueing items has no mention of the queue - do you mean something like:

    test << s0 << s1 << s2 << s3;
    

    If so, it's possible, but I wouldn't recommend it. It really doesn't help readability that much. If you really do want it, though, put this in a header somewhere and include it wherever you want this behavior:

    template
    std::queue &operator<<(std::queue &q, const T &v)
    {
        q.push(v);
        return q;
    }
    

    Note that the opposite order - s0 >> s1 >> s2 >> test - is not possible, due to C++ precedence rules.

提交回复
热议问题