comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en
comp.lang.c++@googlegroups.com
Today's topics:
* Flummoxed - Please Help! - 9 messages, 5 authors
http://groups.google.com/group/comp.lang.c++/t/d4b5a6ac1e14e414?hl=en
* IO problem - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/1627ad3e27ed2570?hl=en
* about gets - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/0335e4afbf8afe82?hl=en
* pointer to a vector - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/b17f744707f37fdb?hl=en
* could an braced-init-list be a first-class expression? - 6 messages, 2
authors
http://groups.google.com/group/comp.lang.c++/t/4790566113c6b180?hl=en
* Iterator pair as a function argument? - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/d7a4099890dfa4ed?hl=en
* Nested Boost::unordered_map with std::pair insertion help .. - 2 messages, 2
authors
http://groups.google.com/group/comp.lang.c++/t/0864176555b94b56?hl=en
==============================================================================
TOPIC: Flummoxed - Please Help!
http://groups.google.com/group/comp.lang.c++/t/d4b5a6ac1e14e414?hl=en
==============================================================================
== 1 of 9 ==
Date: Tues, Feb 18 2014 5:06 pm
From: mrc2323@cox.net (Mike Copeland)
> > During execution, the following occurs during the IDE trace:
> > 1. The subprogram is entered and immediately jumps to the "return
> > builder" statement. (Why?)
> > 2. Then, the function is reentered and code steps are taken - until the
> > "if" statement executes. The logic works as expected with the data
It gets weirder and weirder...
First, I'm not producing (or testing) a Release version, and I have
no non-normal optimizations set. I'm running "vanilla" VS2010 Express,
afaics.
Second, there are other things going that I hadn't mentioned:
1. As I debug, some of the variables in the function don't show up, nor
can I see their values during execution by holding my mouse over them
while the code is being stepped through. (Highly unusual!.) The
variables that don't show are all the scalars (ints and chars), while
the non-scalars do show (and change as code executes).
2. I have tried moving the position of the scalar variables within the
routine, but no change there.
3. I moved the scalars outside of the routine (making them global
<gasp!>), and they show and change, as execution proceeds - as they
should.
4. Also, once these scalars are moved and debug normally, the code runs
through the logic provided (!). (I know that nothing really changes in
terms of the return string, but there's more logic to be added to do
some of that - I'm just working out the logic to handle the data and
expand it as needed.)
Thus, it appears that the problem of code that isn't executed within
the routine is some effect of the scalar data declared and used in the
code (totally odd, in my experience...). I'm at a complete loss to
understand this... 8<{{
Here's my latest code (which as I said is working as I step through
the debugger). <sigh>
#include <string>
#include <sstream>
using std::string;
using std::ostringstream;
int ii, dPos, nn;
char wc;
int ndigit;
int column;
string spellNumber(double value)
{
string digitStr;
ostringstream ossw;
static string builder;
ossw.str(""); // Convert integer portion of value to string
ossw << value, digitStr = ossw.str();
dPos = digitStr.find(".");
if(dPos != string::npos)
digitStr.erase(dPos);
nn = digitStr.length(); // Traverse characters in reverse order
wc = digitStr.back();
for(ii = digitStr.length()-1; ii >= 0; ii--)
{
ndigit = (int)(digitStr.at(ii)-'0');
column = (digitStr.length()-(ii+1));
} // for
// more logic to be added here...
return builder;
}
int main(int argc, char *argv[])
{
string str = spellNumber(123.45);
return EXIT_SUCCESS;
} // main
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
== 2 of 9 ==
Date: Tues, Feb 18 2014 5:27 pm
From: Ed Anson
On 2/18/14 8:06 PM, Mike Copeland wrote:
>>> During execution, the following occurs during the IDE trace:
>>> 1. The subprogram is entered and immediately jumps to the "return
>>> builder" statement. (Why?)
>>> 2. Then, the function is reentered and code steps are taken - until the
>>> "if" statement executes. The logic works as expected with the data
>
> It gets weirder and weirder...
> First, I'm not producing (or testing) a Release version, and I have
> no non-normal optimizations set. I'm running "vanilla" VS2010 Express,
> afaics.
> Second, there are other things going that I hadn't mentioned:
> 1. As I debug, some of the variables in the function don't show up, nor
> can I see their values during execution by holding my mouse over them
> while the code is being stepped through. (Highly unusual!.) The
> variables that don't show are all the scalars (ints and chars), while
> the non-scalars do show (and change as code executes).
> 2. I have tried moving the position of the scalar variables within the
> routine, but no change there.
> 3. I moved the scalars outside of the routine (making them global
> <gasp!>), and they show and change, as execution proceeds - as they
> should.
> 4. Also, once these scalars are moved and debug normally, the code runs
> through the logic provided (!). (I know that nothing really changes in
> terms of the return string, but there's more logic to be added to do
> some of that - I'm just working out the logic to handle the data and
> expand it as needed.)
> Thus, it appears that the problem of code that isn't executed within
> the routine is some effect of the scalar data declared and used in the
> code (totally odd, in my experience...). I'm at a complete loss to
> understand this... 8<{{
> Here's my latest code (which as I said is working as I step through
> the debugger). <sigh>
> #include <string>
> #include <sstream>
> using std::string;
> using std::ostringstream;
> int ii, dPos, nn;
> char wc;
> int ndigit;
> int column;
> string spellNumber(double value)
> {
> string digitStr;
> ostringstream ossw;
> static string builder;
> ossw.str(""); // Convert integer portion of value to string
> ossw << value, digitStr = ossw.str();
> dPos = digitStr.find(".");
> if(dPos != string::npos)
> digitStr.erase(dPos);
> nn = digitStr.length(); // Traverse characters in reverse order
> wc = digitStr.back();
> for(ii = digitStr.length()-1; ii >= 0; ii--)
> {
> ndigit = (int)(digitStr.at(ii)-'0');
> column = (digitStr.length()-(ii+1));
> } // for
> // more logic to be added here...
> return builder;
> }
>
> int main(int argc, char *argv[])
> {
> string str = spellNumber(123.45);
> return EXIT_SUCCESS;
> } // main
>
> ---
> This email is free from viruses and malware because avast! Antivirus protection is active.
> http://www.avast.com
>
Your new observations are just more proof that what others surmised must
be true: You are debugging an optimized version. What you are seeing is
precisely what an optimizer will do to your code.
== 3 of 9 ==
Date: Tues, Feb 18 2014 5:36 pm
From: Öö Tiib
On Wednesday, 19 February 2014 03:06:52 UTC+2, Mike Copeland wrote:
> First, I'm not producing (or testing) a Release version, and I have
> no non-normal optimizations set. I'm running "vanilla" VS2010 Express,
> afaics.
What you describe confirm what others said. Cut that "no non-normal" crap.
Remove every last of "normal" optimizations if you want to step thru and
see things. The compiler writers of Microsoft are far wiser than debugger
writers of Microsoft that are far wiser than IDE writers of Microsoft.
So if you want to see things of your program in IDE you *must* make
the compiler as dumb as you only can.
== 4 of 9 ==
Date: Tues, Feb 18 2014 6:07 pm
From: Geoff
On Tue, 18 Feb 2014 14:06:58 -0700, mrc2323@cox.net (Mike Copeland)
wrote:
>In article <le0eu5$gnl$1@dont-email.me>, v.bazarov@comcast.invalid
>says...
>> > I have the following (rather simple, I think) code which compiles but
>> > executes in a bizarre way: the code in the subprogram is skipped when
>> > called. 8<{{
>> > Here's the code and the call to it I use:
>> > Please advise... TIA
>>
>> The logic error is on the line 42 of your original program, at least
>> according to my crystal ball, which is the best source of information I
>> have at this point, since you've chosen not to post the entire program.
>>
>> http://www.parashift.com/c++-faq/posting-code.html
>
> Fair enough; I was sloppy. Here's the entire program code:
>// CPP10 Test Bed Program MRCopeland 02/18/2014
>#include "stdafx.h"
>string spellNumber(double value)
>{
> int ii, dPos, nn;
> char wc;
> string digits;
> ostringstream ossw;
> static string builder;
> ossw.str(""); // Convert integer portion of value to string
> ossw << value;
> digits = ossw.str();
> dPos = digits.find('.');
> if(dPos != string::npos) digits.erase(dPos);
> nn = digits.length(); // Traverse characters in reverse order
> wc = digits.back();
> for(ii = digits.length()-1; ii >= 0; ii--)
> {
> int ndigit = (int)(digits[ii]-'0');
> int column = (digits.length()-(ii+1));
> } // for
> return builder;
>}
>int main(int argc, char *argv[])
>{
> string str = spellNumber(123.45);
> return EXIT_SUCCESS;
>} // main
>
> During execution, the following occurs during the IDE trace:
> 1. The subprogram is entered and immediately jumps to the "return
>builder" statement. (Why?)
> 2. Then, the function is reentered and code steps are taken - until the
>"if" statement executes. The logic works as expected with the data
>presented ("digits" is runcated to "123"), whereupon the function exits
>(!).
> 3. The statements beyond the "if" are skipped for no apparent reason.
> Any thoughts?
The code as posted (and after making changes to make it compile due to
missing definitions) copies nothing into builder, the returned string
is empty.
== 5 of 9 ==
Date: Tues, Feb 18 2014 6:16 pm
From: Geoff
On Tue, 18 Feb 2014 18:06:52 -0700, mrc2323@cox.net (Mike Copeland)
wrote:
>> > During execution, the following occurs during the IDE trace:
>> > 1. The subprogram is entered and immediately jumps to the "return
>> > builder" statement. (Why?)
>> > 2. Then, the function is reentered and code steps are taken - until the
>> > "if" statement executes. The logic works as expected with the data
>
> It gets weirder and weirder...
> First, I'm not producing (or testing) a Release version, and I have
>no non-normal optimizations set. I'm running "vanilla" VS2010 Express,
>afaics.
> Second, there are other things going that I hadn't mentioned:
> 1. As I debug, some of the variables in the function don't show up, nor
>can I see their values during execution by holding my mouse over them
>while the code is being stepped through. (Highly unusual!.) The
>variables that don't show are all the scalars (ints and chars), while
>the non-scalars do show (and change as code executes).
Are you looking at the Autos window or the Locals window? This
behavior is normal for Autos. The variables disappear as they go out
of scope.
> 2. I have tried moving the position of the scalar variables within the
>routine, but no change there.
> 3. I moved the scalars outside of the routine (making them global
><gasp!>), and they show and change, as execution proceeds - as they
>should.
You are looking at Autos window.
> 4. Also, once these scalars are moved and debug normally, the code runs
>through the logic provided (!). (I know that nothing really changes in
>terms of the return string, but there's more logic to be added to do
>some of that - I'm just working out the logic to handle the data and
>expand it as needed.)
> Thus, it appears that the problem of code that isn't executed within
>the routine is some effect of the scalar data declared and used in the
>code (totally odd, in my experience...). I'm at a complete loss to
>understand this... 8<{{
> Here's my latest code (which as I said is working as I step through
>the debugger). <sigh>
>#include <string>
>#include <sstream>
>using std::string;
>using std::ostringstream;
> int ii, dPos, nn;
> char wc;
> int ndigit;
> int column;
>string spellNumber(double value)
>{
> string digitStr;
> ostringstream ossw;
> static string builder;
> ossw.str(""); // Convert integer portion of value to string
> ossw << value, digitStr = ossw.str();
> dPos = digitStr.find(".");
> if(dPos != string::npos)
> digitStr.erase(dPos);
> nn = digitStr.length(); // Traverse characters in reverse order
> wc = digitStr.back();
> for(ii = digitStr.length()-1; ii >= 0; ii--)
> {
> ndigit = (int)(digitStr.at(ii)-'0');
> column = (digitStr.length()-(ii+1));
> } // for
>// more logic to be added here...
> return builder;
>}
>
>int main(int argc, char *argv[])
>{
> string str = spellNumber(123.45);
> return EXIT_SUCCESS;
>} // main
>
All of this returns a blank string under debug and release builds. Do
you mean to return digits, not builder?
== 6 of 9 ==
Date: Tues, Feb 18 2014 6:28 pm
From: Geoff
On Tue, 18 Feb 2014 12:40:26 -0700, mrc2323@cox.net (Mike Copeland)
wrote:
> I have the following (rather simple, I think) code which compiles but
>executes in a bizarre way: the code in the subprogram is skipped when
>called. 8<{{
> Here's the code and the call to it I use:
>
What is this function supposed to do?
>string spellNumber(double value)
>{
> bool showThousands = false;
> bool allZeros = true;
> int ii, dPos, nn;
> char wc;
> string digits, temp;
> ostringstream ossw;
> static string builder;
> ossw.str("");
> ossw << value;
> digits = ossw.str();
> dPos = digits.find('.');
> if(dPos != string::npos) digits.erase(dPos);
Line above erases the decimal point and all after. Your string is now
"123". Why do you need to do anything else to this string?
> nn = digits.length();
> wc = digits.back();
OK, nn is 3, wc is '3'. Now what?
> for(ii = digits.length()-1; ii >= 0; ii--)
> {
> int ndigit = (int)(digits[ii]-'0');
> int column = (digits.length()-(ii+1));
> } // for
WTF was this done for? You could have easily just said ndigit = nn. I
don't know what you are going to do with the column variable but they
both go out of scope when the for loop is completed, ii is 0xffffffff
(-1?) which suggests something went wrong in the loop.
>// more code to be added here
> return builder;
>}
>...
> spellNumber(123.45); // call the function
>
> I have, of course the normal stdafx.h header and "use namespace
>std;", and this is code from a small test program I use to debug new
>functions and such. Many other programs and many thousands of lines of
>code work fine, but this code is weird...and I can't see what's wrong
>with it!
== 7 of 9 ==
Date: Wed, Feb 19 2014 6:46 am
From: scott@slp53.sl.home (Scott Lurndal)
mrc2323@cox.net (Mike Copeland) writes:
>In article <le0eu5$gnl$1@dont-email.me>, v.bazarov@comcast.invalid
>says...
>> > I have the following (rather simple, I think) code which compiles but
>> > executes in a bizarre way: the code in the subprogram is skipped when
>> > called. 8<{{
>> > Here's the code and the call to it I use:
>> > Please advise... TIA
>>
>> The logic error is on the line 42 of your original program, at least
>> according to my crystal ball, which is the best source of information I
>> have at this point, since you've chosen not to post the entire program.
>>
>> http://www.parashift.com/c++-faq/posting-code.html
>
> Fair enough; I was sloppy. Here's the entire program code:
>// CPP10 Test Bed Program MRCopeland 02/18/2014
>#include "stdafx.h"
>string spellNumber(double value)
>{
> int ii, dPos, nn;
> char wc;
> string digits;
> ostringstream ossw;
> static string builder;
> ossw.str(""); // Convert integer portion of value to string
> ossw << value;
> digits = ossw.str();
> dPos = digits.find('.');
> if(dPos != string::npos) digits.erase(dPos);
> nn = digits.length(); // Traverse characters in reverse order
> wc = digits.back();
> for(ii = digits.length()-1; ii >= 0; ii--)
> {
> int ndigit = (int)(digits[ii]-'0');
> int column = (digits.length()-(ii+1));
> } // for
> return builder;
>}
>int main(int argc, char *argv[])
>{
> string str = spellNumber(123.45);
> return EXIT_SUCCESS;
>} // main
>
> During execution, the following occurs during the IDE trace:
> 1. The subprogram is entered and immediately jumps to the "return
>builder" statement. (Why?)
Because all the intervening code is optimized out as useless.
== 8 of 9 ==
Date: Wed, Feb 19 2014 10:50 am
From: mrc2323@cox.net (Mike Copeland)
In article <k358g9he7j8er870k98fkah53kjr42m31r@4ax.com>,
geoff@invalid.invalid says...
> > I have the following (rather simple, I think) code which compiles but
> >executes in a bizarre way: the code in the subprogram is skipped when
> >called. 8<{{
> > Here's the code and the call to it I use:
> >
>
> What is this function supposed to do?
It's the start of a routine that will "spell a number": to produce
the sort of string data that's printed on a check. For example, 123
yields "One Hundred Twenty Three". The code that processes the
conversion isn't yet written...
>
> >string spellNumber(double value)
> >{
> > bool showThousands = false;
> > bool allZeros = true;
> > int ii, dPos, nn;
> > char wc;
> > string digits, temp;
> > ostringstream ossw;
> > static string builder;
> > ossw.str("");
> > ossw << value;
> > digits = ossw.str();
> > dPos = digits.find('.');
> > if(dPos != string::npos) digits.erase(dPos);
>
> Line above erases the decimal point and all after. Your string is now
> "123". Why do you need to do anything else to this string?
See above. I will process the cents part separately. Ultimately I
want to print checks with the output from this routine.
>
> > nn = digits.length();
> > wc = digits.back();
>
> OK, nn is 3, wc is '3'. Now what?
>
> > for(ii = digits.length()-1; ii >= 0; ii--)
> > {
> > int ndigit = (int)(digits[ii]-'0');
> > int column = (digits.length()-(ii+1));
> > } // for
>
> WTF was this done for? You could have easily just said ndigit = nn. I
> don't know what you are going to do with the column variable but they
> both go out of scope when the for loop is completed, ii is 0xffffffff
> (-1?) which suggests something went wrong in the loop.
>
Again, I'm testing only part of the code logic. 8<}}
> >// more code to be added here
> > return builder;
> >}
>
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
== 9 of 9 ==
Date: Wed, Feb 19 2014 1:03 pm
From: Geoff
On Wed, 19 Feb 2014 11:50:19 -0700, mrc2323@cox.net (Mike Copeland)
wrote:
>In article <k358g9he7j8er870k98fkah53kjr42m31r@4ax.com>,
>geoff@invalid.invalid says...
>> > I have the following (rather simple, I think) code which compiles but
>> >executes in a bizarre way: the code in the subprogram is skipped when
>> >called. 8<{{
>> > Here's the code and the call to it I use:
>> >
>>
>> What is this function supposed to do?
>
> It's the start of a routine that will "spell a number": to produce
>the sort of string data that's printed on a check. For example, 123
>yields "One Hundred Twenty Three". The code that processes the
>conversion isn't yet written...
You misunderstood the intent of my comments. They were intended to
tell you to properly document your code. State the purpose of the
function, THEN write the function.
>>
>> >string spellNumber(double value)
>> >{
>> > bool showThousands = false;
>> > bool allZeros = true;
>> > int ii, dPos, nn;
>> > char wc;
>> > string digits, temp;
>> > ostringstream ossw;
>> > static string builder;
>> > ossw.str("");
>> > ossw << value;
>> > digits = ossw.str();
>> > dPos = digits.find('.');
>> > if(dPos != string::npos) digits.erase(dPos);
>>
>> Line above erases the decimal point and all after. Your string is now
>> "123". Why do you need to do anything else to this string?
>
> See above. I will process the cents part separately. Ultimately I
>want to print checks with the output from this routine.
>>
>> > nn = digits.length();
>> > wc = digits.back();
>>
>> OK, nn is 3, wc is '3'. Now what?
>>
>> > for(ii = digits.length()-1; ii >= 0; ii--)
>> > {
>> > int ndigit = (int)(digits[ii]-'0');
>> > int column = (digits.length()-(ii+1));
>> > } // for
>>
>> WTF was this done for? You could have easily just said ndigit = nn. I
>> don't know what you are going to do with the column variable but they
>> both go out of scope when the for loop is completed, ii is 0xffffffff
>> (-1?) which suggests something went wrong in the loop.
>>
> Again, I'm testing only part of the code logic. 8<}}
>
... and doing a very bad job of it by inspection. Those variables in
the for() loop go out of scope and can't be referenced outside that
loop. The loop becomes a no-op and yields nothing of interest. You
never documented why you need ndigit or column. You can get ndigit by
writing ndigit = digits.length() and immediately writing "one-hundred"
or "two-hundred" based on ndigit == 3 alone, the for loop becomes
superfluous.
You never addressed the issues of the IDE mentioned in my other posts
in this thread. What you are seeing is to be expected given the code
you've fed it. Your variables are going out of scope as you step
through the function and you are looking for them in all the wrong
places. Change your style, use white space, learn scoping rules and
declare your variables accordingly.
string spellNumber(double value)
{
int ndigit, column;
string digits;
ostringstream ossw;
static string builder;
// Convert integer portion of value to string
ossw.str("");
ossw << value;
digits = ossw.str();
// truncate the string
int dPos = digits.find('.');
if(dPos != string::npos)
digits.erase(dPos);
// Traverse characters in reverse order
int nn = digits.length();
char wc = digits.back();
for(int ii = digits.length()-1; ii >= 0; ii--)
{
ndigit = (int)(digits[ii]-'0');
column = (digits.length()-(ii+1));
}
return digits;
}
==============================================================================
TOPIC: IO problem
http://groups.google.com/group/comp.lang.c++/t/1627ad3e27ed2570?hl=en
==============================================================================
== 1 of 3 ==
Date: Wed, Feb 19 2014 6:29 am
From: nvangogh
I have come to a question in C++ Primer (p 314 , exercise 8.1) that is
not clear to me.
"Write a function that takes and returns an istream&. The function
should read the stream until it hits end-of-file. The function should
print what it reads to the standard output. Reset the stream so that it
is valid before returning the stream."
Breaking this down, the function has to do three things:
1. Read a stream until it hits end-of-file
So the >> operator reads input from an istream object - cin.
This stream's end of file can be interrogated by
cin.eof(). This returns true if the end of file bit is set which can be
tested with a bool variable
bool on = false;
on = cin.eof();
if(on == true)
// end of file is reached, else
if(on ==false)
// keep reading cin
I don't believe that this is completely correct so can someone show me
how this code should be presented?
2. Print what is read to the standard output
I can only imagine this to be cout << ? But am lost from here
3. Reset the stream so it is valid before returning the stream
This section of the problem again defeats me.
Can anyone help with this function?
== 2 of 3 ==
Date: Wed, Feb 19 2014 6:41 am
From: Victor Bazarov
On 2/19/2014 9:29 AM, nvangogh wrote:
> I have come to a question in C++ Primer (p 314 , exercise 8.1) that is
> not clear to me.
> "Write a function that takes and returns an istream&. The function
> should read the stream until it hits end-of-file. The function should
> print what it reads to the standard output. Reset the stream so that it
> is valid before returning the stream."
>
> Breaking this down, the function has to do three things:
>
> 1. Read a stream until it hits end-of-file
> So the >> operator reads input from an istream object - cin.
> This stream's end of file can be interrogated by
> cin.eof(). This returns true if the end of file bit is set which can be
> tested with a bool variable
> bool on = false;
> on = cin.eof();
> if(on == true)
> // end of file is reached, else
> if(on ==false)
> // keep reading cin
>
> I don't believe that this is completely correct so can someone show me
> how this code should be presented?
There has to be something after the 'if' and its condition to designate
the action that shall be taken when the condition is met. So, instead
of the comments ("// end of file is reached") put something there so
that the program takes the action you want it to take. Same for the
other condition.
Usually, if you only have a "either-or" situation (like in your case,
it's either the end of file or not), then there is no need for the
second 'if', a simple 'else' ought to do. Something *like*
if (on)
...
else
...
(put the proper action instead of the ellipsis)
>
> 2. Print what is read to the standard output
> I can only imagine this to be cout << ? But am lost from here
Yes, that's pretty much what you need to do.
> 3. Reset the stream so it is valid before returning the stream
> This section of the problem again defeats me.
std::cin.clear();
resets the error condition on the 'std::cin' stream.
> Can anyone help with this function?
Part of the answer can also be found in the FAQ, section 5, question 2,
if memory serves.
V
--
I do not respond to top-posted replies, please don't ask
== 3 of 3 ==
Date: Wed, Feb 19 2014 10:09 am
From: Barry Schwarz
There are at least a couple of responses in comp.lang.c++.moderated.
Please don't multipost.
On Wed, 19 Feb 2014 14:29:18 +0000, nvangogh <nvangogh@pcexpert.net>
wrote:
>I have come to a question in C++ Primer (p 314 , exercise 8.1) that is
>not clear to me.
>"Write a function that takes and returns an istream&. The function
>should read the stream until it hits end-of-file. The function should
>print what it reads to the standard output. Reset the stream so that it
>is valid before returning the stream."
>
>Breaking this down, the function has to do three things:
>
>1. Read a stream until it hits end-of-file
>So the >> operator reads input from an istream object - cin.
>This stream's end of file can be interrogated by
>cin.eof(). This returns true if the end of file bit is set which can be
>tested with a bool variable
>bool on = false;
>on = cin.eof();
>if(on == true)
>// end of file is reached, else
>if(on ==false)
>// keep reading cin
>
>I don't believe that this is completely correct so can someone show me
>how this code should be presented?
>
>2. Print what is read to the standard output
>I can only imagine this to be cout << ? But am lost from here
>
>3. Reset the stream so it is valid before returning the stream
>This section of the problem again defeats me.
>
>Can anyone help with this function?
--
Remove del for email
==============================================================================
TOPIC: about gets
http://groups.google.com/group/comp.lang.c++/t/0335e4afbf8afe82?hl=en
==============================================================================
== 1 of 1 ==
Date: Wed, Feb 19 2014 10:32 am
From: Juha Nieminen
mary8shtr@gmail.com wrote:
> hi.I am writing a program.while i define some variable but program say this variable undefined.
Lucky you.
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
==============================================================================
TOPIC: pointer to a vector
http://groups.google.com/group/comp.lang.c++/t/b17f744707f37fdb?hl=en
==============================================================================
== 1 of 1 ==
Date: Wed, Feb 19 2014 10:34 am
From: Juha Nieminen
A <a@a.a> wrote:
> a) does the above *v needs to be deleted?
Did you write the 'new' keyword anywhere? If not, then why exactly do
you think you would need to write 'delete'?
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
==============================================================================
TOPIC: could an braced-init-list be a first-class expression?
http://groups.google.com/group/comp.lang.c++/t/4790566113c6b180?hl=en
==============================================================================
== 1 of 6 ==
Date: Wed, Feb 19 2014 10:47 am
From: pietro.cerutti@gmail.com
I'm wondering what would prevent an initialization list from being a first-class expression of type std::initializer_list.
I understand that the current standar allows braced-init-lists to appear only when assigning to a scalar of type T or to an object of a class taking an std::initializer_list in the assignment operator.
Having
void f (std::vector<double> v);
It is currently possible to do this:
auto a {1, 2, 3}:
g (a);
but not to do this:
g ({1, 2, 3});
As I see it, all information the compiler needs is there. Is there any specific reason why this isn't possible?
Thanks,
== 2 of 6 ==
Date: Wed, Feb 19 2014 10:48 am
From: pietro.cerutti@gmail.com
> g (a);
> g ({1, 2, 3});
of course I meant f(..);
== 3 of 6 ==
Date: Wed, Feb 19 2014 11:48 am
From: Paavo Helde
pietro.cerutti@gmail.com wrote in
news:a45ccd07-3838-4fda-b221-3a9deb589e70@googlegroups.com:
>
> void f (std::vector<double> v);
>
> It is currently possible to do this:
>
> auto a {1, 2, 3}:
> g (a);
>
> but not to do this:
>
> g ({1, 2, 3});
FWIW, gcc 4.8 (with -std=c++0x) compiles and runs this fine:
#include <vector>
void g(std::vector<double> v) {}
int main(int argc, char *argv[]) {
g({1.5, 2.0, 3.0});
}
== 4 of 6 ==
Date: Wed, Feb 19 2014 11:55 pm
From: Pietro Cerutti
Correct. This is what I get from posting trying to recall what the problem was. The actual issue is this. Why can't I construct a temporary initializer_list object with curly braces, so I can call this?
void h (const double d[3]);
h ({1., 2., 3.}.begin());
== 5 of 6 ==
Date: Thurs, Feb 20 2014 12:54 am
From: Paavo Helde
Pietro Cerutti <pietro.cerutti@gmail.com> wrote in
news:d8738a16-7951-43b1-befb-f1337390a040@googlegroups.com:
> Correct. This is what I get from posting trying to recall what the
> problem was. The actual issue is this. Why can't I construct a
> temporary initializer_list object with curly braces, so I can call
> this?
>
>
> void h (const double d[3]);
>
> h ({1., 2., 3.}.begin());
... whereas this compiles and runs:
auto x {1.};
typedef decltype(x) T;
h (T({1., 2., 3.}).begin());
Right... Something to do with grammar and that '}' can't be followed by
'.'?
== 6 of 6 ==
Date: Thurs, Feb 20 2014 1:10 am
From: Pietro Cerutti
On Thursday, February 20, 2014 9:54:18 AM UTC+1, Paavo Helde wrote:
> Pietro Cerutti <pietro.cerutti@gmail.com> wrote in
>
> news:d8738a16-7951-43b1-befb-f1337390a040@googlegroups.com:
>
>
>
> > Correct. This is what I get from posting trying to recall what the
>
> > problem was. The actual issue is this. Why can't I construct a
>
> > temporary initializer_list object with curly braces, so I can call
>
> > this?
>
> >
>
> >
>
> > void h (const double d[3]);
>
> >
>
> > h ({1., 2., 3.}.begin());
>
>
>
> ... whereas this compiles and runs:
>
>
>
> auto x {1.};
>
> typedef decltype(x) T;
>
> h (T({1., 2., 3.}).begin());
>
>
>
> Right... Something to do with grammar and that '}' can't be followed by
>
> '.'?
Yep my point exactly. The type info is there.
==============================================================================
TOPIC: Iterator pair as a function argument?
http://groups.google.com/group/comp.lang.c++/t/d7a4099890dfa4ed?hl=en
==============================================================================
== 1 of 4 ==
Date: Wed, Feb 19 2014 12:22 pm
From: nvangogh
Suppose I have a vector<int> values. Now the pair values.begin() and
values.end() will cover the range of the vector.
I am writing a simple function that takes the pair of iterators as
arguments along with an int value. The function then returns a bool to
indicate if the int value is found in the vector.
The first problem I have is to question if it is possible to pass
iterators as distinct arguments to a function? I would have thought that
the only way to accomplish this would be to pass the vector as a
reference, from there the function can use the iterators to do it's work.
I was thinking the correct definition would be
bool find_value (vector<int>&, int);
Or is there a way to pass an iterator pair as function arguments which I
have missed?
== 2 of 4 ==
Date: Wed, Feb 19 2014 12:23 pm
From: Ian Collins
nvangogh wrote:
> Suppose I have a vector<int> values. Now the pair values.begin() and
> values.end() will cover the range of the vector.
>
> I am writing a simple function that takes the pair of iterators as
> arguments along with an int value. The function then returns a bool to
> indicate if the int value is found in the vector.
>
> The first problem I have is to question if it is possible to pass
> iterators as distinct arguments to a function? I would have thought that
> the only way to accomplish this would be to pass the vector as a
> reference, from there the function can use the iterators to do it's work.
>
> I was thinking the correct definition would be
>
> bool find_value (vector<int>&, int);
>
> Or is there a way to pass an iterator pair as function arguments which I
> have missed?
std::find?
--
Ian Collins
== 3 of 4 ==
Date: Wed, Feb 19 2014 12:28 pm
From: Paavo Helde
nvangogh <nvangogh@pcexpert.net> wrote in news:cn8Nu.5082$1B3.554@fx26.am4:
> Suppose I have a vector<int> values. Now the pair values.begin() and
> values.end() will cover the range of the vector.
>
> I am writing a simple function that takes the pair of iterators as
> arguments along with an int value. The function then returns a bool to
> indicate if the int value is found in the vector.
>
> The first problem I have is to question if it is possible to pass
> iterators as distinct arguments to a function? I would have thought that
> the only way to accomplish this would be to pass the vector as a
> reference, from there the function can use the iterators to do it's work.
>
> I was thinking the correct definition would be
>
> bool find_value (vector<int>&, int);
>
> Or is there a way to pass an iterator pair as function arguments which I
> have missed?
There are plenty of STL functions like std::sort() which take iterators as
arguments, maybe you can take a look of their interfaces? BTW, your
function appears to be already implemented as std::find(), with iterator
arguments.
Cheers
Paavo
== 4 of 4 ==
Date: Wed, Feb 19 2014 1:03 pm
From: Jorgen Grahn
On Wed, 2014-02-19, Paavo Helde wrote:
> nvangogh <nvangogh@pcexpert.net> wrote in news:cn8Nu.5082$1B3.554@fx26.am4:
...
>> bool find_value (vector<int>&, int);
...
> There are plenty of STL functions like std::sort() which take iterators as
> arguments, maybe you can take a look of their interfaces? BTW, your
> function appears to be already implemented as std::find(), with iterator
> arguments.
Except not returning a bool. I might write a small wrapper
(untested):
template<class It, class Val>
bool contains(It a, It b, const Val& val) {
return std::find(a, b, val)!=b;
}
Sometimes the code becomes much more readable as
if(contains(a, b, 42) && !contains(b, c, 96)) ...
instead of
if(std::find(a, b, 42)!=b && std::find(b, c, 96)==c) ...
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
==============================================================================
TOPIC: Nested Boost::unordered_map with std::pair insertion help ..
http://groups.google.com/group/comp.lang.c++/t/0864176555b94b56?hl=en
==============================================================================
== 1 of 2 ==
Date: Thurs, Feb 20 2014 4:32 am
From: Rahul Mathur
All,
I have having Boost boost::unordered_map nested map calling boost::unordered_map which calls std::map finally.
The LHS (towards RHS) side key for first boost::unordered_map is 'int' type followed by next key being char buffer value of size 10, and key for std::map being again a char buffer value of size 10. So the boost::unordered_map has nested map internally.
I have to insert the char buffer of size 10 (value being either 'ORANGE' or ORD123 as test case) into nested std::pair as defined below. After inserting, I have to search or find the value from char buffer of size 10.
The code as compiled on Linux is -
----
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <boost/unordered_map.hpp>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
#if 0
#pragma pack(push, 2)
struct BookKeeping {
int ABCD;
char Apple_ID [10];
char Apple_NAME[10];
int Price;
int Number;
};
#pragma pack(pop)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment