1

I'm used to program in C# or Java, so I'm doing really bad in C++. I believe it's easy but I just can't make this work. Please help me.

I have this:


void swap(vector  * vet, int i, int j)
{
    int temp = vet[i];
    vet[i] = vet[j];
    vet[j] = temp;
}

I'm calling the method this way:


swap(&vet, j, j - 1);

What I want is to pass the vector using pointers instead of using value.

Obs: The code compiles well without the "*" and "&".

Please don't say that I have to at least try to study pointers, because I did. I just can't make this damn thing work!

4
  • @AusCBloke, its just a vector... Commented Oct 22, 2012 at 22:04
  • yes, it's just a vector. I couldnt show the code well because I can't write <> in here. But I do this way: vector<int> vet. Commented Oct 22, 2012 at 22:06
  • 2
    @Link: Is it though? Without seeing the declaration of vet we might be missing the crux of the error. Commented Oct 22, 2012 at 22:07
  • 1
    Other answers have said use a reference (which is the correct answer). But just in case you were wondering the way to do it with pointers is (*vec)[i] = (*vec)[j]; etc. Commented Oct 22, 2012 at 22:10

4 Answers 4

5

You should take the vector by reference rather than "pass by pointer".

void swap(std::vector<int>& vet, std::size_t i, std::size_t j)
{
    using std::swap;
    swap(vet[i], vet[j]);
}

http://en.cppreference.com/w/cpp/algorithm/swap

Note the more idiomatic:

http://en.cppreference.com/w/cpp/algorithm/iter_swap

Sign up to request clarification or add additional context in comments.

Comments

4

Everyone has thus far responded by telling you to use references, which is correct, but they fail to explain why your code doesn't work. The problem here is that you do not understand pointer arithmetic.

Let's say we have a a pointer to 10 ints:

// best to use a vector for this, but for the sake of example...
int *p = new int[10];

Now, if we want to change the value of the second int in that chunk of memory we can write:

*(p + 1) = 20;

Or, the equivalent:

p[1] = 20;

See? Those two lines do the same thing. Adding n to a pointer increases the address of the pointer by n * sizeof *p bytes. Pointer arithmetic is convenient because it hides the sizeof bit from you and allows you to work with logical units (elements) instead of bytes.

So, knowing that, back to your broken code:

vet[i] = vet[j];

This indexes i * sizeof *vet bytes away from the pointer, i.e., i full vectors away from the base address. Obviously that is wrong, you wanted to invoke operator[] on the vector, i.e., treat it as an array. It is not an array however, so the correct syntax would be:

(*vec)[i]

Or

vec->operator[](i);

That said... just use a reference. Safer (object guaranteed to be valid) and idiomatic.

6 Comments

oh great, I didn't know that. But yes, reference seems better to me =). Thanks!
I dont know about others, but I just learned a new thing...never thought about role of operator[] for index operations...
There is a typo in the explanation, there should be n * sizeof *p.
@JaroslawWaliszko: I'm sorry, I'm not sure what you mean. I originally wrote (and still have) n + sizeof *p, which is correct. The compiler knows how to treat *p in a sizeof statement to produce sizeof int (in this case) and it is better than explicitly stating the type as the type may change, in which case, you now have a bug.
Mabye it's a time to sleep for me now but I was thinking about the formula for computing the address of ptr + n where ptr has type T *. Then the formula for the address is: addr(ptr + n) = addr(ptr) + n * sizeof(T)
|
3

You can try something like....

void swap(vector<int> &vet, int i, int j)
{
    int temp = vet[i];
    vet[i] = vet[j];
    vet[j] = temp;
}

and call your swap function as

swap(vet,i,j);

Bottomline: Use reference variables.They are more like reference in Java.

3 Comments

I agree with this answer. However, if you wanted to make it work the way you have it you could write (*vet)[i] or vet->operator[](i)
Can't upvote this because it doesn't enlighten the OP as to why his code was wrong to begin with.
@Seva: Better to learn how things work so you don't repeat the same mistakes time and time again.
2

In fact, in C++ you'd just say

using std::swap;

swap(vet[i], vet[j]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.