Some simple STL doubts. 
Author Message
 Some simple STL doubts.

Hi all,
I am learning STL these days.
I have some doubts.Could someone please help me.
1. A class is defined as follows -
class U{
public:
unsigned long id;
U () : id (0) {}
U (unsigned long x) : id (x) {}

Quote:
};

In the main function -
vector<U> vector1, vector2(3);
Later in the program a stmt like this appears
assert ( vector2[0] == U () && vector2[1] == U() && vector2[2] == U ());
My question is that can a constructor be called explicitly like this. I
thought constructor is invoked only when an object is declared or through
new.
How does the above line work ?

2. Function objects.
How does a function object work?
E.g.
int mult (int x, int y) { return x*y;}

Main-
int x[5] = {2, 3, 4, 5, 6}
vector<int> vector1 (x, x+5);
int product = accumulate (vector1.begin (), vector1.end (), 1 , mult);
How does the mult function get the two parameters or what is the call
sequence?

Thanks in advance.



Sun, 16 Oct 2005 14:36:28 GMT  
 Some simple STL doubts.

Quote:

> I am learning STL these days.
> I have some doubts.Could someone please help me.
> 1. A class is defined as follows -
> class U{
> public:
> unsigned long id;
> U () : id (0) {}
> U (unsigned long x) : id (x) {}
> };

> In the main function -
> vector<U> vector1, vector2(3);
> Later in the program a stmt like this appears
> assert ( vector2[0] == U () && vector2[1] == U() && vector2[2] == U ());
> My question is that can a constructor be called explicitly like this. I
> thought constructor is invoked only when an object is declared or through
> new.
> How does the above line work ?

In each instance, the vector item on the left is compared against a
default-constructed U temporary.  The "U()" is not a call to the
constructor per se as it also creates an unnamed temporary.

Quote:
> 2. Function objects.
> How does a function object work?
> E.g.
> int mult (int x, int y) { return x*y;}

> Main-
> int x[5] = {2, 3, 4, 5, 6}
> vector<int> vector1 (x, x+5);
> int product = accumulate (vector1.begin (), vector1.end (), 1 , mult);
> How does the mult function get the two parameters or what is the call
> sequence?

If accumulate is specified to use a function that takes two arguments,
it will specify them in the call.  You don't have a function object
there, though.  This would be the function object version:
    struct mult
    {
        int operator()(int x, int y) { return x*y; }
    };

--
Craig Powers
MVP - Visual C++



Sun, 16 Oct 2005 21:55:53 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. microsoft visual studio - simple doubt

2. Doubt using Simple Object

3. Doubt While using Simple Object

4. simple doubt-dll

5. Simple doubt-Dll

6. Can u help me?I've a simple doubt in accessing data from VC++

7. simple C++ string doubt

8. Simple Doubt on Mouse Event Capture

9. Simple STL vs pointer question

10. VC++5.0 STL SIMPLE PROBLEM

11. Lots 'o warnings when compiling simple STL program

12. Simple STL memory leak

 

 
Powered by phpBB® Forum Software