comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en
comp.lang.c++@googlegroups.com
Today's topics:
* Choosing the right epsilon for comparing doubles - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/5f5b1d64b66b5096?hl=en
* template friend with local classes ? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/1013c4946db48f70?hl=en
* Getting Started with a Visual Studio C++ 2013 IDE - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/6af661da9c3466bf?hl=en
* question regarding the shared_ptr use_count - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/236ca0a4278d1ba0?hl=en
* Boost - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/81738d66827a11c8?hl=en
* show all subset of a set - 6 messages, 5 authors
http://groups.google.com/group/comp.lang.c++/t/708873746ae8ae59?hl=en
* subset - 9 messages, 7 authors
http://groups.google.com/group/comp.lang.c++/t/898bcce92c1b97fc?hl=en
==============================================================================
TOPIC: Choosing the right epsilon for comparing doubles
http://groups.google.com/group/comp.lang.c++/t/5f5b1d64b66b5096?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Feb 4 2014 8:53 am
From: red floyd
On 2/4/2014 12:22 AM, Juha Nieminen wrote:
> walkietalkie74 <a.laforgia@gmail.com> wrote:
>> However, I am trying to write my program in a test-driven way, so I'd like
>> to write a tester that checks that the result of a specific calculation
>> is actually zero.
>
> Comparing floating point values is a tricky business. I have written
> an article about the subject:
>
> http://warp.povusers.org/programming/floating_point_caveats.html
>
And the classic document:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
==============================================================================
TOPIC: template friend with local classes ?
http://groups.google.com/group/comp.lang.c++/t/1013c4946db48f70?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Feb 4 2014 9:25 am
From: Luca Risolia
Helmut Jarausch wrote:
>> template <typename T> // ???????????? why do I need this if I will
>> never instantiate
>> class Add { /*...*/}; // a Term object with a nested Add object with a
>> different template parameter?
..because you used Add like template class in your friend function
declaration.
If you don't want Add to be a template class, just make it non-template and
modify the function declarations accordingly. It's left as an exercise.
==============================================================================
TOPIC: Getting Started with a Visual Studio C++ 2013 IDE
http://groups.google.com/group/comp.lang.c++/t/6af661da9c3466bf?hl=en
==============================================================================
== 1 of 3 ==
Date: Tues, Feb 4 2014 11:42 am
From: "W. eWatson"
On 2/1/2014 2:56 PM, Rick C. Hodgin wrote:
> On Friday, January 31, 2014 11:28:55 PM UTC-5, W. eWatson wrote:
>> I understand C++. What I'm looking for is how one operates the IDE
>> provided in VS C++ Desktop. What programming I've done usually does not
>> include a sophisticated IDE. I see FILE, EDIT,VIEW, DEBUG, etc. along
>> the top of the IDE. I'd like more details on these items. I would
>> suspect somewhere there's an explanation of them. Most manuals describe
>> them in the first 10 pages. Does a tutorial or manual that explains them
>> exist? It would be nice to see some examples of building a program and
>> compiling libraries for it.
>>
>> BTW, I've spent a lot of time today Googling VS introductions to IDEs,
>> but they never sync with the VS C++ 2013 product I have.
>
> There is an add-on utility I would recommend. It's called "Visual Assist X"
> and it's made by Whole Tomato (www.wholetomato.com). It will augment your
> editing experience to provide many features that are natively lacking in the
> Visual Studio IDE environment.
>
> Best regards,
> Rick C. Hodgin
>
Wholetomato looks good.
== 2 of 3 ==
Date: Tues, Feb 4 2014 12:03 pm
From: "Rick C. Hodgin"
On Tuesday, February 4, 2014 2:42:34 PM UTC-5, W. eWatson wrote:
> On 2/1/2014 2:56 PM, Rick C. Hodgin wrote:
> > There is an add-on utility I would recommend. It's called "Visual Assist X"
> > and it's made by Whole Tomato (www.wholetomato.com). It will augment your
> > editing experience to provide many features that are natively lacking in the
> > Visual Studio IDE environment.
>
> Wholetomato looks good.
Some of your new best friends:
Alt+G -- Goto declaration or definition. Works with overloaded functions
by name, and does not limit the scope by parameters.
Alt+Shift+R -- Refactor the highlighted token (change its name system-wide)
Alt+Shift+F -- Find the highlighted token
Also, there are options to turn on highlighting (such as when you move onto
a token it highlights other nearby instances to give you visual cues of
where that token is referenced). Hovering over a token shows you the
definition line and nearby comments, etc.
It will change the way I looked at the Visual Studio IDE.
Best regards,
Rick C. Hodgin
== 3 of 3 ==
Date: Tues, Feb 4 2014 2:08 pm
From: Jorgen Grahn
On Sat, 2014-02-01, W. eWatson wrote:
> On 1/31/2014 9:16 PM, Robert Wessel wrote:
>> On Fri, 31 Jan 2014 20:28:55 -0800, "W. eWatson"
>> <wolftracks@invalid.com> wrote:
>>
>>> I understand C++. What I'm looking for is how one operates the IDE
>>> provided in VS C++ Desktop. What programming I've done usually does not
>>> include a sophisticated IDE. I see FILE, EDIT,VIEW, DEBUG, etc. along
>>> the top of the IDE. I'd like more details on these items. I would
>>> suspect somewhere there's an explanation of them. Most manuals describe
>>> them in the first 10 pages. Does a tutorial or manual that explains them
>>> exist? It would be nice to see some examples of building a program and
>>> compiling libraries for it.
...
> I just installed 2010, and see there's a video in it for Beginner
> Developers. Cleverly the title is for the 2008 Express Edition. A little
> examination here shows me this isn't going to help me. Sigh!
Mostly out of curiosity: do you really need to use this IDE? You
don't seem very excited about it, and you seem comfortable with
whatever tools you have today. That's not a good start. In fact,
those are the reasons I've never used an IDE myself ...
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
==============================================================================
TOPIC: question regarding the shared_ptr use_count
http://groups.google.com/group/comp.lang.c++/t/236ca0a4278d1ba0?hl=en
==============================================================================
== 1 of 2 ==
Date: Wed, Feb 5 2014 2:55 pm
From: cuzdav@gmail.com
On Sunday, February 2, 2014 7:12:57 PM UTC-6, somenath wrote:
> I am not able to understand the behavior of the following program
>
> #include <iostream>
> #include<memory>
> #include<string>
> using namespace std;
>
> int main() {
> auto p1 = make_shared<string> (10,'S');
> cout<<"p1 use_count = "<<p1.use_count()<<endl;
> shared_ptr<string> p2(new string()) ;
> p1 = p2;
> cout<<"p2 use_count = "<<p2.use_count()<<endl;
> cout<<"second p1 use_count = "<<p1.use_count()<<endl;
> return 0;
> }
>
> p1 use_count = 1
> p2 use_count = 2
> second p1 use_count = 2 <<< ???
...
> Please let me know where my understanding is going wrong?
Perhaps it useful to have it explicitly pointed out: the reference count is associated with the shared object, not with the shared pointers.
Initial:
p1 refers to object1, whose count==1
p2 refers to object2, whose count==1
Change p1 to refer to what p2 refers to
p1=p2
(Notice, when p1 changes what it points to, the refcount
goes down on the old object, and up on the new one. Afterall,
p1 no longer points to the old object so its refcount must
change!)
Result:
object1 destroyed (no references)
p2 still refers to object2, whose count==2
p1 also refers to object2, whose count==2
Notice that p1 and p2 both end up referring to the same underlying
object, so they both report the same reference count.
Chris
== 2 of 2 ==
Date: Thurs, Feb 6 2014 1:46 am
From: Stuart
Am 03.02.14 02:12, schrieb somenath:
> I am not able to understand the behavior of the following program
>
> #include <iostream>
> #include<memory>
> #include<string>
> using namespace std;
>
> int main() {
> auto p1 = make_shared<string> (10,'S');
> cout<<"p1 use_count = "<<p1.use_count()<<endl;
> shared_ptr<string> p2(new string()) ;
> p1 = p2;
> cout<<"p2 use_count = "<<p2.use_count()<<endl;
> cout<<"second p1 use_count = "<<p1.use_count()<<endl;
> return 0;
> }
> Output
> ++++++++++++++++
> p1 use_count = 1
> p2 use_count = 2
> second p1 use_count = 2
>
> I can understand the first two print. At beginning p1 points to one string so the reference count is 1.
> When the statement p1=p2; executes p2's reference count gets incremented to 2 but at the same time I was expecting that p1's reference count to be decremented by 1 as p1 would be pointing to p2 now.
> Please let me know where my understanding is going wrong?
>
(this posting contains some ASCII art which will look quite bad if
viewed with proportional fonts).
The situation just before the assignment "p1 = p2;" looks like this:
Each shared_pointer does not point to the object directly but to an
intermediate object which contains the reference count. Both pointers p1
and p2 are the only shared_pointers pointing to their strings, so the
reference counts have both the value 1.
,''''''''`.
| refcnt 1|
,_______ `. | | _______________
| P1 |------=: |.......... . | |
L______| - | | `. |"SSSSSSSSSS" |
| ptr |----------'|_______________|
|_________J ,'
-'
,''''''''`.
| refcnt 1|
,_______ `. | | _______________
| P2 |------=: |.......... . | |
L______| - | | `. |"" |
| ptr |----------'|_______________|
|_________J ,'
-'
In the assignment statement, p1 will decrement the ref-count of the
intermediate object, since p1 no longer points to the intermediate
object of string "SSSSSSSSSS". Instead it will point to the intermediate
object of p2:
,''''''''`.
| refcnt 0|
,_______ | | _______________
| P1 | |.......... . | |
L______| | | `. |"SSSSSSSSSS" |
`. | ptr |----------'|_______________|
`. |_________J ,'
`. -'
\ |
`.|
---- ,''''''''`.
| refcnt 2|
,_______ `. | | _______________
| P2 |------=: |.......... . | |
L______| - | | `. |"" |
| ptr |----------'|_______________|
|_________J ,'
-'
Now the assigment statement will figure out that the reference count of
the intermediate object of "SSSSSSSSSS" has become zero, so it can
delete the intermediate object and the string "SSSSSSSSSS".
Sorry, if I did not use the proper vocabulary. The intermediate object
is an implementation details, although I doubt whether there is an
implementation that differs from this scheme.
Regards,
Stuart
==============================================================================
TOPIC: Boost
http://groups.google.com/group/comp.lang.c++/t/81738d66827a11c8?hl=en
==============================================================================
== 1 of 4 ==
Date: Thurs, Feb 6 2014 2:56 am
From: Öö Tiib
On Tuesday, 4 February 2014 04:25:53 UTC+2, woodb...@gmail.com wrote:
> On Monday, February 3, 2014 4:37:55 AM UTC-6, Öö Tiib wrote:
> >
> > I feel that you avoid answering what it is that your
> > *CMW* *does* *better* than competition for example
> > Protocol Buffers. Where is the edge from what you are
> > above?
>
>
> Would someone be willing to supply me with the
> Protocol Buffers pieces to serialize and deserialize
> a ::std::vector<::std::deque<double> > ?
>
> If so, I'll work on writing a test with CMW code
> that compares these. Tia.
You sell non-standard language-specific alternative to
wide set of well-established open and language-neutral
solutions. You sell it to engineers. Yet you openly tell
that you need help in finding out if it is outstanding in
anything at all?
== 2 of 4 ==
Date: Thurs, Feb 6 2014 2:33 pm
From: woodbrian77@gmail.com
On Thursday, February 6, 2014 4:56:27 AM UTC-6, Öö Tiib wrote:
> On Tuesday, 4 February 2014 04:25:53 UTC+2, woodb...@gmail.com wrote:
>
> > Would someone be willing to supply me with the
> > Protocol Buffers pieces to serialize and deserialize
> > a ::std::vector<::std::deque<double> > ?
> >
>
> > If so, I'll work on writing a test with CMW code
> > that compares these. Tia.
>
> You sell non-standard language-specific alternative to
It's free.
> wide set of well-established open and language-neutral
> solutions. You sell it to engineers. Yet you openly tell
> that you need help in finding out if it is outstanding in
> anything at all?
I have tests that compare with the serialization
library in Boost.
I haven't updated my site yet, but I'm updating
those tests using Boost 1.55. For the
vector<deque<double> > test, the Boost version is
over 2 times slower than the CMW version. The
Boost-based executable is over 3 times larger than
the CMW-based executable.
I'm not sure Protocol Buffers is worth comparing
against. If someone is interested in seeing that,
they can do a little bit to make it happen. I'm
happy to turn over my side of the test so they
can do the comparison at their location also.
Brian
Ebenezer Enterprises - A stone the builders refused
has become head of a corner. Psalms 118:22
http://webEbenezer.net
== 3 of 4 ==
Date: Fri, Feb 7 2014 2:31 am
From: Öö Tiib
On Friday, 7 February 2014 00:33:30 UTC+2, woodb...@gmail.com wrote:
> On Thursday, February 6, 2014 4:56:27 AM UTC-6, Öö Tiib wrote:
> > On Tuesday, 4 February 2014 04:25:53 UTC+2, woodb...@gmail.com wrote:
> >
> > > Would someone be willing to supply me with the
> > > Protocol Buffers pieces to serialize and deserialize
> > > a ::std::vector<::std::deque<double> > ?
> > >
> >
> > > If so, I'll work on writing a test with CMW code
> > > that compares these. Tia.
> >
> > You sell non-standard language-specific alternative to
>
> It's free.
>
> > wide set of well-established open and language-neutral
> > solutions. You sell it to engineers. Yet you openly tell
> > that you need help in finding out if it is outstanding in
> > anything at all?
>
> I have tests that compare with the serialization
> library in Boost.
Its usage is very narrow (being language- and version-specific).
Basically clipboard operations (undo/redo/cut/paste) within single
application and dumping objects into text or xml file or log for
debugging/problem reporting/code generation purposes.
For those things it is adequate; it does not matter if undo takes
25 or 50 milliseconds. CMW feels even more narrow in sense that
it does not look like suitable for most of named use-cases.
It is more likely that someone wanting to actually serialise
something between applications/services using boost uses
Boost.PropertyTree instead.
> I haven't updated my site yet, but I'm updating
> those tests using Boost 1.55. For the
> vector<deque<double> > test, the Boost version is
> over 2 times slower than the CMW version. The
> Boost-based executable is over 3 times larger than
> the CMW-based executable.
Yes, but what use case it is that you compare?
You push that 'std::vector<std::deque<double> >' reflection?
Typically it is implementation detail that may change between
versions. It is likely that profiling shows in version 2.1 that
'std::valarray' trumps 'std::deque' but it can't be done since
that would make CMW of 2.2 not compatible with 2.1. Telling
that it is indexed collection of indexed collections of floating
point values gives it more longevity and is neutral to
programming language.
If C++ reflection is major property for you then there is cereal.
http://uscilab.github.io/cereal/index.html (has JSON/XML and
various binary representations). There are even attempts of
making reflection libraries:
https://bitbucket.org/dwilliamson/clreflect
> I'm not sure Protocol Buffers is worth comparing
> against. If someone is interested in seeing that,
> they can do a little bit to make it happen. I'm
> happy to turn over my side of the test so they
> can do the comparison at their location also.
I'm also not sure. Those seem to be on same market
between small enough for using text-based protocols
and big enough for using real binary formats (audio,
graphics, database etc.). Protobuf lacks reflection
and CMW lacks language neutrality.
In current world it is hard to find need for entirely new
type of service. If it is not entirely new then you need
to collaborate with existing applications.
== 4 of 4 ==
Date: Fri, Feb 7 2014 6:07 pm
From: woodbrian77@gmail.com
On Friday, February 7, 2014 4:31:23 AM UTC-6, Öö Tiib wrote:
> On Friday, 7 February 2014 00:33:30 UTC+2, woodb...@gmail.com wrote:
>
> > On Thursday, February 6, 2014 4:56:27 AM UTC-6, Öö Tiib wrote:
>
> > I have tests that compare with the serialization
> > library in Boost.
>
> Its usage is very narrow (being language- and version-specific).
> Basically clipboard operations (undo/redo/cut/paste) within single
> application and dumping objects into text or xml file or log for
> debugging/problem reporting/code generation purposes.
>
> For those things it is adequate; it does not matter if undo takes
> 25 or 50 milliseconds. CMW feels even more narrow in sense that
> it does not look like suitable for most of named use-cases.
>
CMW doesn't support JSON or text formats, but I
don't think it's difficult for application written
using CMW to work with an application written in
a language other than C++. I don't have examples
of that, but there aren't difficult technical
problems behind doing that.
>
> It is more likely that someone wanting to actually serialise
> something between applications/services using boost uses
> Boost.PropertyTree instead.
>
>
> > I haven't updated my site yet, but I'm updating
> > those tests using Boost 1.55. For the
> > vector<deque<double> > test, the Boost version is
> > over 2 times slower than the CMW version. The
> > Boost-based executable is over 3 times larger than
> > the CMW-based executable.
>
> Yes, but what use case it is that you compare?
>
It's a low-level test. Applications are made up
of low-level operations ... I'm open to other
ideas of what to test.
>
>
> You push that 'std::vector<std::deque<double> >' reflection?
> Typically it is implementation detail that may change between
> versions. It is likely that profiling shows in version 2.1 that
> 'std::valarray' trumps 'std::deque' but it can't be done since
> that would make CMW of 2.2 not compatible with 2.1.
From a protocol perspective deque and valarray are
the same. First we marshal the size of the container
and then the elements. So it should be possible to
make the change you mention without compatibility
problem.
> Telling
> that it is indexed collection of indexed collections of floating
> point values gives it more longevity and is neutral to
> programming language.
>
>
> If C++ reflection is major property for you then there is cereal.
> http://uscilab.github.io/cereal/index.html (has JSON/XML and
> various binary representations).
The following type is from that link:
struct SomeData
{
int32_t id;
std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;
template <class Archive>
void save( Archive & ar ) const
{
ar( data );
}
template <class Archive>
void load( Archive & ar )
{
static int32_t idGen = 0;
id = idGen++;
ar( data );
}
};
The load function looks funny to me. In order
for something to be loaded, it has to have been
saved. How is id being set in code that's not
shown and can it be coherent with the static in
the load function?
I agree with their requiring C++ 2011 or newer.
But Cereal looks similar to the serialization
library in Boost. Users have to maintain
serialization functions and the dependence on iostream.
> There are even attempts of
> making reflection libraries:
> https://bitbucket.org/dwilliamson/clreflect
>
>
>
> > I'm not sure Protocol Buffers is worth comparing
> > against. If someone is interested in seeing that,
> > they can do a little bit to make it happen. I'm
> > happy to turn over my side of the test so they
> > can do the comparison at their location also.
>
> I'm also not sure. Those seem to be on same market
> between small enough for using text-based protocols
> and big enough for using real binary formats (audio,
> graphics, database etc.). Protobuf lacks reflection
> and CMW lacks language neutrality.
I think Protobuf has some support for reflection.
I don't think support for other languages is hard
to reach. Assuming we stick with byte-level
marshalling, I don't think there are difficult
problems in making applications written with
CMW work with applications in another language.
Also assuming IEEE 754 floating point.
Switching to bit-level marshalling would make
it more work to support other languages so
probably we won't do that.
Brian
Ebenezer Enteprises - In G-d we trust.
http://webEbenezer.net
==============================================================================
TOPIC: show all subset of a set
http://groups.google.com/group/comp.lang.c++/t/708873746ae8ae59?hl=en
==============================================================================
== 1 of 6 ==
Date: Thurs, Feb 6 2014 8:13 am
From: mary8shtr@gmail.com
A program is a set of all subsets of this part of the show. Users can enter a number., For example, if n = 2 the output looks like this:
{}
{1}
{2}
{1,2}
or for n=3 we have:
{}
{1}
{2}
{3}
{1,2}
{1,3}
{2,3}
{1,2,3}
meantime this program should be solved with tow way.
recursive function and Non-recursive.Can also be used in solving the problem of bitwise operations.
my algorithm that is for per subset exists one number. these numbers are form 0 until (2^n)-1.allow me explain it with a example.
n=3
{ } 000 0
{1} 100 4
{2} 010 2
{3} 001 1
{1,2} 110 6
{1,3} 101 5
{2,3} 011 3
{1,2,3} 111 7
The third column show some number(fn) that i use in code.now we know per number of subset has to state(sn).0 or 1. this means it exists or not exists.now i should match fn and sn.and suggested way is use of bitwise operators.(operator &).and now i don't know what do and thing with else thing.
#include <iostream>
using namespace std;
#include <conio.h>
#include <string.h>
int pow(int a,int b)
{
int p=1;
for(int i=0;i<b;i++)
p=p*a;
return p;
}
struct ar
{
int binary;
int content;
};
int main()
{
int testcase=0;
int n;
ar *a;
a=new ar [2000];
cin>>testcase;
int i=0;
while(i<testcase)
{
cin>>n;
for(int j=0,k=1;j<n;j++,k++)
{
a[j].content=k;
a[j].binary=2;
}
for(int p=0;p<pow(2,n)-1;p++)
{
cout<<'{';
for(int m=0;m<n;m++)
{
int b;
b=a[m].binary&p;
if(b==1)
cout<<a[i].content<<' ';
}
cout<<'}';
}
cout<<endl;
i++;
}
return 0;
}
== 2 of 6 ==
Date: Thurs, Feb 6 2014 8:48 am
From: Öö Tiib
On Thursday, 6 February 2014 18:13:54 UTC+2, mary...@gmail.com wrote:
> allow me explain it with a example.
>
> n=3
> { } 000 0
> {1} 100 4
> {2} 010 2
> {3} 001 1
> {1,2} 110 6
> {1,3} 101 5
> {2,3} 011 3
> {1,2,3} 111 7
Your illustration seems messed up. Usually we expect to
see that:
n=3
set binary decimal
{ } 000 0
{1} 001 1
{2} 010 2
{1,2} 011 3
{3} 100 4
{1,3} 101 5
{2,3} 110 6
{1,2,3} 111 7
Lower sub-patterns repeat so it is simple to make it recursive.
Also like we see the decimal is now linear so it is pointless to
make it recursive. ;)
== 3 of 6 ==
Date: Thurs, Feb 6 2014 8:49 am
From: Jorgen Grahn
On Thu, 2014-02-06, mary8shtr@gmail.com wrote:
> A program is a set of all subsets of this part of the show.
...
> int main()
>
> {
> int testcase=0;
> int n;
> ar *a;
> a=new ar [2000];
Where do they teach people to do it like this?
There are at least two simpler, safer and more obvious ways of getting
an array of 2000 objects:
1. Use a plain old C array:
ar a[2000];
2. Use std::vector:
std::vector<ar> a(2000);
3. Things which were invented after 1998, like std::array. I haven't
learned to use them so I cannot come with suggestions.
I realize this wasn't the core of the question. I didn't read it,
and it didn't seem very clear. Sorry.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
== 4 of 6 ==
Date: Thurs, Feb 6 2014 9:38 pm
From: ram@zedat.fu-berlin.de (Stefan Ram)
mary8shtr@gmail.com writes:
>this program should be solved with tow way.
with throw-away (code)?
#include <iostream> // ::std::cout
#include <ostream> // <<
#include <cassert> // assert
#include <new> // ::std::nothrow
void print( int const set[], size_t const n, size_t const m )
{ size_t j; int * c = new( ::std::nothrow )int[ n + 3 ];
if( c != nullptr )
{ assert( m >= n ); for( j = 1; j <= n; ++j )c[ j ]= j - 1;
c[ n + 1 ]= m; c[ n + 2 ]= 0; loop: goto visit;
next: for( j = 1; c[ j ]+ 1 == c[ j + 1 ]; ++j )c[ j ]= j - 1;
if( j > n )goto end; c[ j ]= c[ j ]+ 1; goto loop;
visit: for( size_t i = 1; i <= n; ++i )::std::cout << set[ c[ i ]];
::std::cout << "\n";
goto next; end: delete[] c; }}
int main()
{ int const set[] ={ 1, 2, 3 }; size_t const m = sizeof set / sizeof 0[ set ];
for( size_t size = 0; size <= m; ++size )print( set, size, m ); }
The first time ever I used array new and array delete in a program,
hope I got it right!
== 5 of 6 ==
Date: Fri, Feb 7 2014 12:20 am
From: David Brown
On 07/02/14 06:38, Stefan Ram wrote:
> mary8shtr@gmail.com writes:
>> this program should be solved with tow way.
>
> with throw-away (code)?
Don't you mean with "throw-up" code? :-)
>
> #include <iostream> // ::std::cout
> #include <ostream> // <<
> #include <cassert> // assert
> #include <new> // ::std::nothrow
>
> void print( int const set[], size_t const n, size_t const m )
> { size_t j; int * c = new( ::std::nothrow )int[ n + 3 ];
> if( c != nullptr )
> { assert( m >= n ); for( j = 1; j <= n; ++j )c[ j ]= j - 1;
> c[ n + 1 ]= m; c[ n + 2 ]= 0; loop: goto visit;
> next: for( j = 1; c[ j ]+ 1 == c[ j + 1 ]; ++j )c[ j ]= j - 1;
> if( j > n )goto end; c[ j ]= c[ j ]+ 1; goto loop;
> visit: for( size_t i = 1; i <= n; ++i )::std::cout << set[ c[ i ]];
> ::std::cout << "\n";
> goto next; end: delete[] c; }}
>
> int main()
> { int const set[] ={ 1, 2, 3 }; size_t const m = sizeof set / sizeof 0[ set ];
> for( size_t size = 0; size <= m; ++size )print( set, size, m ); }
>
> The first time ever I used array new and array delete in a program,
> hope I got it right!
>
== 6 of 6 ==
Date: Fri, Feb 7 2014 12:29 am
From: David Brown
On 06/02/14 17:13, mary8shtr@gmail.com wrote:
You've already been given some help about the problem itself, but let me
give you a little advice on style. The biggest key on your keyboard is
the space key - learn to use it. If you get in the habit of laying out
your code clearly and neatly /now/, your code will be far more readable
- leading to fewer errors (and higher marks for your homework).
Here are a couple of example style guides:
<https://www.kernel.org/doc/Documentation/CodingStyle>
<http://httpd.apache.org/dev/styleguide.html>
These are both for C, rather than C++ - but for this purpose C is a
subset of C++, and these guides are shorter than C++ style guides.
You don't necessarily have to agree with these guides on everything
(they don't agree with each other on everything), but learn from them.
And pretty much /every/ style guide agrees on the use of spaces.
> #include <iostream>
> using namespace std;
> #include <conio.h>
> #include <string.h>
> int pow(int a,int b)
> {
> int p=1;
> for(int i=0;i<b;i++)
> p=p*a;
> return p;
> }
> struct ar
> {
> int binary;
> int content;
> };
> int main()
>
> {
> int testcase=0;
> int n;
> ar *a;
> a=new ar [2000];
> cin>>testcase;
> int i=0;
> while(i<testcase)
> {
> cin>>n;
> for(int j=0,k=1;j<n;j++,k++)
> {
> a[j].content=k;
> a[j].binary=2;
> }
>
>
> for(int p=0;p<pow(2,n)-1;p++)
> {
> cout<<'{';
> for(int m=0;m<n;m++)
> {
>
> int b;
> b=a[m].binary&p;
> if(b==1)
> cout<<a[i].content<<' ';
>
> }
> cout<<'}';
> }
>
> cout<<endl;
> i++;
> }
> return 0;
> }
>
==============================================================================
TOPIC: subset
http://groups.google.com/group/comp.lang.c++/t/898bcce92c1b97fc?hl=en
==============================================================================
== 1 of 9 ==
Date: Fri, Feb 7 2014 4:30 am
From: mary8shtr@gmail.com
Hi.I write this program.but i should write it, too recursive.and Could you help me in this case.
#include <iostream>
using namespace std;
int pow(int a,int b)
{
int p=1;
for(int i=0;i<b;i++)
p=p*a;
return p;
}
int main()
{
int testcase=0;
int n;
long long *a;
a=new long long[200];
cin>>testcase;
int i=0;
int d,b,c;
while(i<testcase)
{
cin>>n;
for(int j=0,k=1;j<n;j++,k++)
{
a[j]=k;
}
for(int p=0;p<=pow(2,n)-1;p++)
{
cout<<'{';
d=p;
for(int m=0;m<n;m++)
{
c=pow(2,n-1);
b=c&d;
if(b!=0)
cout<<a[m]<<' ';
d=d<<1;
}
cout<<'}';
cout<<endl;
}
cout<<endl;
i++;
}
return 0;
}
== 2 of 9 ==
Date: Fri, Feb 7 2014 5:23 am
From: ram@zedat.fu-berlin.de (Stefan Ram)
mary8shtr@gmail.com writes:
>Hi.I write this program.but i should write it, too recursive.and Could you help me in this case.
#include <iostream>
#include <ostream>
#include <vector>
template< typename T >struct counter
{ ::std::vector< T >& sequence; int top;
counter( ::std::vector< T >&& sequence ):
sequence( sequence ), top( sequence.size() ){ count( 0 ); }
void count( int const pos )
{ if( pos == top )
{ for( int pos = 0; pos < top; ++pos )
::std::cout <<( sequence[ pos ]?( char )( '`' +( top - pos ) ): ' ' );
::std::cout << '\n'; }
else
{ sequence.at( pos )= 0; count( pos + 1 );
sequence.at( pos )= 1; count( pos + 1 ); }}};
int main(){ counter< int > c( ::std::vector< int >( 3 )); }
== 3 of 9 ==
Date: Fri, Feb 7 2014 11:04 am
From: Barry Schwarz
It would make your code a lot easier to read if
You indented consistently.
You doubled space between lines only to separate sections of your
functions.
You used the space bar more frequently.
Why does the definition of testcase contain initialization?
Why is a defined as a pointer and not as an array?
In the 'm' for loop, why do you re-evaluate c each iteration when its
value never changes?
What is the output supposed to represent?
If you google for recursion in C, you should get several hits that
show how to convert an iterative solution to a recursive one. If you
don't find an example for pow, look at the one for factorial.
On Fri, 7 Feb 2014 04:30:47 -0800 (PST), mary8shtr@gmail.com wrote:
>Hi.I write this program.but i should write it, too recursive.and Could you help me in this case.
>
>#include <iostream>
>using namespace std;
>int pow(int a,int b)
>{
> int p=1;
> for(int i=0;i<b;i++)
> p=p*a;
> return p;
>}
>int main()
>
>{
> int testcase=0;
>
> int n;
>
> long long *a;
>
> a=new long long[200];
>
> cin>>testcase;
>
> int i=0;
>
> int d,b,c;
>
> while(i<testcase)
> {
> cin>>n;
>
> for(int j=0,k=1;j<n;j++,k++)
> {
> a[j]=k;
> }
>
>
> for(int p=0;p<=pow(2,n)-1;p++)
> {
> cout<<'{';
>
> d=p;
> for(int m=0;m<n;m++)
> {
>
> c=pow(2,n-1);
>
> b=c&d;
>
> if(b!=0)
>
> cout<<a[m]<<' ';
>
> d=d<<1;
> }
>
> cout<<'}';
>
> cout<<endl;
> }
>
> cout<<endl;
>
> i++;
> }
> return 0;
> }
--
Remove del for email
== 4 of 9 ==
Date: Fri, Feb 7 2014 4:06 pm
From: woodbrian77@gmail.com
On Friday, February 7, 2014 1:04:40 PM UTC-6, Barry Schwarz wrote:
> It would make your code a lot easier to read if
>
>
> You indented consistently.
> You doubled space between lines only to separate sections of your
> functions.
>
> You used the space bar more frequently.
>
I'm with you for the first two, but I've seen
some who I think are good programmers not use
many spaces within a line of code, and I write
it that way sometimes also:
http://webEbenezer.net/misc/direct.cc
>
> If you google for recursion in C,
Watch out for Google.
Duckduckgo cares about your privacy:
https://Duckduckgo.com
http://donttrack.us
Brian
Ebenezer Enterprises
http://webEbenezer.net
== 5 of 9 ==
Date: Sat, Feb 8 2014 2:33 am
From: David Harmon
On Fri, 07 Feb 2014 11:04:40 -0800 in comp.lang.c++, Barry Schwarz
<schwarzb@dqel.com> wrote,
>Why does the definition of testcase contain initialization?
Because you should never define an uninitialized variable without a
really good reason!
The given code looks like:
int testcase=0;
cin>>testcase;
while(i<testcase)
Now, there are many problems you might pick with that code, but
anything caused by initializing testcase to a default value in case
the cin input fails is not among them. Would you rather that the
subsequent loop go running off to some undetermined possibly huge
number just because testcase was never given a value?
== 6 of 9 ==
Date: Sat, Feb 8 2014 2:43 am
From: ram@zedat.fu-berlin.de (Stefan Ram)
David Harmon <source@netcom.com> writes:
>The given code looks like:
> int testcase=0;
> cin>>testcase;
> while(i<testcase)
>Now, there are many problems you might pick with that code, but
>anything caused by initializing testcase to a default value in case
>the cin input fails is not among them. Would you rather that the
>subsequent loop go running off to some undetermined possibly huge
>number just because testcase was never given a value?
Yes, otherwise the real error (not checking the stream
state after reading) might remain hidden even longer!
== 7 of 9 ==
Date: Sat, Feb 8 2014 4:05 am
From: David Brown
On 08/02/14 11:33, David Harmon wrote:
> On Fri, 07 Feb 2014 11:04:40 -0800 in comp.lang.c++, Barry Schwarz
> <schwarzb@dqel.com> wrote,
>> Why does the definition of testcase contain initialization?
>
> Because you should never define an uninitialized variable without a
> really good reason!
>
> The given code looks like:
> int testcase=0;
> cin>>testcase;
> while(i<testcase)
>
> Now, there are many problems you might pick with that code, but
> anything caused by initializing testcase to a default value in case
> the cin input fails is not among them. Would you rather that the
> subsequent loop go running off to some undetermined possibly huge
> number just because testcase was never given a value?
>
You should never unnecessarily initialise a variable, because that stops
the compiler checking your code for you.
If you leave "int testcase;" uninitialised, and later use it without
first setting its value, then the compiler's warnings will tell you.
But if you've used "int testcase = 0;", then the compiler can't help you
spot that error.
== 8 of 9 ==
Date: Sat, Feb 8 2014 5:25 am
From: Victor Bazarov
On 2/8/2014 7:05 AM, David Brown wrote:
> On 08/02/14 11:33, David Harmon wrote:
>> On Fri, 07 Feb 2014 11:04:40 -0800 in comp.lang.c++, Barry Schwarz
>> <schwarzb@dqel.com> wrote,
>>> Why does the definition of testcase contain initialization?
>>
>> Because you should never define an uninitialized variable without a
>> really good reason!
>>
>> The given code looks like:
>> int testcase=0;
>> cin>>testcase;
>> while(i<testcase)
>>
>> Now, there are many problems you might pick with that code, but
>> anything caused by initializing testcase to a default value in case
>> the cin input fails is not among them. Would you rather that the
>> subsequent loop go running off to some undetermined possibly huge
>> number just because testcase was never given a value?
>>
>
> You should never unnecessarily initialise a variable, because that stops
> the compiler checking your code for you.
>
> If you leave "int testcase;" uninitialised, and later use it without
> first setting its value, then the compiler's warnings will tell you. But
> if you've used "int testcase = 0;", then the compiler can't help you
> spot that error.
Reliance on compiler warnings can only be done in conjunction with other
ways of ensuring correctness of the code. Code reviews and following
coding standards are other methods. However, in the end there is no
single action that would completely protect of making a mistake.
Besides, warnings are non-normative and therefore are not consistent
from compiler to compiler or even from one version of the same compiler
to another.
That said, a random value is worse than a known one. It can lead to
unpredictable behavior of the program. Repeatability, even if the
actions of the program are incorrect, helps tremendously in fixing the bugs.
V
--
I do not respond to top-posted replies, please don't ask
== 9 of 9 ==
Date: Sat, Feb 8 2014 5:43 am
From: ram@zedat.fu-berlin.de (Stefan Ram)
Victor Bazarov <v.bazarov@comcast.invalid> writes:
>That said, a random value is worse than a known one.
n3290 does not call it »random«:
»if no initialization is performed, an object with automatic
or dynamic storage duration has indeterminate value.«
==============================================================================
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