How can I copy one map into another using std::copy?

前端 未结 2 1746
深忆病人
深忆病人 2020-12-02 21:17

I would like to copy the content of one std::map into another. Can I use std::copy for that? Obviously, the following code won\'t work:

int main         


        
相关标签:
2条回答
  • 2020-12-02 21:27

    You need a variant of an insert iterator:

    std::copy(m1.begin(), m1.end(), std::inserter(m2, m2.end()) );
    

    inserter is defined in <iterator>. It requires a place to insert into (hence the m2.end()), and returns an insert_iterator.

    0 讨论(0)
  • 2020-12-02 21:43

    You can use GMan's answer --- but the question is, why do you want to use std::copy? You should use the member function std::map<k, v>::insert instead.

    m2.insert(m1.begin(), m1.end());
    
    0 讨论(0)
提交回复
热议问题