Hi. I am studying Bjarne Stroustrup's book. He gives the following code while explaining references:
Code:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct Pair
{
string name;
double val;
};
vector<Pair> pairs;
double& value(const string& s)
{
for(int i=0; i<pairs.size(); i++)
if(s==pairs[i].name)
return pairs[i].val;
Pair p={s, 0};
pairs.push_back(p);
return pairs[pairs.size()-1].val;
}
int main()
{
string buf="aa";
value(buf)++;
string buf2="aa";
value(buf2)++;
for(vector<Pair>::const_iterator p=pairs.begin();
p!=pairs.end (); ++p)
cout<<p->name<<": "<<p->val<< '\n';
cin.get();
return 0;
}
OK. First of all, I notice that if I do this:
Code:
double value(const string& s)
...
I get a compile-time error:
Code:
non-lvalue in increment
He speaks of these lvalues here. I don't follow.
Also, I don't quite understand this:
Code:
...const string& s)
Why is it const? What does this do?
Thanks in advance, Steve