1

I am new to C++. Say I have the following code:

vector<int> vec; 
vec.push_back(5); 
int x = vec.at(0);

From my understanding, x now represents a reference to the value in the vector. If this is the case, then why is it that if I were to have x = 7, the value in the vector would still be 5?

I tried searching on the site for a relevant thread, but did not see any that answered the question.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    No, `x` is not a reference. You don't see an `&` anywhere in its vicinity. Now, make it an `int &x`, instead, and it's a different story. – Sam Varshavchik Dec 13 '22 at 20:19
  • 4
    what does your book say about references and how they can be declared? – 463035818_is_not_an_ai Dec 13 '22 at 20:19
  • "I tried searching on the site for a relevant thread and did not see any that answered the question" do not use stackoverflow as your first source of information. stackoverflow cannot replace [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) nor [documentation](https://en.cppreference.com/w/cpp/language/reference) – 463035818_is_not_an_ai Dec 13 '22 at 20:22
  • 3
    @463035818_is_not_a_number the C++ documentation says that `vec.at(n)` returns a reference to the element. That's where I'm getting the idea of it being a reference. – lafarrjam99 Dec 13 '22 at 20:23
  • 3
    `at` returns a reference, but `int x;` is an `int`. Assigning something else to `x` does not magically change its type. – 463035818_is_not_an_ai Dec 13 '22 at 20:24
  • You do have a point, from [vector::at() and vector::swap() in C++ STL - GeeksforGeeks](https://www.geeksforgeeks.org/vectorat-vectorswap-c-stl/) "`vectorname.at(position)` Parameters: Position of the element to be fetched. Returns: **Direct reference to the element at the given position**. If the position is not present in the vector, it throws out_of_range" – Greenonline Dec 13 '22 at 20:24
  • 2
    @Greenonline `at` returns a reference, but the code shown copies the result to `int x` which is not a reference. – François Andrieux Dec 13 '22 at 20:27
  • 2
    @Greenonline that site is a terrible reference. – 463035818_is_not_an_ai Dec 13 '22 at 20:29
  • @463035818_is_not_a_number - Fair point, agreed, but unfortunately, it is the top, or first, link that google throws up. – Greenonline Dec 13 '22 at 20:31
  • Bah. Google is for the weak. Real men memorize the [C++ Standard.](http://eel.is/c++draft/) Note: this posting is using an extreme position for humorous purposes. – user4581301 Dec 13 '22 at 20:54

1 Answers1

4

int x declares an integer. It is not a reference. In your code a copy is made of the value in the vector.

If you want a reference then you need to declare x as a reference to int:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v{42};
    int& x = v.at(0);
    x = 5;
    std::cout << v.at(0);
}

For further reading I suggest you pick a book and cppreference can be recommended very much https://en.cppreference.com/w/cpp/language/reference.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185