need some kind of operator.. c++

后端 未结 6 1520
我寻月下人不归
我寻月下人不归 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:51

    I would not really do it, but if you really feel that you need to provide that syntax, you can write an inserter adapter...

    template 
    class inserter_type {
    public:
       typedef C container_type;
       typedef typename container_type::value_type value_type;
    
       explicit inserter_type( container_type & container ) : container(container) {}
       inserter_type& operator<<( value_type const & value ) {
          container.push( value );
          return *this;
       }
    private:
       container_type & container;
    };
    template 
    inserter_type inserter( C & container ) {
       return inserter_type(container);
    }
    int main() {
       std::queue q;
       inserter(q) << "Hi" << "there";
    }
    

提交回复
热议问题