Home > Software engineering >  Confused with c swap function: std::vector<int>().swap(search_indices);
Confused with c swap function: std::vector<int>().swap(search_indices);

Time:02-03

Here is the code, I am very confused. swap function is usually used to exchange the value of two parameters, like a.swap(b) or swap(a, b). What is the meaning of swap here?

std::vector<int> search_indices;
        std::vector<float> distances;

        int keypointNum = 0;
        do
        {
            keypointNum  ;
            std::vector<int>().swap(search_indices);
            std::vector<float>().swap(distances);

            int id;
            iterUnseg = unVisitedPtId.begin();
            id = *iterUnseg;
            indices->indices.push_back(features[id].ptId);
            unVisitedPtId.erase(id);

            tree.radiusSearch(features[id].pt, _curvature_non_max_radius, search_indices, distances);

            for (int i = 0; i < search_indices.size();   i)
            {
                unVisitedPtId.erase(search_indices[i]);
            }

        } while (!unVisitedPtId.empty()); 

I have looked for how swap function works, no related explanations.

CodePudding user response:

Given std::vector<int> v; definition, std::vector<int>().swap(v); clears vector v and disposes of the memory it reserved (so that v.capacity() returns 0). Starting from C 11, an arguably better way to write it is:

v.clear();
v.shrink_to_fit();

CodePudding user response:

It is a trick to clear a vector and free all the allocated memory for its elements.

In these statements

std::vector<int>().swap(search_indices);
std::vector<float>().swap(distances);

there are used empty temporary created vectors, std::vector<int>() and std::vector<float>(), that are swapped with the vectors search_indices and distances.

After the calls of the member function swap the both vectors search_indices and distances become empty. In turn the temporary vectors that after the swapping contain the elements of the above two vectors will be destroyed.

This trick is used because if you will just write

search_indices.clear();
distances.clear();

the allocated memory can be preserved. That is the member function capacity can return a non-zero value.

Here is a demonstration program.

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    std::cout << "v.size() = " << v.size() << '\n';
    std::cout << "v.capacity() = " << v.capacity() << '\n';

    std::cout << '\n';

    v.clear();

    std::cout << "v.size() = " << v.size() << '\n';
    std::cout << "v.capacity() = " << v.capacity() << '\n';

    std::cout << '\n';

    std::vector<int>().swap( v );

    std::cout << "v.size() = " << v.size() << '\n';
    std::cout << "v.capacity() = " << v.capacity() << '\n';
}

The program output is

v.size() = 5
v.capacity() = 5

v.size() = 0
v.capacity() = 5

v.size() = 0
v.capacity() = 0

As you can see after calling the member function swap with the temporary empty vector the capacity of the vector v becomes equal tp 0.

To get the same effect using the method clear you should after calling it also to call the method shrink_to_fit(). For example

v.clear();
v.shrink_to_fit();

CodePudding user response:

It seems that this is a strategy to free up memory. I wrote a test code here:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    std::vector<int> test(9, 0);
    std::cout <<test.size() << std::endl;
    std::vector<int>().swap(test);
    std::cout <<test.size() << std::endl;
    cout<<"Hello World";

    return 0;
}

The output is:

9 0 Hello World

  • Related