References and Pointers Flashcards
give existing variables new names
allow modification of arguments or items
avoid copying data
references
the original iterator
pointers
int x = 5;
int y = x;
what is the value of y?
5
int x = 5;
int y = x;
y = 10;
what is the value of x now?
5
the line y = x; copied the value of x into y
the blank makes a reference
& ampersand;
a reference is a blank
new name for something
True or False
you can change what a reference refers to after it is created
False
One of the main places we use references is with blank
function arguments
why is the argument for print_vector both const and a reference?
a const reference argument avoids the cost of copying the vector, but does not allow the vector to be accidentally modified by the function
blank is usually the right way to pass an argument
const reference
unless
the function needs to modify the variable passed in
the argument has a simple data type, such as int or double
References can be used with blank to allow modification to the value of the items in the container:
range-style for loops
references and range-style for loops
for(int&x : numbers)
{
x = x * 2;
}
for (pair& p : scores)
{
p.second = p.second + 5.0;
}
**no need for iterators
blank is like a big vector of bytes
memory
each byte has an address; the first byte is blank, the second is blank, etc
first byte is 0
the second byte is 1
Each object is given a blank of those bytes to store its value
subsequence
ex: an int is given 4 bytes
a double is given 8 bytes