Pointers in functions

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
void display(const vector<string>& vec); //prototype

int main()
{
	
	vector<string> inventory;
	inventory.push_back("Obj1");
	inventory.push_back("Obj2"); //load the vector 
	inventory.push_back("Obj3");

	display(inventory);

	cin.get();

	return 0;
}

void display(const vector<string>& vec)
{
	vector<string>::const_iterator iter; //iterator to go through the loop

	cout<<"Your items\n ";
	for(iter = vec.begin();iter != vec.end();++iter)  //display the contents of the vector
		cout << *iter << endl;
}


Hey guys,

I'm looking into pointers in functions, and in this part of the program there is an "&" sign. Ive known these to be "An address of" and the program runs with our without them, I'm just wondering what exactly are they doing?
This has nothing really to do with pointers.

Here, the & operator is not "address of". Instead, it's the reference operator.

it means that 'vec' is a const reference to the vector, rather than being a copy of the vector.

Passing by reference is typically more efficient with large objects like vectors, where copying may be expensive.
Topic archived. No new replies allowed.