comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en
comp.lang.c++@googlegroups.com
Today's topics:
* Tom Baker!!!!! - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/7c07034ed019f287?hl=en
* How can I pass-through an r-value reference in VC++ 2013? - 3 messages, 3
authors
http://groups.google.com/group/comp.lang.c++/t/64eab6103dee04a3?hl=en
* Is this C or C++? - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/e3c896a70fd187e8?hl=en
* Good way to write integer overflow checks? - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/1363de0ff88836cd?hl=en
* Comparing C++ Compilers on Windows - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/ec8f9203f4f792ed?hl=en
* vector - 5 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/19954b3ce53b2f9c?hl=en
* Replacement for _getch()? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/b3cd0d984e2ab19f?hl=en
* Printing concatenated string .. - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/b27a164a9845390f?hl=en
* 2d array element change - 5 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/7cca99fe241f98ce?hl=en
==============================================================================
TOPIC: Tom Baker!!!!!
http://groups.google.com/group/comp.lang.c++/t/7c07034ed019f287?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Nov 25 2013 11:08 am
From: scott@slp53.sl.home (Scott Lurndal)
woodbrian77@gmail.com writes:
>On Saturday, November 23, 2013 3:37:48 PM UTC-6, Leigh Johnston wrote:
>> That is all.
>
>
>I wish our "leaders" held the individual in high regard.
>If you go back far enough they used to.
Our "leaders" used to hold Tom Baker in high regard? I didn't
know they were whovians.
And, I urge you to actually learn about the religious beliefs of
the founding fathers - you'd be very suprised at what they actually
believed (as opposed to what the modern xians believe about them).
http://en.wikipedia.org/wiki/Christian_deism
==============================================================================
TOPIC: How can I pass-through an r-value reference in VC++ 2013?
http://groups.google.com/group/comp.lang.c++/t/64eab6103dee04a3?hl=en
==============================================================================
== 1 of 3 ==
Date: Mon, Nov 25 2013 8:46 pm
From: darylew@gmail.com
On Sunday, November 24, 2013 2:07:57 PM UTC-5, Öö Tiib wrote:
> On Sunday, 24 November 2013 19:48:20 UTC+2, dar...@gmail.com wrote:
> > I'm starting out on Visual Studio 2013 Express on my Windows 8.1 Pro laptop
> > (upgraded from Windows 8 Pro and Vista). My code:
>
> snip
>
> > The third overload gives me the following error:
>
> ...
>
> > 1>c:\users\daryle\documents\github\arraymd\include\boost\container\array_md.hpp(1868): error C2440: 'return' : cannot convert from 'char [6]' to 'char (&&)[6]'
>
> You seemingly attempt something for what 'boost::container::array_md'
> is not meant for. It is apparently not meant for providing rvalue
> references to its elements.
I am attempting something that is supposed to be possible. I know this
because...
> > I can't figure out how to adapt this. Help.
>
> May be write your own array that does support such strange thing then.
...I AM the writer of array_md! And I am trying to write an r-value overload
for get, just like the Standard library has. My feat works just fine with GCC
(4.7 and 4.8, MinGW versions). I just can't get it to work in MSVC. I know
it's possible because MS's own libraries have r-value overloads for get. So why
doesn't my version work?
== 2 of 3 ==
Date: Tues, Nov 26 2013 3:49 am
From: "Alf P. Steinbach"
On 26.11.2013 05:46, darylew@gmail.com wrote:
> On Sunday, November 24, 2013 2:07:57 PM UTC-5, �� Tiib wrote:
>> On Sunday, 24 November 2013 19:48:20 UTC+2, dar...@gmail.com wrote:
>>> I'm starting out on Visual Studio 2013 Express on my Windows 8.1 Pro laptop
>>> (upgraded from Windows 8 Pro and Vista). My code:
>>
>> snip
>>
>>> The third overload gives me the following error:
>>
>> ...
>>
>>> 1>c:\users\daryle\documents\github\arraymd\include\boost\container\array_md.hpp(1868): error C2440: 'return' : cannot convert from 'char [6]' to 'char (&&)[6]'
>>
>> You seemingly attempt something for what 'boost::container::array_md'
>> is not meant for. It is apparently not meant for providing rvalue
>> references to its elements.
>
> I am attempting something that is supposed to be possible. I know this
> because...
>
>>> I can't figure out how to adapt this. Help.
>>
>> May be write your own array that does support such strange thing then.
>
> ...I AM the writer of array_md! And I am trying to write an r-value overload
> for get, just like the Standard library has. My feat works just fine with GCC
> (4.7 and 4.8, MinGW versions). I just can't get it to work in MSVC. I know
> it's possible because MS's own libraries have r-value overloads for get. So why
> doesn't my version work?
>
Please provide a complete minimum example that exhibits the problem.
Something one can compile.
Cheers,
- Alf
== 3 of 3 ==
Date: Tues, Nov 26 2013 4:54 am
From: Öö Tiib
On Tuesday, 26 November 2013 06:46:11 UTC+2, dar...@gmail.com wrote:
> On Sunday, November 24, 2013 2:07:57 PM UTC-5, Öö Tiib wrote:
> > On Sunday, 24 November 2013 19:48:20 UTC+2, dar...@gmail.com wrote:
> > > I'm starting out on Visual Studio 2013 Express on my Windows 8.1 Pro laptop
> > > (upgraded from Windows 8 Pro and Vista). My code:
> >
> > snip
> >
> > > The third overload gives me the following error:
> >
> > ...
> >
> > > 1>c:\users\daryle\documents\github\arraymd\include\boost\container\array_md.hpp(1868): error C2440: 'return' : cannot convert from 'char [6]' to 'char (&&)[6]'
> >
> > You seemingly attempt something for what 'boost::container::array_md'
> > is not meant for. It is apparently not meant for providing rvalue
> > references to its elements.
>
> I am attempting something that is supposed to be possible. I know this
> because...
Yes, you already said you know it because 'std::get' works with 'std::array' and 'std::tuple'.
> > > I can't figure out how to adapt this. Help.
> >
> > May be write your own array that does support such strange thing then.
>
> ...I AM the writer of array_md! And I am trying to write an r-value overload
> for get, just like the Standard library has. My feat works just fine with GCC
> (4.7 and 4.8, MinGW versions). I just can't get it to work in MSVC. I know
> it's possible because MS's own libraries have r-value overloads for get. So why
> doesn't my version work?
Yes you have posted that the error is at line 1868 of array_md.hpp
that you maintain. So now write some own array that has same difficulty
exposed. It is likely that we discover a defect in MSVC of VS 2013 or that
we discover defect in your code or both or neither. Very interesting on all
the cases but ...
right now we can't help since we do not have enough information. :(
==============================================================================
TOPIC: Is this C or C++?
http://groups.google.com/group/comp.lang.c++/t/e3c896a70fd187e8?hl=en
==============================================================================
== 1 of 4 ==
Date: Tues, Nov 26 2013 10:55 am
From: Jorgen Grahn
On Sun, 2013-11-24, �� Tiib wrote:
> On Saturday, 23 November 2013 11:42:17 UTC+2, Jorgen Grahn wrote:
>> On Fri, 2013-11-22, �� Tiib wrote:
>> > When you are reading-writing tens or hundreds of items (typical case) then
>> > you use the streams with some JSON or XML parser or the like. Speed of
>> > implementing it matters and more than two hours is clearly wasteful.
>>
>> Minor complaint: depends on what area you work in. I tend to write my
>> text data formats from scratch, especially when I want them to be
>> conveniently editable or when I want them to double as a commandline
>> (i.e. people will sit and type interactively).
>
> JSON is pretty conveniently editable.
(Checks Wikipedia). Yes, it seems reasonably ok (much better than
XML, or ASN.1 BER, or ...) but I normally don't need the burden of a
language with structs. A series of "name=value" lines is almost always
enough. Or sometimes "command argument ...".
> Just like with JSON or XML there
> are some stock parsers for other text formats CSV, INI or command
> line switches. I have worked in (bit too) several areas over the years.
> I thought that command line UIs are now mostly used for (unit)
> testing and scripting; typically they want some gesture-based graphical
> UIs. What area it is that relies heavily on command line UIs?
Traditional Unix, from sysadmin tasks and data analysis to networking
protocols. I almost always want to be able to automate things, or
parse data with simple Perl-one liners, and/or feed it to gnuplot ...
I guess that falls under "scripting" above.
My views are similar to Eric Raymond's here:
http://www.catb.org/esr/writings/taoup/html/textualitychapter.html
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
== 2 of 4 ==
Date: Tues, Nov 26 2013 11:44 am
From: "Alf P. Steinbach"
On 26.11.2013 19:55, Jorgen Grahn wrote:
> On Sun, 2013-11-24, �� Tiib wrote:
>>
>> JSON is pretty conveniently editable.
>
> (Checks Wikipedia). Yes, it seems reasonably ok (much better than
> XML, or ASN.1 BER, or ...) but I normally don't need the burden of a
> language with structs. A series of "name=value" lines is almost always
> enough. Or sometimes "command argument ...".
Well this is getting pretty off-topic, but as opposed to name=value
lines JSON can represent just about any hierarchical data. I.e. it's as
powerful as you get. And like name=value lines it's easy enough to write
and to check manually, so IMHO it's a good candidate for
... one logic to parse them all,
e.g. look at this example:
(http://stackoverflow.com/questions/12394472/serializing-and-deserializing-json-with-boost).
The promise of XML was once that we would be able to apply all the
existing SGML tools and machinery, which advantage would more than
compensate for the complexity. What? Haven't seen any of that? Well
neither have I. But then I haven't really delved into JSON either, so
maybe there's something smelly-for-poking-nose also in there somewhere?
Cheers,
- Alf (wondering, could it be "peeking nose", or is that just eyes?)
== 3 of 4 ==
Date: Tues, Nov 26 2013 12:19 pm
From: "J. Clarke"
In article <slrnl99rkk.hj3.grahn+nntp@frailea.sa.invalid>,
grahn+nntp@snipabacken.se says...
>
> On Sun, 2013-11-24, �� Tiib wrote:
> > On Saturday, 23 November 2013 11:42:17 UTC+2, Jorgen Grahn wrote:
> >> On Fri, 2013-11-22, �?�? Tiib wrote:
> >> > When you are reading-writing tens or hundreds of items (typical case) then
> >> > you use the streams with some JSON or XML parser or the like. Speed of
> >> > implementing it matters and more than two hours is clearly wasteful.
> >>
> >> Minor complaint: depends on what area you work in. I tend to write my
> >> text data formats from scratch, especially when I want them to be
> >> conveniently editable or when I want them to double as a commandline
> >> (i.e. people will sit and type interactively).
> >
> > JSON is pretty conveniently editable.
>
> (Checks Wikipedia). Yes, it seems reasonably ok (much better than
> XML, or ASN.1 BER, or ...) but I normally don't need the burden of a
> language with structs. A series of "name=value" lines is almost always
> enough. Or sometimes "command argument ...".
>
> > Just like with JSON or XML there
> > are some stock parsers for other text formats CSV, INI or command
> > line switches. I have worked in (bit too) several areas over the years.
> > I thought that command line UIs are now mostly used for (unit)
> > testing and scripting; typically they want some gesture-based graphical
> > UIs. What area it is that relies heavily on command line UIs?
>
> Traditional Unix, from sysadmin tasks and data analysis to networking
> protocols. I almost always want to be able to automate things, or
> parse data with simple Perl-one liners, and/or feed it to gnuplot ...
> I guess that falls under "scripting" above.
Just an aside, but Microsoft is also moving back in the direction of a
command line UI for server and network administration. The recommended
install for Server 2012 is "server core" which does not include the GUI
and is to be administered through the Powershell, and much of the
current go-around of MCSE training is how to run things using
Powershell.
> My views are similar to Eric Raymond's here:
> http://www.catb.org/esr/writings/taoup/html/textualitychapter.html
>
> /Jorgen
== 4 of 4 ==
Date: Tues, Nov 26 2013 1:50 pm
From: Ian Collins
Alf P. Steinbach wrote:
> On 26.11.2013 19:55, Jorgen Grahn wrote:
>> On Sun, 2013-11-24, �� Tiib wrote:
>>>
>>> JSON is pretty conveniently editable.
>>
>> (Checks Wikipedia). Yes, it seems reasonably ok (much better than
>> XML, or ASN.1 BER, or ...) but I normally don't need the burden of a
>> language with structs. A series of "name=value" lines is almost always
>> enough. Or sometimes "command argument ...".
>
> Well this is getting pretty off-topic, but as opposed to name=value
> lines JSON can represent just about any hierarchical data. I.e. it's as
> powerful as you get. And like name=value lines it's easy enough to write
> and to check manually, so IMHO it's a good candidate for
>
> ... one logic to parse them all,
:)
The OS I spend most of my time developing for (SmartOS) uses JSON (and
node.js) extensively. It makes integration with web tools very easy.
> The promise of XML was once that we would be able to apply all the
> existing SGML tools and machinery, which advantage would more than
> compensate for the complexity. What? Haven't seen any of that? Well
> neither have I. But then I haven't really delved into JSON either, so
> maybe there's something smelly-for-poking-nose also in there somewhere?
I get the impression (from the number of tools written in it) Java
embraced XML more enthusiastically.
XML still has its place (where it began, in document markup). It
certainly makes working with (Open)Office documents a lot easier than
the older binary formats. My favorite XML editor is LibreOffice.
My rule of thumb is if it's going to published or used as project
documentation, use XML. If not, use JSON.
--
Ian Collins
==============================================================================
TOPIC: Good way to write integer overflow checks?
http://groups.google.com/group/comp.lang.c++/t/1363de0ff88836cd?hl=en
==============================================================================
== 1 of 3 ==
Date: Tues, Nov 26 2013 3:13 pm
From: Gerhard Fiedler
David Brown wrote:
> On 10/11/13 07:15, Alf P. Steinbach wrote:
>> However, with the C++ standard as it is (as of C++11) this code is not
>> formally portable, and worse, it /should not be/ practically portable to
>> environments where "-ftrapv" is used with the g++ compiler.
>
> The code is portable. It is undefined behaviour on all platforms.
> (With some compilers, and some flag options, it happens to give a
> particular type of behaviour - but /any/ behaviour is consistent with
> /undefined/ behaviour.) As it stands, the code asks for nasal daemons -
> it might happen to give a check for signed overflow in some
> circumstances, but that's just luck.
>
>> So, I tried
>> your code with MinGW g++ 4.7.2, and the code appears to detect the
>> overflow even with the "-ftrapv" option specified, where the overflow
>> should be trapped. Apparently that is a COMPILER BUG. :-(
>>
>
> In the great majority of cases where people think they have found a
> "COMPILER BUG", they are wrong. This is one of them. It is, IMHO, a
> documentation "bug" - the gcc manual could be much clearer here.
>
> The "-ftrapv" option does not stop signed overflow from being undefined
> behaviour. This means that you are not guaranteed traps on overflows -
> but you /might/ get them. It is dependent on many things, including the
> types of optimisation, the target, the knowledge the compiler has (i.e.,
> does it /know/ there is always an overflow, or never an overflow), etc.
FWIW (not much, but still), and no disagreement, my experience is that
-ftrapv works with the biggest size integer that is natively supported
(i.e. 32 bit ints on x86, 64 bit ints on x86-64).
Which sort of makes sense.
Gerhard
== 2 of 3 ==
Date: Thurs, Nov 28 2013 7:12 am
From: "Alf P. Steinbach"
On 17.11.2013 22:00, Gennaro Prota wrote:
> On Sun, 17 Nov 2013 15:24:07 +0100, Alf P. Steinbach wrote:
>> On 17.11.2013 14:49, Gennaro Prota wrote:
>>>
>>> I employed some min-max macros protection (the parentheses, and the
>>> weird names
>>> "mn" and "mx"). Note that you might want to use the ?: operator.
>>
>> Oh! You're thinking of MACRO interference. Well I say, those who let
>> macros called "min" and "max" exist, deserve whatever they get! :D
>
> It isn't an easy problem. One of the main offenders in this case
> is <windows.h>. I've never found a reassurance in Microsoft
> documentation that their min/max macros are not used in their
> own functions. This means that using NOMINMAX in your code and
> linking to something else that doesn't use it could potentially
> lead to ODR violations.
Indeed.
Some years ago (I think less than a decade) I posted a min/max-fixed
version of Microsoft's <gdiplus.h>.
Checking, it still uses the min/max macros from <windows.h>, as of
Visual C++ 2013 (version 12.0):
<code>
#define NOMINMAX
#include <Windows.h>
#include <gdiplus.h>
auto main() -> int
{
MessageBox( 0, L"Hi", L"Info:", MB_SETFOREGROUND );
}
</code>
<compilation result>
1>c:\program files (x86)\windows
kits\8.1\include\um\gdiplustypes.h(500): error C3861: 'max': identifier
not found
1>c:\program files (x86)\windows kits\8.1\include
... etc.
</compilation result>
<gdiplus.h> is mainly a header-only C++ module that wraps the separately
compiled GDI+ C API, i.e. a C++ language binding to that API, Microsoft
style. Happily most of the code is in a C++ namespace. So my old fix was
to add "using std::min" and "using std::max" in that namespace, as well
as a common include of <algorithm>, but this entailed fixing a lot of
sub-headers -- I don't recall exactly why now, but it was a lot of
editing, not a simple quick-fix.
> (I think they do not use them; but that's not a certainty.)
See above. :(
> Up until a few weeks ago, I've been avoiding the names "min" and
> "max" in my own code, exactly to avoid problems. Then, I wrote a
> little class intended to be a model of standard uniform random
> number generator; and that *requires* those names.
>
>
> To protect those names (including if you use
> numeric_limits<>::min/max), you have two main alternatives:
> either you parenthesize or you use a dummy macro like this:
>
> #define NO_MACRO_EXPANSION
> int x = std::numeric_limits< int >::max NO_MACRO_EXPANSION () ;
>
> It prevents expanding any function-like macro named "max" for
> the same reason that a ')' does: the macro name isn't followed
> by an open parenthesis.
Well I think neither alternative is open for 3rd party code that one
includes.
So I think that the only reasonable way to do things is to define
NOMINAX and make sure to include <windows.h> oneself before including
any other headers.
That, by the way, is also necessary to avoid inconsistencies with
respect to supported OS version etc. (what declarations are included,
and even the form of the declarations).
Cheers,
- Alf
== 3 of 3 ==
Date: Thurs, Nov 28 2013 7:19 am
From: "Alf P. Steinbach"
On 28.11.2013 16:12, Alf P. Steinbach wrote:
>
> Some years ago (I think less than a decade) I posted a min/max-fixed
> version of Microsoft's <gdiplus.h>.
Found it, it was in 2009:
https://groups.google.com/d/msg/comp.os.ms-windows.programmer.win32/SNcB-ZAbBa8/MO-VcYqc-54J
Cheers,
- Alf
==============================================================================
TOPIC: Comparing C++ Compilers on Windows
http://groups.google.com/group/comp.lang.c++/t/ec8f9203f4f792ed?hl=en
==============================================================================
== 1 of 2 ==
Date: Wed, Nov 27 2013 10:12 am
From: Lynn McGuire
Comparing C++ Compilers on Windows:
http://slashdot.org/topic/cloud/speed-test-2-comparing-c-compilers-on-windows/
"In all cases I�m using the 64-bit versions of the
compilers. But let�s be clear about something: When
I say 64-bit compiler, I�m talking about the generated
code. The compiler itself may or may not be a 64-bit
application."
Bummer, no comparison tables or charts.
Lynn
== 2 of 2 ==
Date: Thurs, Nov 28 2013 10:00 am
From: Jorgen Grahn
On Wed, 2013-11-27, Lynn McGuire wrote:
> Comparing C++ Compilers on Windows:
> http://slashdot.org/topic/cloud/speed-test-2-comparing-c-compilers-on-windows/
He's measuring compilation speed. IMO the best way to speed up
compilation times (or save hardware resources) is to fix your build
system so you're not building the same thing, over and over again.
(I'm writing this as I wait for our super-slow compilation to finish.
I don't trust our build system, so I did a 'make clean' ...)
Using Git (or other modern version control tools?) helps here. It does
the Right Thing with file timestamps, so make works as intended.
Surprisingly useful.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
==============================================================================
TOPIC: vector
http://groups.google.com/group/comp.lang.c++/t/19954b3ce53b2f9c?hl=en
==============================================================================
== 1 of 5 ==
Date: Thurs, Nov 28 2013 1:59 am
From: Rosario1903
why the below goes to seg fault?
#include <iostream>
#include <vector>
using namespace std;
void f(vector<unsigned>& v)
{v[3]=4; v[4]=9;}
int main(void)
{vector <unsigned> mv;
mv[0]=1; mv[1]=2;
f(mv);
cout<<"mv[0]="<<mv[0]<<", mv[1]="<<mv[1]<<"\n";
cout<<"mv[3]="<<mv[3]<<", mv[4]="<<mv[4]<<" mv[5]="<<mv[5]<<"\n";
return 0;
}
== 2 of 5 ==
Date: Thurs, Nov 28 2013 2:18 am
From: guinness.tony@gmail.com
On Thursday, 28 November 2013 09:59:07 UTC, Rosario1903 wrote:
> why the below goes to seg fault?
>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> void f(vector<unsigned>& v)
> {v[3]=4; v[4]=9;}
>
>
> int main(void)
> {vector <unsigned> mv;
vector<unsigned> mv(6); // or more
>
> mv[0]=1; mv[1]=2;
> f(mv);
> cout<<"mv[0]="<<mv[0]<<", mv[1]="<<mv[1]<<"\n";
> cout<<"mv[3]="<<mv[3]<<", mv[4]="<<mv[4]<<" mv[5]="<<mv[5]<<"\n";
>
> return 0;
> }
When you declare a vector, with no construction arguments, it contains
exactly zero elements. Elements mv[0], mv[1], etc. do not yet exist.
This is why you get a seg-fault when trying to access them.
You can (as in my example, above) pre-allocate a number of
default-initialised elements. Or you can "manually" change the
size of the vector (using its resize() member function) or use
any of the insert()/push_back() member functions to add elements
to the vector.
== 3 of 5 ==
Date: Thurs, Nov 28 2013 8:27 am
From: "K. Frank"
Hello Rosario!
On Thursday, November 28, 2013 4:59:07 AM UTC-5, Rosario1903 wrote:
> why the below goes to seg fault?
> ...
> void f(vector<unsigned>& v)
> {v[3]=4; v[4]=9;}
> ...
> {vector <unsigned> mv;
> mv[0]=1; mv[1]=2;
> f(mv);
As guinne explained, vector mv starts off with no
elements, so you're accessing non-existent elements,
which can lead to a seg fault.
As a side note, for additional safety, you can access
elements using the "at" member function:
v.at(3)=4; v.at(4)=9;
"At" throws a std::out_of_range exception if the vector
doesn't contain the index being accessed. (For maximum
efficiency, "[]" doesn't perform this check.)
Good luck!
K. Frank
== 4 of 5 ==
Date: Thurs, Nov 28 2013 10:12 am
From: Rosario1903
On Thu, 28 Nov 2013 08:27:00 -0800 (PST), "K. Frank" wrote:
>Hello Rosario!
>
>On Thursday, November 28, 2013 4:59:07 AM UTC-5, Rosario1903 wrote:
>> why the below goes to seg fault?
>> ...
>> void f(vector<unsigned>& v)
>> {v[3]=4; v[4]=9;}
>> ...
>> {vector <unsigned> mv;
>> mv[0]=1; mv[1]=2;
>> f(mv);
>
>As guinne explained, vector mv starts off with no
>elements, so you're accessing non-existent elements,
>which can lead to a seg fault.
yes i thought that index allocate mem...
>As a side note, for additional safety, you can access
>elements using the "at" member function:
>
> v.at(3)=4; v.at(4)=9;
thank you
>"At" throws a std::out_of_range exception if the vector
>doesn't contain the index being accessed. (For maximum
>efficiency, "[]" doesn't perform this check.)
>
>
>Good luck!
>K. Frank
== 5 of 5 ==
Date: Thurs, Nov 28 2013 10:18 am
From: Rosario1903
On Thu, 28 Nov 2013 02:18:22 -0800 (PST), guinness.tony wrote:
ok thanks
==============================================================================
TOPIC: Replacement for _getch()?
http://groups.google.com/group/comp.lang.c++/t/b3cd0d984e2ab19f?hl=en
==============================================================================
== 1 of 2 ==
Date: Fri, Nov 29 2013 11:39 am
From: Nobody
On Sun, 24 Nov 2013 23:28:18 +0100, Alf P. Steinbach wrote:
>> One of the main reasons why C++ doesn't standardise getch() is because
>> it's a DOS-ism, i.e. something which only makes sense on a "PC",
>
> With block mode terminals I guess it won't make much sense. But then e.g.
> iostreams don't make much sense for embedded programming either. And I've
> used similar functions in many programming languages on a variety of
> systems, including *nix, with a variety of terminals, since the early
> 1980s, so it's not a function that's specific to PCs.
The functionality may not be specific to PCs, but the getch() API is. On
Unix (or other systems whose model of interactive input is based upon
dedicated terminals connected to serial ports), "read character without
echo" cannot be simplified to a single getch() function without being
oversimplified.
Configuring the terminal is a specific operation, as are reading
characters, restoring the terminal to its original state, and ensuring
that the terminal is restored upon suspend and reconfigured upon resume.
And iostreams are perfectly useful for embedded programming. "iostream"
doesn't automatically mean "fstream" (and plenty of embedded platforms
have filesystems).
== 2 of 2 ==
Date: Fri, Nov 29 2013 11:53 am
From: "Alf P. Steinbach"
On 29.11.2013 20:39, Nobody wrote:
> On Sun, 24 Nov 2013 23:28:18 +0100, Alf P. Steinbach wrote:
>
>>> One of the main reasons why C++ doesn't standardise getch() is because
>>> it's a DOS-ism, i.e. something which only makes sense on a "PC",
>>
>> With block mode terminals I guess it won't make much sense. But then e.g.
>> iostreams don't make much sense for embedded programming either. And I've
>> used similar functions in many programming languages on a variety of
>> systems, including *nix, with a variety of terminals, since the early
>> 1980s, so it's not a function that's specific to PCs.
>
> The functionality may not be specific to PCs, but the getch() API is. On
> Unix (or other systems whose model of interactive input is based upon
> dedicated terminals connected to serial ports), "read character without
> echo" cannot be simplified to a single getch() function without being
> oversimplified.
So?
Why would anyone would want just a single function?
> Configuring the terminal is a specific operation, as are reading
> characters, restoring the terminal to its original state, and ensuring
> that the terminal is restored upon suspend and reconfigured upon resume.
That's what C++ constructors and destructors help to automate:
initialization and cleanup. Yay! :-)
Anyway, I suggest you try the "curses" library (in one of its
incarnations), even if it's C language; IIRC it does much of this for you.
http://linux.die.net/man/3/getch
> And iostreams are perfectly useful for embedded programming. "iostream"
> doesn't automatically mean "fstream" (and plenty of embedded platforms
> have filesystems).
So in your opinion I should have written "fstream", not "iostreams", yes?
Cheers, & have a nice weekend!,
- Alf
==============================================================================
TOPIC: Printing concatenated string ..
http://groups.google.com/group/comp.lang.c++/t/b27a164a9845390f?hl=en
==============================================================================
== 1 of 1 ==
Date: Sat, Nov 30 2013 1:31 pm
From: "Alf P. Steinbach"
[follow-up set to comp.lang.c++]
On 14.11.2013 01:20, Stefan Ram wrote:
> Barry Schwarz <schwarzb@dqel.com> writes:
>> This is the standard push-pop idiom for temporarily changing any state
>> variable and then restoring it.
>
> One can use RAII with a custom class �with� like:
>
> { with flags{ ::std::hex }; /* use ::std::hex here */ }
> /* flags are restored here */
There may be some problem with the following code, because for some
reason it lingered in an old repository, not moved over to current.
But anyway, people may just find it useful to sort of emulate C# "using"
or Python "with" or the new-fangled Java variant of its "try":
[code]
/*
CPPX_WITH( std::mutex() )
{
// Blah blah...
}
*/
#define CPPX_WITH( initializer ) \
if( bool cppx_with_finished = false ) {} \
else for( \
auto const& cppx_with_object = initializer; \
((void) cppx_with_object, !cppx_with_finished); \
cppx_with_finished = true )
[/code]
Explanation: the "if" is a limited means to introduce an extra local
variable, that is used to control the "for" loop which is a more general
means to introduce a local variable. The "(void)" cast makes that latter
variable appear to be used, so as to avoid silly-warnings about it. So
essentially, CPPX_WITH creates a local scope where an "anonymous"
variable is declared with the specified initializer expression, and the
whole point is usually to express more clearly that some code depends on
being executed between that variable's initialization and cleanup.
To make the macro more accurately reflect the intent of an anonymous
variable one could perhaps generate a less easy to use name via
__LINE__, but of old at least one compiler (Visual C++) messed that up
when some special option (Zi?) was used, so that that compiler had to be
detected and treated specially -- as I discovered when I first tried
out the original scope guard implementation.
Cheers & hth.,
- Alf
==============================================================================
TOPIC: 2d array element change
http://groups.google.com/group/comp.lang.c++/t/7cca99fe241f98ce?hl=en
==============================================================================
== 1 of 5 ==
Date: Sat, Nov 30 2013 5:27 pm
From: axcytz@gmail.com
I have 2d array (10x12), and they have 0 and 1s in them. I want to count the 1s in each array and I have two cases here: (1) if an array has more than three 1s, say k many, then generate k - 3 random variables and replace that many 1s with 0s if that random variable corresponds to a 1. (2)if an array has less than three 1s, say q many, then generate 3 - q random variables and replace that many 0s with 1s if that random variable corresponds to a 0. I have the following code so far. I need some suggestions here, I could not construct the code well.
int y[10];
int r[10];
for(int i=0; i < 10; i++){
for(int j=0; j < 12; j++)
{
y[i] += Array[i][j];
}
if(y[i] > 3)
{
f = y[i] - 3;
if(Array[i][r[f]] ==1)
Array[i][r[f]] = 0;
}
if(y[i] < 3)
{
f = 3 - y[i];
if(Array[i][r[f]] ==0)
Array[i][r[f]] = 1;
}
}
Thanks
== 2 of 5 ==
Date: Sat, Nov 30 2013 6:01 pm
From: "Alf P. Steinbach"
On 01.12.2013 02:27, axcytz@gmail.com wrote:
> I have 2d array (10x12), and they have 0 and 1s in them. I want to count
> the 1s in each array and I have two cases here: (1) if an array has more
> than three 1s, say k many, then generate k - 3 random variables and replace
> that many 1s with 0s if that random variable corresponds to a 1. (2)if an
> array has less than three 1s, say q many, then generate 3 - q random
> variables and replace that many 0s with 1s if that random variable
> corresponds to a 0. I have the following code so far. I need some suggestions
> here, I could not construct the code well.
Is this a fair restatement:
* You have 10 arrays, each of 12 items, each item is either 0 or 1.
* In each array:
- Let n = count of 1 values in the array.
- If n < 3, then randomly change 0s into 1s until n = 3.
- If n > 3, then randomly change 1s into 0s until n = 3.
?
> int y[10];
> int r[10];
>
> for(int i=0; i < 10; i++){
> for(int j=0; j < 12; j++)
> {
> y[i] += Array[i][j];
> }
You are not guaranteed that `y` is all zeroes, since `y` is a non-static
variable.
To guarantee all zeros you need to explicitly /zero-initialize/:
int y[10] = {};
But if you use a `std::vector` then its constructor will do it for you:
vector<int> y(10);
> if(y[i] > 3)
> {
> f = y[i] - 3;
> if(Array[i][r[f]] ==1)
> Array[i][r[f]] = 0;
This part doesn't appear meaningful.
Presumably this part is what you're seeking help with?
Cheers,
- Alf
== 3 of 5 ==
Date: Sat, Nov 30 2013 6:29 pm
From: axcytz@gmail.com
On Saturday, November 30, 2013 7:27:13 PM UTC-6, axc...@gmail.com wrote:
> I have 2d array (10x12), and they have 0 and 1s in them. I want to count the 1s in each array and I have two cases here: (1) if an array has more than three 1s, say k many, then generate k - 3 random variables and replace that many 1s with 0s if that random variable corresponds to a 1. (2)if an array has less than three 1s, say q many, then generate 3 - q random variables and replace that many 0s with 1s if that random variable corresponds to a 0. I have the following code so far. I need some suggestions here, I could not construct the code well.
>
>
>
> int y[10];
>
> int r[10];
>
>
>
> for(int i=0; i < 10; i++){
>
> for(int j=0; j < 12; j++)
>
> {
>
> y[i] += Array[i][j];
>
> }
>
> if(y[i] > 3)
>
> {
>
> f = y[i] - 3;
>
> if(Array[i][r[f]] ==1)
>
> Array[i][r[f]] = 0;
>
> }
>
> if(y[i] < 3)
>
> {
>
> f = 3 - y[i];
>
> if(Array[i][r[f]] ==0)
>
> Array[i][r[f]] = 1;
>
> }
>
> }
>
>
>
>
>
> Thanks
Yes, it is exactly what I want to do.
Is this a fair restatement:
* You have 10 arrays, each of 12 items, each item is either 0 or 1.
* In each array:
- Let n = count of 1 values in the array.
- If n < 3, then randomly change 0s into 1s until n = 3.
- If n > 3, then randomly change 1s into 0s until n = 3.
?
Yes, I need help in that part but I am also not confident about the usage of "f" and "y", as well.
Thanks
== 4 of 5 ==
Date: Sat, Nov 30 2013 7:18 pm
From: "Alf P. Steinbach"
On 01.12.2013 03:29, axcytz@gmail.com wrote:
> On Saturday, November 30, 2013 7:27:13 PM UTC-6, axc...@gmail.com wrote:
[snip requote of original]
> Yes, it is exactly what I want to do.
>
> Is this a fair restatement:
>
> * You have 10 arrays, each of 12 items, each item is either 0 or 1.
> * In each array:
> - Let n = count of 1 values in the array.
> - If n < 3, then randomly change 0s into 1s until n = 3.
> - If n > 3, then randomly change 1s into 0s until n = 3.
>
> ?
OK, but first, the convention for this group is to quote some context,
below that add your response or new text, perhaps quote some more, add
response below, and so on. That's called /bottom posting/. In contrast,
the above, with response first and then quote of the context, is called
/top posting/ and, although that's fine in e-mail (mostly because it
would be practically impossible to re-educate the zillions of e-mail
users), it's not-so-fine on Usenet.
See <url: http://www.parashift.com/c++-faq/netiquette.html> for more info.
The part about top-posting is in the 4th bullet point.
* * *
Now, (I believe that) the problems mainly stem from /doing too much/ in
one place of the code.
So I suggest, define a suitably named function for counting 1-bits, so
you get that code moved away from the main code.
Also define a suitably named function for replacing one random 0 in an
array, with 1.
And I suspect that it will help with a suitably named function that
given an array returns the index of the N'th zero-bit in that array.
Also, a suitably named function to generate a random integer in the
range [0...M] inclusive, given M as an argument.
Note that I'm using uppercase single-letter names here simply for
brevity. In source code both practices are generally Evil(TM). In order
to avoid undesired text substitutions and name clashes it's a good idea
to reserve uppercase names for macros, and both for readability and in
order to avoid visual confusion between e.g. 1 and l and between e.g. O
and 0, it's a good idea to generally avoid single-letter names, except
where they're established conventions such as for loop control.
* * *
So, the main code that you showed will now, in C++03, look something like
for( int i = 0; i < n_arrays; ++i )
{
ensure_3_nonzero_bits_in( bit_arrays[i] );
}
or in C++11 like
for( auto& a : bit_arrays )
{
ensure_3_nonzero_bits_in( a );
}
anyway with greatly reduced complexity in that top-level code, and
likewise in each called function (just define functions for relevant
chunks of code), and so on down -- the name of the game is /naming/.
Cheers & hth.,
- Alf
== 5 of 5 ==
Date: Sun, Dec 1 2013 3:13 am
From: Öö Tiib
On Sunday, 1 December 2013 03:27:13 UTC+2, axc...@gmail.com wrote:
> I have 2d array (10x12), and they have 0 and 1s in them.
Value that can be 1 or 0 is called bit. To have array of 12 bits
there is 'std::bitset<12>'.
typedef std::bitset<12> TwelveBits;
Then put it into array of 10
typedef std::array<TwelveBits,10> BitArray10x12;
> I want to count
> the 1s in each array ...
'std::bitset' has member function conveniently
called 'count':
TwelveBits foo (std::string("101110110011"));
std::cout << foo << " has ";
std::cout << foo.count() << " ones and ";
std::cout << (foo.size()-foo.count()) << " zeros.\n";
> ... and I have two cases here: (1) if an array has more
> than three 1s, say k many, then generate k - 3 random variables and
> replace that many 1s with 0s if that random variable corresponds to a 1.
> (2)if an array has less than three 1s, say q many, then generate 3 - q
> random variables and replace that many 0s with 1s if that random variable
> corresponds to a 0. I have the following code so far. I need some suggestions
> here, I could not construct the code well.
Yes, I removed your code since it did look confused.
There were no traces of making anything random. Try something like that:
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0,11);
auto gen = std::bind( distribution, generator );
int RandomFromTwelve = gen(); // generates int in range 0..11
==============================================================================
You received this message because you are subscribed to the Google Groups "comp.lang.c++"
group.
To post to this group, visit http://groups.google.com/group/comp.lang.c++?hl=en
To unsubscribe from this group, send email to comp.lang.c+++unsubscribe@googlegroups.com
To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c++/subscribe?hl=en
To report abuse, send email explaining the problem to abuse@googlegroups.com
==============================================================================
Google Groups: http://groups.google.com/?hl=en
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment