C++:vector、string、list、forward_list、deque、array的swap


1. vector of swap (list, forward_list, deque Similarly)

#include <iostream>
#include <vector>
using namespace std;
void show(int i, vector<int> &v1, vector<int> &v2, vector<int>::iterator i1, vector<int>::iterator i2, int &r1, int &r2, int *p1, int *p2);

int main()
{
   vector<int> v1(1, 1);
   vector<int> v2(2, 2);
   // 迭代器
   auto i1 = v1.begin();
   auto i2 = v2.begin();
   //引用
   int &r1 = *v1.begin();
   int &r2 = *v2.begin();
   //指针
   int *p1 = &(*v1.begin());
   int *p2 = &(*v2.begin());
   //输出
   show(0, v1, v2, i1, i2, r1, r2, p1, p2);
   //交换
   swap(v1, v2);
   //输出
   cout << "\n--------------------------------------------------" << endl;
   show(1, v1, v2, i1, i2, r1, r2, p1, p2);
   return 0;
}

void show(int i, vector<int> &v1, vector<int> &v2, vector<int>::iterator i1, vector<int>::iterator i2, int &r1, int &r2, int *p1, int *p2)
{
   if (i)
   {
       cout << "vector after:\n"
            << endl;
   }
   else
   {
       cout << "vector before:\n"
            << endl;
   }

   cout << "v1:";
   for (auto &a : v1)
   {
       cout << a << "(" << &a << ")\t";
   }

   cout << endl;
   cout << "i1->" << *i1 << "(" << &(*i1) << ")"
        << "\tr1->" << r1 << "(" << &r1 << ")"
        << "\tp1->" << *p1 << "(" << &(*p1) << ")" << endl;

   cout << "\nv2:";
   for (auto &a : v2)
   {
       cout << a << "(" << &a << ")\t";
   }
   cout << endl;

   cout << "i2->" << *i2 << "(" << &(*i2) << ")"
        << "\tr2->" << r2 << "(" << &r2 << ")"
        << "\tp2->" << *p2 << "(" << &(*p2) << ")" << endl;
}

FIG exchange vector

operation result

analysis:

  1. Iterator i1, references r1, p1 pointer always points 0x2c0f88, a value of 1, remain unchanged; iterator i2, referenced r2, p2 pointer always points 0x2c0f98, a value of 2, remains unchanged.
  2. When the swap, v1 and v2 which is the address of the exchange elements. v1 first element address from 0x2c0f88 replaced 0x2c0f98, v2 first element address from 0x2c0f98 replaced 0x2c0f88.

2. array的swap

#include <iostream>
#include <array>
using namespace std;
void show(int i, array<int, 2> &a1, array<int, 2> &a2, array<int, 2>::iterator i1, array<int, 2>::iterator i2, int &r1, int &r2, int *p1, int *p2);

int main()
{
    array<int, 2> a1{1, 1};
    array<int, 2> a2{2, 2};
    // 迭代器
    auto i1 = a1.begin();
    auto i2 = a2.begin();
    //引用
    int &r1 = *a1.begin();
    int &r2 = *a2.begin();
    //指针
    int *p1 = &(*a1.begin());
    int *p2 = &(*a2.begin());
    //输出
    show(0, a1, a2, i1, i2, r1, r2, p1, p2);
    //交换
    swap(a1, a2);
    //输出
    cout << "\n--------------------------------------------------" << endl;
    show(1, a1, a2, i1, i2, r1, r2, p1, p2);
    return 0;
}

void show(int i, array<int, 2> &a1, array<int, 2> &a2, array<int, 2>::iterator i1, array<int, 2>::iterator i2, int &r1, int &r2, int *p1, int *p2)
{
    if (i)
    {
        cout << "array after:\n"
             << endl;
    }
    else
    {
        cout << "array before:\n"
             << endl;
    }

    cout << "a1:";
    for (auto &a : a1)
    {
        cout << a << "(" << &a << ")\t";
    }

    cout << endl;
    cout << "i1->" << *i1 << "(" << &(*i1) << ")"
         << "\tr1->" << r1 << "(" << &r1 << ")"
         << "\tp1->" << *p1 << "(" << &(*p1) << ")" << endl;

    cout << "\na2:";
    for (auto &a : a2)
    {
        cout << a << "(" << &a << ")\t";
    }
    cout << endl;

    cout << "i2->" << *i2 << "(" << &(*i2) << ")"
         << "\tr2->" << r2 << "(" << &r2 << ")"
         << "\tp2->" << *p2 << "(" << &(*p2) << ")" << endl;
}

FIG array exchange process

operation result

analysis:

  1. Iterator i1, references r1, p1 pointer always points 0x22feb0, but its value from 1 to transducer 2; iterator i2, referenced r2, p2 pointer always points 0x22fea8, but its value is also changed from 1 to 2.
  2. When the swap, a1 and a2 is a value of its exchange elements. a1 and a2 first element address unchanged.

3. string of swap

#include <iostream>
#include <vector>
using namespace std;
void show(int i, string &s1, string &s2, string::iterator i1, string::iterator i2, char &r1, char &r2, char *p1, char *p2);

int main()
{
    string s1 = "a";
    string s2 = "bc";
    // 迭代器
    auto i1 = s1.begin();
    auto i2 = s2.begin();
    //引用
    char &r1 = *s1.begin();
    char &r2 = *s2.begin();
    //指针
    char *p1 = &(*s1.begin());
    char *p2 = &(*s2.begin());
    //输出
    show(0, s1, s2, i1, i2, r1, r2, p1, p2);
    //交换
    swap(s1, s2);
    //输出
    cout << "\n--------------------------------------------------" << endl;
    show(1, s1, s2, i1, i2, r1, r2, p1, p2);
    return 0;
}

void show(int i, string &s1, string &s2, string::iterator i1, string::iterator i2, char &r1, char &r2, char *p1, char *p2)
{
    if (i)
    {
        cout << "string after:\n"
             << endl;
    }
    else
    {
        cout << "string before:\n"
             << endl;
    }

    cout << "s1:";
    for (auto &a : s1)
    {
        cout << a << "(" << &a << ")\t";
    }

    cout << endl;
    cout << "i1->" << *i1 << "(" << &(*i1) << ")"
         << "\tr1->" << r1 << "(" << &r1 << ")"
         << "\tp1->" << *p1 << "(" << &(*p1) << ")" << endl;

    cout << "\ns2:";
    for (auto &a : s2)
    {
        cout << a << "(" << &a << ")\t";
    }
    cout << endl;

    cout << "i2->" << *i2 << "(" << &(*i2) << ")"
         << "\tr2->" << r2 << "(" << &r2 << ")"
         << "\tp2->" << *p2 << "(" << &(*p2) << ")" << endl;
}

operation result

analysis:

  1. string when performing swap, similar to the vector, i.e. exchanging only the internal data structure of the container.
  2. Because the string is too short, too wasteful of memory assigned, so it is stored in the temporary memory area in which an address different from other container elements. When the string execute swap, it will be re-allocated memory for the string, resulting iterators, pointers and references fail.

4. Summary

  1. When performing array swap, corresponding to the real exchange element values ; container exchanging only other internal data structure of its container (i.e., element address ).
  2. When performing array swap, it requires two containers container type, the number of elements and element types same; other containers only requires container type and element types same.
  3. string after performing swap, which points to the original container element iterators, references and pointers failure ; other iterators original container, and a reference pointer is still valid .
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/105109990