Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector int swap implementation?

Tags:

c++

I have a simple problem with the following C++ vector swapping code:

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

class Base
{
private:
    std::vector<int> vec;
public:
    Base(std::vector<int> v) : vec(v) {}

    std::vector<int> getVec() {
        return vec;
    }

    void setVec(std::vector<int> vec) {
        this->vec = vec;
    }

    void printVec() {
        for (auto &v : vec) {
            std::cout << v << std::endl;
        } 
    }

    void swap(Base b) {
        std::vector<int> tmp = vec;

        vec = b.getVec();

        b.setVec(tmp);
    }
};


int main()
{
    std::vector<int> v1 = {1, 2, 3, 4};
    std::vector<int> v2 = {5, 6, 7, 4};

    Base b1(v1);
    Base b2(v2);

    b1.swap(b2);

    b1.printVec();
    b2.printVec();

    return 0;
}

I expect the program to print (indicating a successful swap)

5                                                                                                                                                                                                                         
6                                                                                                                                                                                                                         
7                                                                                                                                                                                                                         
4                                                                                                                                                                                                                         
1                                                                                                                                                                                                                         
2                                                                                                                                                                                                                         
3                                                                                                                                                                                                                         
4 

But it prints

5                                                                                                                                                                                                                         
6                                                                                                                                                                                                                         
7                                                                                                                                                                                                                         
4                                                                                                                                                                                                                         
5                                                                                                                                                                                                                         
6                                                                                                                                                                                                                         
7                                                                                                                                                                                                                         
4 

So it looks like only the first vector gets swapped correctly, not the second one, what is wrong with this code? I'm getting confused as when I add print statements in the swap function it appears to me that they the second vector gets swapped correctly, but then it goes out of scope???

like image 878
A Beautiful Indian Man Avatar asked Dec 18 '25 21:12

A Beautiful Indian Man


1 Answers

swap takes its parameter by value, so the local variable b is just a copy of the argument. Any modification (like b.setVec(tmp);) has nothing to with the original argument.

Change it to pass-by-reference, i.e.

void swap(Base& b) {
    std::vector<int> tmp = vec;

    vec = b.getVec();

    b.setVec(tmp);
}

PS: I suppose you want to implement swap by yourself, otherwise you can take advantage of std::vector::swap or std::swap, e.g.

void swap(Base& b) {
    vec.swap(b.vec);
}
like image 54
songyuanyao Avatar answered Dec 21 '25 11:12

songyuanyao