Re: More STL questions
From: Mark P (not_at_my.real.email)
Date: 10/01/04
- Next message: Karl Heinz Buchegger: "Re: More STL questions"
- Previous message: Spacen Jasset: "Re: insight on references vs. pointers"
- In reply to: Karl Heinz Buchegger: "Re: More STL questions"
- Next in thread: Karl Heinz Buchegger: "Re: More STL questions"
- Reply: Karl Heinz Buchegger: "Re: More STL questions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 01 Oct 2004 02:08:59 -0700
Karl Heinz Buchegger wrote:
> Mark P wrote:
>
> [snip]
>
>>set<T> mySet;
>>
>>for (i = 0; i < 10; i++)
>>{
>> /* read in some data[i] and do some analysis that will insert (and
>>create) and erase elements of mySet */
>>}
>>
>>My concern in this situation is that every time I execute the for loop
>>it's my understanding that, and I'm sure my terminology is wrong, but
>>the scope is "reset" and the automatic variables of the previous
>>iteration are lost. So isn't this a problem then, creating an element
>>within the loop, and expecting it to be safely available in mySet during
>>future iterations?
>
>
> What you are missing is this:
> When you ask an STL container to add something to it, the container will
> create a copy of what you passed to it. It is this copy that gets inserted
> into the container.
>
I get it now. I think. I actually wrote a little test program before I
saw your response that confirmed the above. main() looked like:
int main() {
set<MyInt> mySet;
for (int i = 0; i < 10; i++)
mySet.insert(MyInt(i));
/* do some other stuff with mySet */
}
Here MyInt is just a wrapper for an integer. For fun, I put a line in
the destructor to write to the screen when it was called. Sure enough,
the destructor was called 20 times. If I understand things right, the
first ten calls came one after each iteration of the for loop when the
argument to insert() was destroyed (presumable right after insert()
executed). The last ten calls came at the end of main when the copies
of MyInt managed by mySet were destroyed (along with mySet).
Is this all a fairly accurate description of reality?
Mark
- Next message: Karl Heinz Buchegger: "Re: More STL questions"
- Previous message: Spacen Jasset: "Re: insight on references vs. pointers"
- In reply to: Karl Heinz Buchegger: "Re: More STL questions"
- Next in thread: Karl Heinz Buchegger: "Re: More STL questions"
- Reply: Karl Heinz Buchegger: "Re: More STL questions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|