Re: How does this work...
From: Mark P (not_at_my.real.email)
Date: 03/01/05
- Next message: Edd: "Re: How does this work..."
- Previous message: Jedispy: "Re: C++...and I'm stumped!"
- In reply to: Michael: "How does this work..."
- Next in thread: Michael: "Re: How does this work..."
- Reply: Michael: "Re: How does this work..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 01 Mar 2005 01:20:43 GMT
Michael wrote:
> The following code snippet is from Accelerated C++, but I don't
> understand how it can work:
>
> typedef vector<string> Rule;
> typedef vector<Rule> Rule_collection;
> typedef map<string, Rule_collection> Grammar;
>
> // read a grammar from a given input stream
> Grammar read_grammar(istream& in)
> {
> Grammar ret;
> string line;
>
> // read the input
> while (getline(in, line)) {
>
> // `split' the input into words
> vector<string> entry = split(line);
>
> if (!entry.empty())
> // use the category to store the associated rule
> ret[entry[0]].push_back(
> Rule(entry.begin() + 1, entry.end()));
> }
> return ret;
> }
>
>
> The bit I don't understand is this line:
>
> ret[entry[0]].push_back(Rule(entry.begin() + 1, entry.end()));
>
> More specifically, the bil I don't understand is the 'Rule' bit because
> its not an object. Can someone please explain how that code is valid.
>
> Cheers
> Micheal
It's invoking the constructor for Rule, one form of which takes two
iterators as arguments, and creating an unnamed object which is then
inserted into ret[entry[0]]. This form of the constructor copies the
elements between the two iterators to initialize the newly created object.
- Next message: Edd: "Re: How does this work..."
- Previous message: Jedispy: "Re: C++...and I'm stumped!"
- In reply to: Michael: "How does this work..."
- Next in thread: Michael: "Re: How does this work..."
- Reply: Michael: "Re: How does this work..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|