Monday, January 20, 2014

comp.lang.c++ - 26 new messages in 5 topics - digest

comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en

comp.lang.c++@googlegroups.com

Today's topics:

* Boost - 14 messages, 7 authors
http://groups.google.com/group/comp.lang.c++/t/81738d66827a11c8?hl=en
* STL map to STL vector - 5 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/1164ef3d2f08f58f?hl=en
* memory manager to prevent memory leaks - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/f3d6fc8d8d354450?hl=en
* THRINAXODON BREAKS THE GUINESS WORLD RECORD FOR THE SMALLEST SPAM POST EVER.
IN YOUR FACE, CONRAD! - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/176ed2a9a46db3fd?hl=en
* STL::MAP: Printing values only once .. - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/671080d0fc07abda?hl=en

==============================================================================
TOPIC: Boost
http://groups.google.com/group/comp.lang.c++/t/81738d66827a11c8?hl=en
==============================================================================

== 1 of 14 ==
Date: Fri, Jan 17 2014 8:56 am
From: Victor Bazarov


On 1/17/2014 10:49 AM, Nick Baumbach wrote:
> n wrote:
>
>> On 17.01.2014 16:11, Nick Baumbach wrote:
>>> Does anybody use Boost code, what is it.
>>>
>>> I mean, it has to make things easier, but it does not looks like, since
>>> it takes hours to compile.
>>>
>>>
>> You can use the header-only sub-libraries without building Boost.
>>
>> However, the most useful of them are now part of the standard, e.g.
>> shared_ptr.
>
> How do I know whether I need it.

I think the guiding principle here is "if you need to ask, you probably
have no use for it. Yet."

Here is the metaphor I think is applicable here. Can you dig a ditch
using your spade? Would an excavator help? Not really, if a ditch is
but a few feet long and a foot deep and the soil is loose and light.
You would only use an excavator if the amount of work warrants it. Of
course, if you already have an excavator on site, using it to move even
a bit of earth costs you almost nothing and can save some time, compared
to digging with a spade, even a mere few cubic feet of dirt.

Read about Boost, learn about the problems people solved using it. Some
here might tell you about it, but using Boost's online forum is probably
more effective. If you find that you can solve some problem with it,
then get it, invest some time learning it. Or don't.

V
--
I do not respond to top-posted replies, please don't ask




== 2 of 14 ==
Date: Fri, Jan 17 2014 9:01 am
From: Nick Baumbach


n wrote:

> Read about Boost, learn about the problems people solved using it. Some
> here might tell you about it, but using Boost's online forum is probably
> more effective. If you find that you can solve some problem with it,
> then get it, invest some time learning it. Or don't.

And now please answer the question. Only if you can, I did not asked for
noise.




== 3 of 14 ==
Date: Fri, Jan 17 2014 9:59 am
From: Victor Bazarov


On 1/17/2014 12:01 PM, Nick Baumbach wrote:
> n wrote:
>
>> Read about Boost, learn about the problems people solved using it. Some
>> here might tell you about it, but using Boost's online forum is probably
>> more effective. If you find that you can solve some problem with it,
>> then get it, invest some time learning it. Or don't.
>
> And now please answer the question. Only if you can, I did not asked for
> noise.

Your original post did not contain any questions. It only contained a
childish whine about the perceived difficulty of compiling Boost. Learn
to ask questions. Visit this page:
http://www.catb.org/~esr/faqs/smart-questions.html

As Drew Lawson write here

On 1/17/2014 10:59 AM, Drew Lawson wrote:
> [..]
> We do not know what your projects are, so have no way of knowing
> what you need.

consider telling us what it is you do, what problems you're trying to
solve, _before_ anyone could tell you to use some specific part of Boost
or to skip it altogether.

And now please ask your questions. Or don't.

V
--
I do not respond to top-posted replies, please don't ask




== 4 of 14 ==
Date: Fri, Jan 17 2014 10:35 am
From: Jorgen Grahn


On Fri, 2014-01-17, Nick Baumbach wrote:
> Does anybody use Boost code, what is it.
>
> I mean, it has to make things easier, but it does not looks like, since it
> takes hours to compile.

I suspect that Stroustrup sums it up nicely:

http://www.stroustrup.com/bs_faq.html#boost

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .




== 5 of 14 ==
Date: Fri, Jan 17 2014 12:14 pm
From: Robert Wessel


On Fri, 17 Jan 2014 10:43:52 -0500, Victor Bazarov
<v.bazarov@comcast.invalid> wrote:

>On 1/17/2014 10:11 AM, Nick Baumbach wrote:
>> Does anybody use Boost code, what is it.
>>
>> I mean, it has to make things easier, but it does not looks like, since it
>> takes hours to compile.
>
>Plenty of people use Boost code. It does make things easier. If you
>don't like it, don't use it. There is very little value in complaining
>about something for which you don't have any use, or on which you don't
>have any intention to spend any of your precious time.
>
>Here is an analogy: it takes significant time to earn money to buy a car
>and to learn to drive one. But once you buy it and start using it, you
>often find that it does make your life easier. It's called "investment".
>
>If you think that a few hours of compilation is not worth the return
>that you might get from using Boost after you have prepared it for your
>use, then don't spend those few hours. Life's too short.


And just what part (or all) of Boost is going to require hours of
compilation time? Sounds like a troll to me...




== 6 of 14 ==
Date: Fri, Jan 17 2014 12:56 pm
From: Paavo Helde


Robert Wessel <robertwessel2@yahoo.com> wrote in
news:ck3jd9hvrjo7eio0idenggt72dg2hjtvct@4ax.com:

> And just what part (or all) of Boost is going to require hours of
> compilation time? Sounds like a troll to me...

Figuring out how to get the bjam (or b2 or whatever it is called nowadays)
compilation to work and then building all possible variants
(static/dynamic/debug/nodebug/32-bit/64-bit/static runtime/dynamic runtime
etc) of all existing compilable Boost libraries can indeed take hours on a
mediocre machine. Not that it's needed for anything, it's just the simplest
approach if someone has no idea what he wants.

Cheers
Paavo





== 7 of 14 ==
Date: Fri, Jan 17 2014 2:10 pm
From: woodbrian77@gmail.com


On Friday, January 17, 2014 9:11:24 AM UTC-6, Nick Baumbach wrote:
> Does anybody use Boost code, what is it.
>

I use the Boost Intrusive library.

> I mean, it has to make things easier, but it does not looks like, since it
> takes hours to compile.

I think parts of Boost, like the containers, are great.
Other parts of Boost are not so great.

Brian
Ebenezer Enterprises - Heavenly code.
http://webEbenezer.net




== 8 of 14 ==
Date: Fri, Jan 17 2014 3:04 pm
From: Robert Wessel


On Fri, 17 Jan 2014 14:56:47 -0600, Paavo Helde
<myfirstname@osa.pri.ee> wrote:

>Robert Wessel <robertwessel2@yahoo.com> wrote in
>news:ck3jd9hvrjo7eio0idenggt72dg2hjtvct@4ax.com:
>
>> And just what part (or all) of Boost is going to require hours of
>> compilation time? Sounds like a troll to me...
>
>Figuring out how to get the bjam (or b2 or whatever it is called nowadays)
>compilation to work and then building all possible variants
>(static/dynamic/debug/nodebug/32-bit/64-bit/static runtime/dynamic runtime
>etc) of all existing compilable Boost libraries can indeed take hours on a
>mediocre machine. Not that it's needed for anything, it's just the simplest
>approach if someone has no idea what he wants.


For grins, I just did a "bootstrap" / ".\b2" of a freshly downloaded
Boost 1.55.0 on an eight year old 2.8GHz dual core Pentium 4 (a
Pentium D), rotating disk, 4GB, WinXP, and running some other stuff as
well (so both cores were maxing out at times). I'm not sure how much
more mediocre we can practically get.

Wall clock was 53 minutes. And that's long by a bit, since I was away
from the PC when the bootstrap finished, but from the look of the time
stamps, about two minutes, and as I mentioned, I was using the machine
for other things as well. So let's call it 50 minutes on that
machine.

Admittedly MSVC is usually a faster compiler than GCC...




== 9 of 14 ==
Date: Fri, Jan 17 2014 3:23 pm
From: woodbrian77@gmail.com


On Friday, January 17, 2014 9:37:16 AM UTC-6, Alf P. Steinbach wrote:
>
> You can use the header-only sub-libraries without building Boost.
>
>
> However, the most useful of them are now part of the standard, e.g.
> shared_ptr.
>

I think std::array would be a better example. I still
don't find much need for shared_ptr.

Brian
Ebenezer Enterprises
http://webEbenezer.net




== 10 of 14 ==
Date: Fri, Jan 17 2014 4:09 pm
From: Ian Collins


woodbrian77@gmail.com wrote:
> On Friday, January 17, 2014 9:37:16 AM UTC-6, Alf P. Steinbach wrote:
>>
>> You can use the header-only sub-libraries without building Boost.
>>
>>
>> However, the most useful of them are now part of the standard, e.g.
>> shared_ptr.
>>
>
> I think std::array would be a better example. I still
> don't find much need for shared_ptr.

Maybe you don't but others certainly do!

--
Ian Collins




== 11 of 14 ==
Date: Fri, Jan 17 2014 4:27 pm
From: Ian Collins


Robert Wessel wrote:
> On Fri, 17 Jan 2014 14:56:47 -0600, Paavo Helde
> <myfirstname@osa.pri.ee> wrote:
>
>> Robert Wessel <robertwessel2@yahoo.com> wrote in
>> news:ck3jd9hvrjo7eio0idenggt72dg2hjtvct@4ax.com:
>>
>>> And just what part (or all) of Boost is going to require hours of
>>> compilation time? Sounds like a troll to me...
>>
>> Figuring out how to get the bjam (or b2 or whatever it is called nowadays)
>> compilation to work and then building all possible variants
>> (static/dynamic/debug/nodebug/32-bit/64-bit/static runtime/dynamic runtime
>> etc) of all existing compilable Boost libraries can indeed take hours on a
>> mediocre machine. Not that it's needed for anything, it's just the simplest
>> approach if someone has no idea what he wants.
>
>
> For grins, I just did a "bootstrap" / ".\b2" of a freshly downloaded
> Boost 1.55.0 on an eight year old 2.8GHz dual core Pentium 4 (a
> Pentium D), rotating disk, 4GB, WinXP, and running some other stuff as
> well (so both cores were maxing out at times). I'm not sure how much
> more mediocre we can practically get.
>
> Wall clock was 53 minutes. And that's long by a bit, since I was away
> from the PC when the bootstrap finished, but from the look of the time
> stamps, about two minutes, and as I mentioned, I was using the machine
> for other things as well. So let's call it 50 minutes on that
> machine.
>
> Admittedly MSVC is usually a faster compiler than GCC...

On a more up to date machine with gcc:

time ./b2 -j 32

<stuff>

The Boost C++ Libraries were successfully built!

real 1m2.319s
user 27m57.961s
sys 1m49.214s

--
Ian Collins




== 12 of 14 ==
Date: Fri, Jan 17 2014 8:18 pm
From: woodbrian77@gmail.com


On Friday, January 17, 2014 6:09:45 PM UTC-6, Ian Collins wrote:
>
> Maybe you don't but others certainly do!
>

I think Mr. Stroustrup's comments on shared_ptr
are helpful.

At the 46 minutes and 43 second mark here:

http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Interactive-Panel-The-Importance-of-Being-Native

He says he wouldn't list shared_ptr in his top ten
C++ 2011 features. I agree.

I think it's a good idea to review/minimize your use
of unique_ptr and to be even more wary of shared_ptr.

Brian
Ebenezer Enteprises - So far G-d has helped us.
http://webEbenezer.net




== 13 of 14 ==
Date: Sat, Jan 18 2014 12:25 am
From: Nick Baumbach


Robert Wessel oratorically vehemently insists


> And just what part (or all) of Boost is going to require hours of
> compilation time? Sounds like a troll to me...

Sir, you do not contribute! YOU seemingly are the troll here.

Or, alternatively, are fucking stupid. In any case, stop infesting this
thread with nonsense and leave!




== 14 of 14 ==
Date: Sat, Jan 18 2014 12:30 am
From: Nick Baumbach


Ian Collins oratorically vehemently insists

> On a more up to date machine with gcc:
>
> time ./b2 -j 32
>
> <stuff>
>
> The Boost C++ Libraries were successfully built!
>
> real 1m2.319s user 27m57.961s sys 1m49.214s

Wow, -j 32 !! Say no more. Where did you find that 32 since at most I only
can find 16, as 2 threads per core. Actually thread core, not real core.
An i7 is still a 4 core, not sure how that threaded core is embedded into
the hardware.

Please elaborate





==============================================================================
TOPIC: STL map to STL vector
http://groups.google.com/group/comp.lang.c++/t/1164ef3d2f08f58f?hl=en
==============================================================================

== 1 of 5 ==
Date: Fri, Jan 17 2014 1:53 pm
From: Vir Campestris


On 17/01/2014 14:28, Robert Wessel wrote:
> While I can see being able to search for a pair so long a key_equal is
> provided, I'm not sure how you could manage the complexity guarantees
> if you ignored hash_function. The problem is when hash_function
> preprocesses the key in some way before hashing it (for example, you
> might lowercase a string, so that the hash is not case sensitive).

That doesn't matter, it's what the key_equal does that counts.

You'll still be able to store objects if your hash just returns zero -
it's for efficiency only that you want to minimise hash clashes.

Andy




== 2 of 5 ==
Date: Fri, Jan 17 2014 2:23 pm
From: Robert Wessel


On Fri, 17 Jan 2014 21:53:13 +0000, Vir Campestris
<vir.campestris@invalid.invalid> wrote:

>On 17/01/2014 14:28, Robert Wessel wrote:
>> While I can see being able to search for a pair so long a key_equal is
>> provided, I'm not sure how you could manage the complexity guarantees
>> if you ignored hash_function. The problem is when hash_function
>> preprocesses the key in some way before hashing it (for example, you
>> might lowercase a string, so that the hash is not case sensitive).
>
>That doesn't matter, it's what the key_equal does that counts.
>
>You'll still be able to store objects if your hash just returns zero -
>it's for efficiency only that you want to minimise hash clashes.


Which is why I said: "While I can see being able to search for a pair
so long a key_equal is provided, I'm not sure how you could manage the
complexity guarantees if you ignored hash_function".




== 3 of 5 ==
Date: Fri, Jan 17 2014 2:27 pm
From: woodbrian77@gmail.com


Please don't swear here, Leigh.




== 4 of 5 ==
Date: Fri, Jan 17 2014 2:35 pm
From: woodbrian77@gmail.com


On Friday, January 17, 2014 3:53:13 PM UTC-6, Vir Campestris wrote:
>
> That doesn't matter, it's what the key_equal does that counts.
>
> You'll still be able to store objects if your hash just returns zero -
> it's for efficiency only that you want to minimise hash clashes.
>

I'd consider that to be an efficiency bug.

Brian
Ebenezer Enterprises
http://webEbenezer.net





== 5 of 5 ==
Date: Fri, Jan 17 2014 3:10 pm
From: woodbrian77@gmail.com


On Wednesday, January 15, 2014 4:52:38 PM UTC-6, Öö Tiib wrote:
>
> The C++ standard committee that wrote C++98 also thought
> so. However in practice there was tons of newbies who wrote
> things like 'if(x.size() == 0)' instead of 'if(x.empty())'. That is
> terrible performance hit if a set counts its elements each time
> someone detects its emptiness but the bug is so subtle and
> common so the library implementers started to cache the size.
> The committee then wizened up and standardised practice
> that existed.

It seems there were some sadists on the committee --
"What is is right."

Oh well, we have alternatives beyond committee's ideas.
For example, if you are working on on line services,
you may have a lot of freedom in terms of what libraries
you use in the back end of your service.


Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net





==============================================================================
TOPIC: memory manager to prevent memory leaks
http://groups.google.com/group/comp.lang.c++/t/f3d6fc8d8d354450?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Jan 17 2014 2:49 pm
From: hbdevelop1@gmail.com


Hello,

I have written a memory tracker to prevent memory leaks in my programs.
The memory tracker meets the following requirements:
r1-Allocate memory using the system's new/new[].
r2-Deallocate memory using the system's delete/delete[].
r3-Log the file name and line where an allocation happens.
r4-When a memory is fred, remove the memory allocation from the log.
r5-At application exit, display the log.

Please find the code at the end of the email.
I have the following questions :

1-
g++ issues the following warning in my operator delete/delete[]: memtracker4.cpp:24:9: warning: deleting 'void*' is undefined [enabled by default]
delete p;
^
Do you have any idea how to correct the code so I don't get the warning ?

2-
To remove tracks of memory added in my operator new/new[], I am using template functions deleteArray and deleteObj.
But I remember in one company I worked in, they were using new(__FILE__,__LINE__) or new[](__FILE__,__LINE__) for allocations and plain delete/delete[] for deallocation.
And I wonder now, how they were removing memory tracks added by their operator new/new[] since they were using plain delete or delete[] for deallocation.
Could anybody please tell me if this is possible ? or were they not removing tracks ?

3-
Please tell me anything else to improve this code.

/////// code ////////

#include <stdio.h>
#include <new>

void AddToLog(long ptr, size_t size,const char * filename, int line)
{
printf("allocation : 0x%08X,%d,%s,%d\n",ptr, size,filename, line);
}

void RemoveFromLog(long ptr)
{
printf("deallocation : 0x%08X\n",ptr);
}

void * operator new(size_t size, const char *filename, int line)
{
void *ptr = ::operator new(size);
AddToLog((long)ptr, size, filename, line);
return(ptr);
};

void * operator new[](size_t size, const char *filename, int line)
{
void *ptr = ::operator new[](size);
AddToLog((long)ptr, size, filename, line);
return(ptr);
};

void operator delete(void *p, const char *filename, int line)
{
RemoveFromLog((long)p);
delete p; //g++ outputs warning: deleting 'void*' is undefined [enabled by default]
};

void operator delete[](void *p, const char *filename, int line)
{
RemoveFromLog((long)p);
delete [] p; //g++ outputs warning: deleting 'void*' is undefined [enabled by default]
};


template<class T> void deleteObj(T *p)
{
RemoveFromLog((long)p);
delete p;
};

template<class T> void deleteArray(T *p)
{
RemoveFromLog((long)p - sizeof(unsigned int));
delete [] p;
};


struct O11
{
int x;
public:
~O11 (){}
};

#define new new(__FILE__,__LINE__)

int main()
{
char *c=new char;
deleteObj<char>(c);

O11 *o=new O11;
deleteObj<O11>(o);

O11 *o1=new O11[10];
deleteArray<O11>(o1);

char *c3=new char[3];
deleteObj<char>(c3);
/*Note that I am treating c3 as a simple object not as array
(which caused my question at http://stackoverflow.com/questions/21178252/c-book-detailing-delete-and-delete)
*/

return 0;
}




== 2 of 2 ==
Date: Fri, Jan 17 2014 11:34 pm
From: Paavo Helde


hbdevelop1@gmail.com wrote in
news:db61afcf-75c0-4f17-b7e1-a982425f96f8@googlegroups.com:

> Hello,
>
> I have written a memory tracker to prevent memory leaks in my
> programs.

This kind of memory tracker does not prevent leaks; at best it just
reports them, and *only in the executed code paths* which happen to be
passed during the run.

However, in C++ actually *preventing* memory leaks is usually simple,
just don't ever use new/delete (or malloc/free) in the application code.
Use automated classes like std::vector instead. If really needed,
encapsulate new/delete in a specific RAII class.

This avoids simple 'forgot-to-delete' errors, so any memory leaks can
come only from more complex logic errors in the program like accumulating
objects in some unbounded container or creating cyclic dependencies.

Anyway, when you don't have new/delete on the application level code any
more, you will notice that the __FILE__ and __LINE__ locations injected
by your 'new' macro do not give much useful information, if any. You will
want more deep call stacks instead (which are indeed provided by proper
memory debugging tools like valgrind).

So, as this memory tracker is both unreliable, subfunctional and most
often not needed, I would advise to ditch it and turn your efforts
instead of getting rid of new/delete in your code. For dealing with
remaining supposedly rare memory problems I would suggest to use existing
special tools like valgrind.


> The memory tracker meets the following requirements:
> r1-Allocate memory using the system's new/new[].
> r2-Deallocate memory using the system's delete/delete[].
> r3-Log the file name and line where an allocation happens.
> r4-When a memory is fred, remove the memory allocation from the log.
> r5-At application exit, display the log.
>
> Please find the code at the end of the email.
> I have the following questions :
>
> 1-
> g++ issues the following warning in my operator delete/delete[]:
> memtracker4.cpp:24:9: warning: deleting �void*� is undefined [enabled
> by default]
> delete p;
> ^
> Do you have any idea how to correct the code so I don't get the
> warning ?

This should be something like ::operator delete [](p); instead.

>
> 2-
> To remove tracks of memory added in my operator new/new[], I am using
> template functions deleteArray and deleteObj. But I remember in one
> company I worked in, they were using new(__FILE__,__LINE__) or
> new[](__FILE__,__LINE__) for allocations and plain delete/delete[]
> for deallocation. And I wonder now, how they were removing memory
> tracks added by their operator new/new[] since they were using plain
> delete or delete[] for deallocation. Could anybody please tell me if
> this is possible ? or were they not removing tracks ?

Why would you need __FILE__ and __LINE__ of the point of deletion if you
will remove the allocation entry anyway?

hth
Paavo





==============================================================================
TOPIC: THRINAXODON BREAKS THE GUINESS WORLD RECORD FOR THE SMALLEST SPAM POST
EVER. IN YOUR FACE, CONRAD!
http://groups.google.com/group/comp.lang.c++/t/176ed2a9a46db3fd?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 17 2014 5:35 pm
From: "OVER 9000!"


http://thrinaxodon.wordpress.com/
--
Thrinaxodon, The Ultimate Defender of USENET





==============================================================================
TOPIC: STL::MAP: Printing values only once ..
http://groups.google.com/group/comp.lang.c++/t/671080d0fc07abda?hl=en
==============================================================================

== 1 of 4 ==
Date: Fri, Jan 17 2014 6:39 pm
From: Rahul Mathur


Hi,

I have a input.txt file separator by pipe '|' as -

40147|181|ORANGE|MIKE|XX||1000397900|3500
40148|373|ORANGE|BOB|XX||1078889400|4500
40149|673|ORANGE|TREY|XX||1095159900|5500


I only wish to have all the FIRST(ID=40147) and LAST FIELD(PRICE=3500) to be printed for three lines as given above.

I am reading as -

--
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>

using namespace std;

struct FILE_INPUT {
int ID;
int Asset_ID;
char T_Name[];
char Symbol[];
char Series[];
char Gap;
int Date;
int price;
};


int main() {

typedef std::map<int, int> unordmap;
unordmap ask;

FILE_INPUT fl_inp;
memset( &fl_inp, 0, sizeof(fl_inp) );
ifstream in ("input.txt");
string s;

const char delimiter = '|';
while ( getline (in, s) ) {
trim( s );
if ( !s.empty() ) {
stringstream strm( s );
string item;
getline( strm, item );
stringstream stra( item );
string tmp;
getline( stra, tmp, delimiter );
{
stringstream strb( tmp );
strb >> fl_inp.ID;
}
getline( stra, tmp, delimiter )
{
stringstream strb( tmp );
strb >> fl_inp.price;
}

ask.insert( std::make_pair((fl_inp.ID),(fl_inp.price)) );
unordmap::iterator pos = ask.begin();
for (pos = ask.begin(); pos != ask.end(); ++pos) {
std::cout << "ID: " << pos->first << "\t"
<< "Price: " << pos->second << std::endl;
}
std::cout << " " << endl;
}
}
}
--

The map is being inserted with both ID as key and price as value but while printing it prints the values thrice as -

ID: 40147 Price: 3500

ID: 40147 Price: 4500
ID: 40148 Price: 5500

ID: 40147 Price: 3500
ID: 40148 Price: 4500
ID: 40149 Price: 5500

The MAP should print all values together only once as -

ID: 40147 Price: 3500
ID: 40148 Price: 4500
ID: 40149 Price: 5500

--

Can I maintain some checks to print all values simply once.

Thanks.




== 2 of 4 ==
Date: Fri, Jan 17 2014 6:53 pm
From: Ian Collins


Rahul Mathur wrote:
> Hi,
>
> I have a input.txt file separator by pipe '|' as -
>
> 40147|181|ORANGE|MIKE|XX||1000397900|3500
> 40148|373|ORANGE|BOB|XX||1078889400|4500
> 40149|673|ORANGE|TREY|XX||1095159900|5500
>
>
> I only wish to have all the FIRST(ID=40147) and LAST FIELD(PRICE=3500) to be printed for three lines as given above.
>
> I am reading as -
>
> --
> #include <iostream>
> #include <fstream>
> #include <sstream>
> #include <string>
> #include <boost/algorithm/string.hpp>
>
> using namespace std;
>
> struct FILE_INPUT {
> int ID;
> int Asset_ID;
> char T_Name[];

You really shouldn't be using a zero size array here, it isn't valid C++
and certainly isn't what you want.

> char Symbol[];
> char Series[];
> char Gap;
> int Date;
> int price;
> };
>
>
> int main() {
>
> typedef std::map<int, int> unordmap;

Not a very good name for a std::map, which is ordered!

> unordmap ask;
>
> FILE_INPUT fl_inp;
> memset( &fl_inp, 0, sizeof(fl_inp) );
> ifstream in ("input.txt");
> string s;
>
> const char delimiter = '|';
> while ( getline (in, s) ) {
> trim( s );

Missing namespace?

> if ( !s.empty() ) {
> stringstream strm( s );
> string item;
> getline( strm, item );
> stringstream stra( item );
> string tmp;
> getline( stra, tmp, delimiter );
> {
> stringstream strb( tmp );
> strb >> fl_inp.ID;
> }
> getline( stra, tmp, delimiter )

Missing semicolon.

> {
> stringstream strb( tmp );
> strb >> fl_inp.price;
> }
>
> ask.insert( std::make_pair((fl_inp.ID),(fl_inp.price)) );

This next section shouldn't be inside the while loop! You are printing
the map each time you add to it.

> unordmap::iterator pos = ask.begin();
> for (pos = ask.begin(); pos != ask.end(); ++pos) {
> std::cout << "ID: " << pos->first << "\t"
> << "Price: " << pos->second << std::endl;
> }
> std::cout << " " << endl;

To here.

> }
> }
> }
>


--
Ian Collins




== 3 of 4 ==
Date: Fri, Jan 17 2014 7:24 pm
From: "Alf P. Steinbach"


On 18.01.2014 03:39, Rahul Mathur wrote:
> Hi,

Oh, hi!


> I have a input.txt file separator by pipe '|' as -
>
> 40147|181|ORANGE|MIKE|XX||1000397900|3500
> 40148|373|ORANGE|BOB|XX||1078889400|4500
> 40149|673|ORANGE|TREY|XX||1095159900|5500
>
>
> I only wish to have all the FIRST(ID=40147) and LAST FIELD(PRICE=3500) to
> be printed for three lines as given above.

I think this would be a task for "awk", or its modern replacement if
there is one. But as I recall from old days, "awk" was good at splitting
lines into fields. If working in Windows, then there are lots of "Unix
utilities" around, including just installing Cygwin (one practical idea
is to map drive U: to such utilities, write "u::awk").


> I am reading as -
>
> --
> #include <iostream>
> #include <fstream>
> #include <sstream>
> #include <string>
> #include <boost/algorithm/string.hpp>

Using Boost is generally a good idea when you have it available, but in
this case I think it's shooting gnats with intercontinental ballistic
H-bomb missiles. Will kill them gnats, guaranteed, yes. But, overkill...


> using namespace std;
>
> struct FILE_INPUT {

It's a good idea to reserve ALL UPPERCASE for macro names.

That way you tend to avoid name collisions and unintended text
substitutions.

Conversely, it's not a good idea to use ALL UPPERCASE for anything else
-- in C and C++.

Java and C# conventions are a different matter, since neither Java nor
C# has a preprocessor.

It's quite a bit ironic in a way. Java got its convention for constants
from C, where constants had to be defined as macros, hence ALL
UPPERCASE. So the Java convention is really about using ALL UPPERCASE
for macros-that-denote-constants, but Java doesn't have a preprocessor,
and does have a facility for defining constants, so it's all misplaced.


> int ID;

Again, ungood ALL UPPERCASE name.


> int Asset_ID;
> char T_Name[];

Oh noes, that won't work! In standard C++ an array must have a specified
size. And unless it's dynamically allocated, size must be >0.

This shouldn't compile with strict ISO compliance.

An alternative for unknown-string-size is to use a `std::string`.


> char Symbol[];
> char Series[];
> char Gap;
> int Date;
> int price;
> };
>
>
> int main() {
>
> typedef std::map<int, int> unordmap;
> unordmap ask;
>
> FILE_INPUT fl_inp;
> memset( &fl_inp, 0, sizeof(fl_inp) );

Oh! `memset`, that's easy to get wrong, and needlessly verbose.

In standard C++ just do

FILE_INPUT fl_inp = {};

to zero it.

See, it's safer, speedier, and shorter... ;-)


> ifstream in ("input.txt");
> string s;
>
> const char delimiter = '|';
> while ( getline (in, s) ) {
> trim( s );

I do not see where you get that unqualified `trim` function from, but
perhaps Boost (formally incorrect) defines it in the `std` namespace so
that it's found by argument dependent lookup?


> if ( !s.empty() ) {
> stringstream strm( s );
> string item;
> getline( strm, item );

This reads the whole line from the stream. And the whole line is all
there is. What is the purpose of that?


> stringstream stra( item );
> string tmp;
> getline( stra, tmp, delimiter );
> {
> stringstream strb( tmp );
> strb >> fl_inp.ID;
> }
> getline( stra, tmp, delimiter )
> {
> stringstream strb( tmp );
> strb >> fl_inp.price;
> }

Presumably the above code is what remained after you removed error
checking etc., so as to get simpler code for presentation here?


> ask.insert( std::make_pair((fl_inp.ID),(fl_inp.price)) );
> unordmap::iterator pos = ask.begin();
> for (pos = ask.begin(); pos != ask.end(); ++pos) {
> std::cout << "ID: " << pos->first << "\t"
> << "Price: " << pos->second << std::endl;
> }
> std::cout << " " << endl;
> }
> }
> }
>

Consider something like this (it differs from yours in not verifying
that the strings are valid number specifications, but you can just add
that):


[code]
#include <algorithm> // std::replace
#include <iostream>
#include <map> // std::map
#include <sstream> // std::istringstream
#include <stdlib.h> // EXIT_FAILURE
#include <string> // std::string
using namespace std;

#define ITEMS_OF( c ) c.begin(), c.end()

auto main()
-> int
{
string line;
while( getline( cin, line ) )
{
replace( ITEMS_OF( line ), '|', ' ' );
istringstream stream( line );
for( int i = 1; i <= 7; ++i )
{
string word;
if( (stream >> word).fail() )
{
cout << endl;
cerr << "! Not enough items on this input line:" << endl;
cerr << "! '" << line << "'" << endl;
return EXIT_FAILURE;
}
if( i == 1 ) { cout << "ID: " << word << " "; }
if( i == 7 ) { cout << "Price: " << word << endl; }
}
}
}
[/code]


Cheers & hth.,

- Alf





== 4 of 4 ==
Date: Fri, Jan 17 2014 9:39 pm
From: Rahul Mathur


On Saturday, January 18, 2014 8:09:21 AM UTC+5:30, Rahul Mathur wrote:
> Hi,
>
>
>
> I have a input.txt file separator by pipe '|' as -
>
>
>
> 40147|181|ORANGE|MIKE|XX||1000397900|3500
>
> 40148|373|ORANGE|BOB|XX||1078889400|4500
>
> 40149|673|ORANGE|TREY|XX||1095159900|5500
>
>
>
>
>
> I only wish to have all the FIRST(ID=40147) and LAST FIELD(PRICE=3500) to be printed for three lines as given above.
>
>
>
> I am reading as -
>
>
>
> --
>
> #include <iostream>
>
> #include <fstream>
>
> #include <sstream>
>
> #include <string>
>
> #include <boost/algorithm/string.hpp>
>
>
>
> using namespace std;
>
>
>
> struct FILE_INPUT {
>
> int ID;
>
> int Asset_ID;
>
> char T_Name[];
>
> char Symbol[];
>
> char Series[];
>
> char Gap;
>
> int Date;
>
> int price;
>
> };
>
>
>
>
>
> int main() {
>
>
>
> typedef std::map<int, int> unordmap;
>
> unordmap ask;
>
>
>
> FILE_INPUT fl_inp;
>
> memset( &fl_inp, 0, sizeof(fl_inp) );
>
> ifstream in ("input.txt");
>
> string s;
>
>
>
> const char delimiter = '|';
>
> while ( getline (in, s) ) {
>
> trim( s );
>
> if ( !s.empty() ) {
>
> stringstream strm( s );
>
> string item;
>
> getline( strm, item );
>
> stringstream stra( item );
>
> string tmp;
>
> getline( stra, tmp, delimiter );
>
> {
>
> stringstream strb( tmp );
>
> strb >> fl_inp.ID;
>
> }
>
> getline( stra, tmp, delimiter )
>
> {
>
> stringstream strb( tmp );
>
> strb >> fl_inp.price;
>
> }
>
>
>
> ask.insert( std::make_pair((fl_inp.ID),(fl_inp.price)) );
>
> unordmap::iterator pos = ask.begin();
>
> for (pos = ask.begin(); pos != ask.end(); ++pos) {
>
> std::cout << "ID: " << pos->first << "\t"
>
> << "Price: " << pos->second << std::endl;
>
> }
>
> std::cout << " " << endl;
>
> }
>
> }
>
> }
>
> --
>
>
>
> The map is being inserted with both ID as key and price as value but while printing it prints the values thrice as -
>
>
>
> ID: 40147 Price: 3500
>
>
>
> ID: 40147 Price: 4500
>
> ID: 40148 Price: 5500
>
>
>
> ID: 40147 Price: 3500
>
> ID: 40148 Price: 4500
>
> ID: 40149 Price: 5500
>
>
>
> The MAP should print all values together only once as -
>
>
>
> ID: 40147 Price: 3500
>
> ID: 40148 Price: 4500
>
> ID: 40149 Price: 5500
>
>
>
> --
>
>
>
> Can I maintain some checks to print all values simply once.
>
>
>
> Thanks.

Thanks ALL, for correcting the correct way of calling make_pair() and latter printing out the contents after the loop.




==============================================================================

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

No comments: