Sunday, November 20, 2016

Digest for comp.lang.c++@googlegroups.com - 25 updates in 9 topics

Christian Steins <cs01@quantentunnel.de>: Nov 20 05:18PM +0100

Hi,
following program gives this output:
 
name = c1 x = 1 y = 2
name = c2 x = 3 y = 4
destructor c1
destructor c2
destructor c1
destructor c1
destructor c2
 
I don't understand why the destructor is called 5 times and the sequence.
 
Compiler is TDM-GCC 4.9.2.
 
Bug or expected behaviour?
 
Should one avoid to store objects in a vector?
Better to store object pointers?
 
 
--- snip -----------------------------------
 
// c++ test program
 
#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
class myclass {
 
private:
string name;
int x;
int y;

public:
void print() {
cout << "name = " << name;
cout << " x = " << x << " y = " << y << endl;
}

myclass() { x = 0; y = 0; name = "unnamed"; }
myclass(string nam, int xx, int yy) { x = xx; y = yy; name = nam; }

~myclass() {
cout << "destructor " << name << endl;
}
};
 
int main() {
 
vector<myclass> myvec;
 
myclass c1("c1",1,2);
myclass c2("c2",3,4);
 
c1.print();
c2.print();
 
myvec.push_back(c1);
myvec.push_back(c2);
 
return 0;
}
 
--- snip -----------------------------------
 
Rgds,
Christian
"Öö Tiib" <ootiib@hot.ee>: Nov 20 08:47AM -0800

On Sunday, 20 November 2016 18:18:35 UTC+2, Christian Steins wrote:
> destructor c1
> destructor c2
 
> I don't understand why the destructor is called 5 times and the sequence.
 
Why don't you explain what you did expect and in what order and why?
Then we can see where your logic fails. For me 5 destructor calls
seems like expected and order seems OK.
 
After 'myvec.push_back(c1);' the vector has one element and capacity for
one element so 'myvec.push_back(c2);' causes it to allocate more storage,
copy 'c1' there and to destroy the old storage and so call destructor for
that 'c1' in it.
After that the 'main' ends that causes destruction of original 'c2'
original 'c1' and then 'myvec' and so copies of 'c1' and 'c2' in that
'myvec'. So 5 calls.
Christian Steins <cs01@quantentunnel.de>: Nov 20 06:30PM +0100

Am 20.11.2016 um 17:47 schrieb Öö Tiib:
 
> After that the 'main' ends that causes destruction of original 'c2'
> original 'c1' and then 'myvec' and so copies of 'c1' and 'c2' in that
> 'myvec'. So 5 calls.
 
I see. Thanks for explanation.
 
Christian
woodbrian77@gmail.com: Nov 20 09:16AM -0800


> I'll leave that to others.
 
> "Let another man praise you, and not your own mouth;
> A stranger, and not your own lips." Proverbs 27:2
 
That page still needs help.
 
Brian
Ebenezer Enterprises
http://webEbenezer.net
Christian Steins <cs01@quantentunnel.de>: Nov 20 03:40PM +0100

Hi,
ich möchte mich ein wenig in C++ einarbeiten.
 
Welches GUI Framework wäre empfehlenswert?
 
Es sollte:
 
- einsteigerfreundlich sein
- light weight
- Plattform übergreifend
- mit DEV c++ (Win7) laufen
 
Momentan habe ich wxWidgets im Auge.
 
Christian
"Öö Tiib" <ootiib@hot.ee>: Nov 20 09:01AM -0800

On Sunday, 20 November 2016 16:40:51 UTC+2, Christian Steins wrote:
> - Plattform übergreifend
> - mit DEV c++ (Win7) laufen
 
> Momentan habe ich wxWidgets im Auge.
 
I would prefer Qt to wxWidgets. Qt framework contains everything and
kitchen sink so it is not simple like you seem to ask but is
IMHO more novice friendly. It contains two different ways of building
GUI.
 
I like it because I can run software made with Qt on Windows, OS-X,
Linux, iOS and Android. Also there are more people who use it and
so more job offers where Qt framework is used than where wxWidgets is
used.
ram@zedat.fu-berlin.de (Stefan Ram): Nov 20 10:13AM

>Lets say I have an array with 500 randon ints between 0-9999
>I want to build a binary tree from the data in the array.
 
for_each( array.begin(), array.end(), insert_into_tree );
ram@zedat.fu-berlin.de (Stefan Ram): Nov 20 11:23AM

>>side.
>Well that's the degenerate case of sorted input. But you say it's random.
>I'd build the tree first and worry about balancing it after.
 
I cannot find a requirement in the OP that say that the
binary tree needs to be balanced.
 
Implementing functionality that is not required might have
disadvantages:
 
- might increase the project duration
- might increase the product costs
- might offer more surface for bugs and attacks
- the additional work might be multiplied when
documentation, tests and reviews are to be done
for the added functionality
ram@zedat.fu-berlin.de (Stefan Ram): Nov 20 12:46PM

I have written this program, which uses ADL.
Observe that there is no »::std« before »binary_search«.
 
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <iterator>
int main()
{ ::std::string str{ "abcdefghijklmnopqrstuvwxyz" };
::std::cout << binary_search( cbegin( str ), cend( str ), 'c' )<< '\n';
::std::cout << binary_search( cbegin( str ), cend( str ), '#' )<< '\n'; }
 
This worked. Next program I wrote:
Observe that there is »::std« before »for_each«.
 
main.cpp
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <ostream>
#include <iostream>
void print( char const c ){ ::std::cout << c << '\n'; }
int main()
{ ::std::initializer_list< char > l { 'a', 'b', 'c' };
::std::for_each( cbegin( l ), cend( l ), print ); }
 
I seem to need the »::std« in front of »for_each« in
this case. Why?
cs01@quantentunnel.de: Nov 20 02:43PM

This message was cancelled from within Mozilla Thunderbird.
ram@zedat.fu-berlin.de (Stefan Ram): Nov 20 04:43PM

>I don't understand why the destructor is called 5 times and the sequence.
 
main.cpp
 
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
 
using namespace ::std::literals;
 
struct myclass
{ static int counter;
int identity;
 
myclass() : identity{ ::myclass::counter++ }
{ ::std::clog << "created " <<
this->identity << " from scratch.\n"s; }
 
myclass( myclass const & other ):
identity{ ::myclass::counter++ }
{ ::std::clog << "created " << this->identity <<
" as a copy of " << other.identity << "\n"s; }
 
~myclass()
{ ::std::clog << "destroying " <<
this->identity << ".\n"s; }};
 
int ::myclass::counter = 0;
 
int main()
{ ::std::clog << "main 0.\n"s; ::std::vector< ::myclass > myvec;
::std::clog << "main 1.\n"s; ::myclass c0;
::std::clog << "main 2.\n"s; ::myclass c1;
::std::clog << "main 3.\n"s; myvec.push_back( c0 );
::std::clog << "main 4.\n"s; myvec.push_back( c1 ); }
 
transcript
 
main 0.
main 1.
created 0 from scratch.
main 2.
created 1 from scratch.
main 3.
created 2 as a copy of 0
main 4.
created 3 as a copy of 1
created 4 as a copy of 2
destroying 2.
destroying 1.
destroying 0.
destroying 4.
destroying 3.
ram@zedat.fu-berlin.de (Stefan Ram): Nov 20 04:56PM

Supersedes: <destructor-20161120174310@ram.dialup.fu-berlin.de>
 
>I don't understand why the destructor is called 5 times and the sequence.
 
main.cpp
 
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
 
using namespace ::std::literals;
 
template<typename T>
struct vector : public ::std::vector< T >
{ using ::std::vector< T >::vector;
~vector(){ ::std::clog << "destroying vector\n"s; }};
 
struct myclass
{ static int counter;
int identity;
 
myclass() : identity{ ::myclass::counter++ }
{ ::std::clog << "created " <<
this->identity << " from scratch.\n"s; }
 
myclass( myclass const & other ):
identity{ ::myclass::counter++ }
{ ::std::clog << "created " << this->identity <<
" as a copy of " << other.identity << "\n"s; }
 
~myclass()
{ ::std::clog << "destroying " <<
this->identity << ".\n"s; }};
 
int ::myclass::counter = 0;
 
int main()
{ ::std::clog << "main 0.\n"s; vector< ::myclass > myvec;
::std::clog << "main 1.\n"s; ::myclass c0;
::std::clog << "main 2.\n"s; ::myclass c1;
::std::clog << "main 3.\n"s; myvec.push_back( c0 );
::std::clog << "main 4.\n"s; myvec.push_back( c1 );
::std::clog << "main 5.\n"s; }
 
transcript
 
main 0.
main 1.
created 0 from scratch.
main 2.
created 1 from scratch.
main 3.
created 2 as a copy of 0
main 4.
created 3 as a copy of 1
created 4 as a copy of 2
destroying 2.
main 5.
destroying 1.
destroying 0.
destroying vector
destroying 4.
destroying 3.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 20 01:02AM +0100

On 19.11.2016 23:37, Popping mad wrote:
 
>> See https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx.
 
> can't reach a microsoft site. I think it is /dev/null routed.
 
> Do you can a C++ link? or a GCC?
 
Check out the SO C++ book list if you don't have a textbook.
 
You can just google that term.
 
I would suggest that you get yourself also a copy of a draft of the C++
standard, even though the standard is written mainly for compiler
vendors and therefore is generally not very accessible to learners.
 
 
Cheers! & hth.,
 
- Alf
 
PS: Microsoft documentation includes numerous examples of `void main`,
which is non-standard, and the Visual Studio default code for a C++
project has a different non-standard main function. To top it off, in
their documentation they have falsely alleged, since the early 1990s!,
that one of their non-standard main functions is the program's entry
point, confusing themselves to the extent that other info has become
corrupted. Thus MS is not a good authority on the C++ main function.
woodbrian77@gmail.com: Nov 20 07:51AM -0800

On Saturday, November 19, 2016 at 6:05:34 PM UTC-6, Alf P. Steinbach wrote:
 
> > Do you can a C++ link? or a GCC?
 
> Check out the SO C++ book list if you don't have a textbook.
 
> You can just google that term.
 
I suggest using Duckduckgo --https://duckduckgo.com
 
. They have better policies than Google.
 
 
Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 20 12:45PM

On 19/11/2016 23:16, Paavo Helde wrote:
> clarify what is meant.
 
> So, it appears I should have worded my claim more carefully: there are
> objects of pointer types, but there are no objects of reference types.
 
However a reference may occupy a region of storage like a pointer object.
 
/Flibble
Paavo Helde <myfirstname@osa.pri.ee>: Nov 20 05:50PM +0200

On 20.11.2016 14:45, Mr Flibble wrote:
 
> However a reference may occupy a region of storage like a pointer object.
 
It may or may not. The standard says:
 
"It is unspecified whether or not a reference requires storage."
 
Even if it does occupy storage, one cannot directly find the address and
size of that storage. At best one can *guess* such info from the address
and size of an enclosing object which logically contains the reference.
 
Cheers
Paavo
Popping mad <rainbow@colition.gov>: Nov 20 07:24AM

I know someone must have solved this problem at some point.
 
Lets say I have an array with 500 randon ints between 0-9999
 
I want to build a binary tree from the data in the array. Each node has
pointers to children nodes, a value and a point to a parent node, just
incase you want to look up.
 
How can you build this? A recursive function goes straight down the left
side.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 20 12:13PM +0100

On 20.11.2016 08:24, Popping mad wrote:
> incase you want to look up.
 
> How can you build this? A recursive function goes straight down the left
> side.
 
Well that's the degenerate case of sorted input. But you say it's random.
 
I'd build the tree first and worry about balancing it after.
 
 
Cheers!,
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 20 02:14PM +0100

On 20.11.2016 12:23, Stefan Ram wrote:
> - the additional work might be multiplied when
> documentation, tests and reviews are to be done
> for the added functionality
 
Are you for real?
 
Cheers!,
 
- Alf
ruben safir <ruben@mrbrklyn.com>: Nov 20 09:16AM -0500

On 11/20/2016 05:13 AM, Stefan Ram wrote:
> for_each( array.begin(), array.end(), insert_into_tree );
 
/dev/null - bye
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 20 02:17PM +0100

On 20.11.2016 13:46, Stefan Ram wrote:
> ::std::for_each( cbegin( l ), cend( l ), print ); }
 
> I seem to need the »::std« in front of »for_each« in
> this case. Why?
 
With a `std::initializer_list` the begin and end iterators are raw pointers.
 
This can also happen with other containers, depending on the standard
library implementation, but with `std::initializer_list` it's guaranteed.
 
Here's reliable documentation: <url:
http://en.cppreference.com/w/cpp/utility/initializer_list>.
 
 
Cheers & hth.,
 
- Alf
Jorgen Grahn <grahn+nntp@snipabacken.se>: Nov 20 09:01AM

On Tue, 2016-11-15, K. Frank wrote:
 
>> The assignment specifies that each line of input will basically
>> look like this:
 
>> nnnnn nnnnnn string string <newline>
...
> takes a std::string (obtained from getline) and returns a
> std::vector<std::string> that contains the white-space-separated
> tokens.
 
That's how I would do it, too. And I tend to have it in my toolbox:
 
vector<string> split(const string&, size_t limit);
 
Splits on whitespace, trims whitespace, and splits at most 'limit'
times. It's pretty much the split() function in Python and Perl.
 
It's surprisingly useful, especially with the optional limit. And I
guess you can build it from the regex parts of the standard library.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
JiiPee <no@notvalid.com>: Nov 20 10:01AM

>> habits people like me have.
 
> Streams are untidy.
> I use getline sometimes.
 
I would say streams can be slow.
JiiPee <no@notvalid.com>: Nov 20 10:04AM

On 20/11/2016 09:01, Jorgen Grahn wrote:
> That's how I would do it, too. And I tend to have it in my toolbox:
 
> vector<string> split(const string&, size_t limit);
 
 
me also (if super speed it not required). what is the purpose of limit?
Jorgen Grahn <grahn+nntp@snipabacken.se>: Nov 20 11:45AM

On Sun, 2016-11-20, JiiPee wrote:
>> That's how I would do it, too. And I tend to have it in my toolbox:
 
>> vector<string> split(const string&, size_t limit);
 
> me also (if super speed it not required).
 
For super speed, I can imagine a version which lets you iterate
through the parts. IIRC, the regex parts of the standard library has
something like that (haven't used it myself).
 
> what is the purpose of limit?
 
In my experience, and with the file formats I parse, most of the time
you know how many columns you want to split out. And others seem to
find it useful too, since the feature is there in Perl and Python.
 
The OP's example syntax was
 
number number text text
 
I'd split that with limit=3, and allow whitespace in the last piece of
text:
 
10 4811 A foo
11 802 B hello, world!
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: