By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<int>
// each of which are empty initially

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

// Second Attempt
vector<deque<int> > _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<int> > _MyVar (N);
?
Thanks In Advance.
Best Regards,
Madhu.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
hrmadhu wrote:
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<int>
// each of which are empty initially

// First attempt
vector<deque<int> > _MyVariable( N, deque<int>());
You're not allowed (in standard C++) to use that name. At least get rid
of the underscore.

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

// some more stuff
return 0;
You don't need that return statement.
}
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<int> > _MyVar (N);
?
Yes, IMHO. That's the right way to do it, assuming you change the
variable name.

Good luck,
Jeff
Thanks In Advance.
Best Regards,
Madhu.


Jul 22 '05 #2

P: n/a
>Jeff Schwab <je******@comcast.net> wrote in message news:
// First attempt
vector<deque<int> > _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.

// Second Attempt
vector<deque<int> > _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 ?
}
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<int> > _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
Jul 22 '05 #3

P: n/a
hrmadhu wrote:
Jeff Schwab <je******@comcast.net> wrote in message news:
// First attempt
vector<deque<int> > _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<int> > _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<int> > _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<int> and push them back
into the vector one by one ?
No, but you can if you like.
Thanks again
Madhu


-Jeff

Jul 22 '05 #4

P: n/a
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 <je******@comcast.net> wrote in message news:<1K********************@comcast.com>...
hrmadhu wrote:
Jeff Schwab <je******@comcast.net> wrote in message news:

// First attempt
vector<deque<int> > _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<int> > _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<int> > _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<int> and push them back
into the vector one by one ?


No, but you can if you like.
Thanks again
Madhu


-Jeff

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.