swap two values with a member function

I understand if we simply define void swap(int a, int b), then calling this swap(x, y) function will not change the value of x and y, since x and y are "copied" into a and b, and swapping does not have any effect on x and y. Unless we use pointer or reference, there's no way to swap the value of x and y.

Now I have a question on this swap function. Following is the code implementing the bubble sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>

using namespace std;

/**************************************************************************************/
/*                                 class declaration                                  */
/**************************************************************************************/
class ArrayBub
{
public:
    ArrayBub(int max);              // constructor
    ~ArrayBub();
    void insert(double value);
    void display() const;
    void bubbleSort();
private:
    vector<double> m_vec;
    size_t m_numOfElements;
    void swap(int one, int two);
};

/**************************************************************************************/
/*                                 main function                                      */
/**************************************************************************************/
int main()
{
    size_t maxSize = 0;             // to use push_back(), maxSize has to be 0;
    ArrayBub arr(maxSize);
    arr.insert(77);
    arr.insert(99);
    arr.insert(44);
    arr.insert(55);
    arr.insert(22);
    arr.insert(88);
    arr.insert(11);
    arr.insert(00);
    arr.insert(66);
    arr.insert(33);

    cout << "Before sorting, the original array is:\n";
    arr.display();
    cout << "\n\nNow, let's test the bubble sort algorithm.\n";
    arr.bubbleSort();
    cout << "After sorting, the sorted array becomes:\n";
    arr.display();

    cout << endl;
    return 0;
}

/**************************************************************************************/
/*                          class member function definition                          */
/**************************************************************************************/
ArrayBub::ArrayBub(int max) : m_numOfElements(0)
{
    m_vec.resize(max);
}

ArrayBub::~ArrayBub()
{
}

void ArrayBub::insert(double value)
{
    m_vec.push_back(value);
    ++m_numOfElements;
}

void ArrayBub::display() const
{
    for (auto iter = m_vec.begin(); iter != (m_vec.begin()+m_numOfElements); ++iter )
        cout << *iter << " ";
}

void ArrayBub::bubbleSort()
{
    size_t outter, inner;
    for (outter = m_numOfElements-1; outter > 1; --outter)
        for (inner = 0; inner < outter; ++inner)
            if (m_vec[inner] > m_vec[inner+1])
                swap(inner, inner+1);
}

void ArrayBub::swap(int one, int two)
{
    double temp = m_vec[one];
    m_vec[one] = m_vec[two];
    m_vec[two] = temp;
}


The private member function void ArrayBub::swap(int one, int two) does not use pointers or references, but it successfully swaps two values. In the example, one and two are used as the index for the private member m_vec. So in this way the vector m_vec can swap the values? I seem to understand a little bit, but not completely. So could you please explain the mechanism behind this more in details?

Thanks.
This swap function isn't actually swapping the values passed, but rather swapping the values indicated at a position by the values passed. This means that they don't need to be passed by reference, as it is only swapping the values within this->m_vec.
closed account (iAk3T05o)
Are you trying to sort an array, vector or a vector array?
@Nathan2222
Look at line 18: vector<double> m_vec;.

Also, looking at that code, there are a few things that are redundant. For example, the variable for the number of elements should instead be replaced with just calls to m_vec.size(), and in fact the swap function has better alternatives, too (such as std::vector::swap).
Last edited on
i get the following error :
warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
and
'iter' does not name a type
and
expected ';' before 'iter'
and
'iter' was not declared in this scope
Try compiling with -std=c++11, to enable C++11 functionality such as the auto keyword.
closed account (iAk3T05o)
Since i never really understood insertion/bubble/other handcoded sorting algorithms, i stuck to the easiest i could find:
1
2
 
std::sort (std::begin(/*array/vector name*/), std::end(/*array/vector name*/));
.
One simple line :)
Last edited on
Thanks for all the explanation and discussion. This is a sample code from "Sams Teach Yourself Data Structure and Algorithm", Hour 4. I'm not trying to modify the code, (actually I modified a little bit, like using iterator instead of index, using push_back() instead of directly assigning values). I'm just wondering how this swap() function works since it is not using pointer and reference.

As NT3 indicates, it seems that this swap() function works as passing pointers rather that passing values. Is that correct?

Nathan2222, thanks for your suggestion, actually I know that sort() function from STL, I just want to implement these famous but non-optimized algorithm for the sake of self-learning.

Thanks a lot.
Last edited on
@NT3,
how can I do that?
What IDE are you using? If you are using Code::Blocks, if you go into Project->Build Options, there should be an option there to specify that the compiler uses the C++11 standard, with [-std=c++11] in brackets next to it.
yeah, thanx that worked.
As NT3 indicates, it seems that this swap() function works as passing pointers rather that passing values. Is that correct?

The swap function works by passing a pointer to an object (this) and two indexes used to dereference a data member of *this. The values passed are not swapped. They are used to get at the elements that should be swapped.

If it were a standalone friend function the implementation of swap might look like this:

1
2
3
4
5
6
void swap(ArrayBub* this, int one, int two)
{
    double temp = this->m_vec[one];
    this->m_vec[one] = this->m_vec[two];
    this->m_vec[two] = temp;
}
Topic archived. No new replies allowed.