search

Home  >  Q&A  >  body text

c++ - “swap 只是交换了两个容器的内部数据结构”是什么意思?

《C++ Primer》第五版,中文版。303 页。

天蓬老师天蓬老师2804 days ago591

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:48:21

    For example, vector is often implemented using arrays, so the swap function just exchanges the arrays inside the two. .
    For example, there is

    vector a{ 1, 2, 3, 4, 5 }; // ptr -> [1, 2, 3, 4, 5] 内部大约是这样子
    vector b{ 6, 7, 8, 9, 10}; // ptr -> [6, 7, 8, 9, 10]

    When we do a.swap(b), we just exchange the pointing of the internal pointer ptr. .

    reply
    0
  • Cancelreply