C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help on the STL and initializing non-const references with t

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
hrmadhu
Guest





PostPosted: Fri Dec 26, 2003 11:24 pm    Post subject: Help on the STL and initializing non-const references with t Reply with quote



Hi,
I wish to declare a vector of deque of int, which I do as follows.

#include<vector>
#include<deque>
#include<iostream>
using namespace std;

int main(int argc, char* argv[])
{
int i=1;
int N=0;
while(i<=argc)
{
// parse arguments
// Most importantly extract the value of N
// as
N=atoi(argv[i]);
i++;
}
// some stuff

//declare a vector of deque of int
// vector contains N deque // each of which are empty initially

// First attempt
vector<deque _MyVariable( N, deque<int>());

// Second Attempt
vector<deque _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;
}


The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque _MyVar (N);
?
Thanks In Advance.
Best Regards,
Madhu.
Back to top
Jeff Schwab
Guest





PostPosted: Fri Dec 26, 2003 11:37 pm    Post subject: Re: Help on the STL and initializing non-const references wi Reply with quote



hrmadhu wrote:
Quote:
Hi,
I wish to declare a vector of deque of int, which I do as follows.

#include<vector
#include #include using namespace std;

int main(int argc, char* argv[])
{
int i=1;
int N=0;
while(i<=argc)
{
// parse arguments
// Most importantly extract the value of N
// as
N=atoi(argv[i]);
i++;
}
// some stuff

//declare a vector of deque of int
// vector contains N deque // each of which are empty initially

// First attempt
vector _MyVariable( N, deque<int>());

You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.

Quote:

// Second Attempt
vector<deque _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;

You don't need that return statement.

Quote:
}


The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque _MyVar (N);
?

Yes, IMHO. That's the right way to do it, assuming you change the
variable name.

Good luck,
Jeff

Quote:
Thanks In Advance.
Best Regards,
Madhu.


Back to top
hrmadhu
Guest





PostPosted: Sat Dec 27, 2003 5:13 pm    Post subject: Re: Help on the STL and initializing non-const references wi Reply with quote



Quote:
Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote in message news:
// First attempt
vector<deque _MyVariable( N, deque<int>());

You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.

Wow!! I didnt know that. As my coding style, I always prefix all
private and protected members of my classes with _ . Could you please
tell me why and where I can find more on this.
Quote:

// Second Attempt
vector<deque _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;

You don't need that return statement.

if I do not put this, then the compiler cribs that main, which is
declared int main(...) does not have a return value. Also, isnt that
the way I return whether the program executed with or without errors ?
return 0 suggests that the program ran successfully, while return -1
says there was an error ?

Quote:
}


The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque _MyVar (N);
?

Yes, IMHO. That's the right way to do it, assuming you change the
variable name.

if so, can I then simply use the variable as
_MyVar[i].push_back() ?
or do I need to create non-temporary deque<int> and push them back
into the vector one by one ?
Thanks again
Madhu

Back to top
Jeff Schwab
Guest





PostPosted: Sat Dec 27, 2003 7:16 pm    Post subject: Re: Help on the STL and initializing non-const references wi Reply with quote

hrmadhu wrote:
Quote:
Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote in message news:

// First attempt
vector<deque _MyVariable( N, deque<int>());

You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.


Wow!! I didnt know that. As my coding style, I always prefix all
private and protected members of my classes with _ . Could you please
tell me why and where I can find more on this.

TC++PL, p.81:

Names starting with an underscore are reserved for
special facilities in the implementation and the run-
time environment, so such names should not be used in
application programs.


Quote:
// Second Attempt
vector<deque _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;

You don't need that return statement.


if I do not put this, then the compiler cribs that main, which is
declared int main(...) does not have a return value.

Your compiler is non-standard. In standard C++, the default return
value of main is 0. You are correct to declare it returning an int.

Quote:
Also, isnt that
the way I return whether the program executed with or without errors ?
return 0 suggests that the program ran successfully, while return -1
says there was an error ?

That depends on your system. On Unix, you can return whatever you want,
as long as you document it. There is a sort of gentlemen's agreement
that 0 means success.

Quote:
}

The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque _MyVar (N);
?

Yes, IMHO. That's the right way to do it, assuming you change the
variable name.


if so, can I then simply use the variable as
_MyVar[i].push_back() ?

Assuming you change the variable name, and 0 <= i < N, yes.

Quote:
or do I need to create non-temporary deque into the vector one by one ?

No, but you can if you like.

Quote:
Thanks again
Madhu

-Jeff


Back to top
hrmadhu
Guest





PostPosted: Sun Dec 28, 2003 8:17 pm    Post subject: Re: Help on the STL and initializing non-const references wi Reply with quote

Thanks Jeff. Got it compiling without any error. (in fact it was also
running without error - but I just dislike compiler warnings!!)
Thanks again.
Best Regards,
Madhu.

Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote

Quote:
hrmadhu wrote:
Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote in message news:

// First attempt
vector<deque _MyVariable( N, deque<int>());

You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.


Wow!! I didnt know that. As my coding style, I always prefix all
private and protected members of my classes with _ . Could you please
tell me why and where I can find more on this.

TC++PL, p.81:

Names starting with an underscore are reserved for
special facilities in the implementation and the run-
time environment, so such names should not be used in
application programs.


// Second Attempt
vector<deque _MyVariable2 (N, deque<int>(0));

// some more stuff
return 0;

You don't need that return statement.


if I do not put this, then the compiler cribs that main, which is
declared int main(...) does not have a return value.

Your compiler is non-standard. In standard C++, the default return
value of main is 0. You are correct to declare it returning an int.

Also, isnt that
the way I return whether the program executed with or without errors ?
return 0 suggests that the program ran successfully, while return -1
says there was an error ?

That depends on your system. On Unix, you can return whatever you want,
as long as you document it. There is a sort of gentlemen's agreement
that 0 means success.

}

The compiler cribs

"Warning: should not initialize a non-const reference with a
temporary." in the STL code which is instantiated in the lines of
interest above.

Could someone please clarify the exact effects of initializing in the
above fashion ?
which of the above two attempts should I use ? Is it better not to
initialize the deque i.e. to use
vector<deque _MyVar (N);
?

Yes, IMHO. That's the right way to do it, assuming you change the
variable name.


if so, can I then simply use the variable as
_MyVar[i].push_back() ?

Assuming you change the variable name, and 0 <= i < N, yes.

or do I need to create non-temporary deque into the vector one by one ?

No, but you can if you like.

Thanks again
Madhu

-Jeff

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.