0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
刘醒醒
  • 9
  • 1

3 Answers3

5

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();
Eugene
  • 6,194
  • 1
  • 20
  • 31
  • 2
    ["It is a **non-binding** request to reduce capacity() to size(). It depends on the implementation whether the request is fulfilled. "](https://en.cppreference.com/w/cpp/container/vector/shrink_to_fit). `shrink_to_fit` potentially changes nothing about the vectors capactiy. Swapping like in OPs code is guaranteed to end up with a empty vector with empty capacity – 463035818_is_not_an_ai Feb 02 '23 at 18:18
  • 2
    @463035818_is_not_a_number While using `shrink_to_fit` isn't strictly guaranteed to reduce capacity, any rational implementation will work as intended in this case (with a size of 0). The reason it is non-binding is to provide leeway for implementations to shrink capacity to whatever value fits their allocation scheme (ex. power of 2 or whatever they use for amortized resize). – François Andrieux Feb 02 '23 at 18:22
  • @FrançoisAndrieux ok, actually I had in mind that `shrink_to_fit` was already older and that the swapping was to overcome the "non-binding". Seems like that was wrong, and the swapping was just the – 463035818_is_not_an_ai Feb 02 '23 at 18:26
2

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();
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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

刘醒醒
  • 9
  • 1