|
What's the best way to stream a container?
inline std::ostream& operator<<(std::ostream& out, const MyClass& s)
{
return out << s.m_SomeVar;
}
MyClass myClass;
std::cout << myClass;
how do I do the same but with a vector of MyClass?
std::vector<MyClass> myClasses;
std::cout << myClasses;
use for_each maybe?
std::for_each(m_PopProfiles.begin(), m_PopProfiles.end(), Something<std::cout>()); ??
Todd Smith
|
|
|
|
|
I found this on a web site. I am sure someone with a clue can post a better solution:
copy (l.begin(), l.end(), ostream_iterator(cout, ""));
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Just what I needed. Thanks!
Todd Smith
|
|
|
|
|
Hi.
I would like to debug a simple algorithm that does not work using an STL solution. The solution works via iteration.
std::list<unsigned int=""> theList;
for (unsigned int i = 0; i < 10; ++i)
theList.push_back(i);
// theList should now contain 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
// Remove "6" from the list.
// Assume that iTheList is an iterator that points to element with value 6.
theList.erase(iTheList);
// After erase() theList should now contain 0, 1, 2, 3, 4, 5, 7, 8, and 9.
Okay. Everything above works as designed. Now I would like to decrement everything bigger or equal to "6," thus make theList hold 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Here is the iterative solution.
-----
// Given iTheList points to theList.begin().
while (*iTheList <= 6)
++iTheList
// Now decrement until end of theList.
while (*iTheList != theList.end()
{
*iTheList = *iTheList - 1;
++iTheList;
}
-----
The iterative solution above works perfect.
I would like to implement a more efficient and elegant solution. Here is one solution using STL algorithms. However, it does not work correctly.
-----
std::for_each(std::find_if(theList.begin(), theList.end(), std::bind2nd(std::greater<>, 6)), theList.end(), std::bind2nd(std::minus<>, 1));
-----
The STL solution above does not work. The logic and syntec seem to be valid.
Is there a logic or syntec misunderstanding in the STL solution? I looked over the function objects and function adapter. They are valid.
Thanks,
KUphryn
|
|
|
|
|
Why are you trying to replace simple code with a god awful stream of unreadable syntax that you have to work hard to actually get to work? That isn't programming, that is STL masturbation.
Stick with the simple solution. Of course, these days that isn't the politically correct answer.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I see your point.
Kuphryn
|
|
|
|
|
The problem is your find_if
What will find_if return in the following case? It will return an interator pointing to the beginning of the list which is the same as l.begin(). So it pretty much infinite loops.
std::find_if(l.begin(), l.end(), std::bind2nd(std::less<int>(), 100));
<b>Todd Smith</b>
|
|
|
|
|
Okay. Thanks.
Fruny posted a solution at GameDev using transform().
http://www.gamedev.net/community/fo...topic_id=120974
Kuphryn
|
|
|
|
|
|
hahahahah
Yeah, I believe the STL is exceptionally powerful if the software engineer knows it well.
Kuphryn
|
|
|
|
|
I'll be a good boy and avoid more of my anti-STL rant. But I really do like the containers. Well, mostly even though I have found the ATL7 containers to produce smaller code with less syntactical mess.
But yes, I do like STL containers. I think...
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Tim Smith wrote:
But yes, I do like STL containers. I think...
Yes you do, because you have realized the power of the dark side. Muhahahaha.
--
Master, I'm so glad to feel your presence. But you don't seem to share my impatience. I relied upon you to break the silence. I cannot understand your reluctance.
Master, I feel so warm and I'm so happy, oh master.
Give me some more of the warm little beasts
I'm so fond of.
|
|
|
|
|
Since I am always interested in finding out if the STL method actually performs better, I love to test this stuff. There have been many cases where STL is faster. But this is another of many more cases where the "right" way isn't the faster way.
Base line, add ten elements and then clear: 40,813 MS
STL Method supplied by Funny and then clear: 40,781 MS
The "EVIL" hand written loop and then clear: 39,547 MS
At least this wasn't as bad as Scott Meyer's example in his "Why STL loops are better than hand written loops" article where HIS example ran 3 times slower than the handwritten loop.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I left some extra code in the base line test. The real numbers are:
Base line, add ten elements and then clear: 38,843 MS
STL Method supplied by Funny and then clear: 40,157 MS
The "EVIL" hand written loop and then clear: 39,281 MS
These numbers seemed to be stable between multiple tests. The relationship was always maintained.
STL way added 1,314ms to the total time.
Hand written loop added 438ms to the total time.
Which means that the STL way is 3 times slower than the hand written loop. Even if you toss in 100 ms here and there for timing error, STL is still much slower.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Interesting. Thanks.
You have to admit the STL solution requires half the lines of code.
Kuphryn
|
|
|
|
|
Oh no argument there at all. But to me it all comes down to the trade-offs.
For me, the number of lines of code has never been an issue because it doesn't reflect on the complexity of the code. In this case, the STL version is very basic and easy to understand. So I give it high maintainability marks. However, I have seen many "1 line" STL statements that take 2 days to decode.
I just don't buy into this line that STL produces the "fastest" implementation. It just can't. STL produces the "most generic" implementation which might be fast. In my tests, STL tends to be much slower than doing things the old method. I have also seen little or no evidence that STL functors or algorithms reduce bugs.
If anything, I am here as a devils advocate. I want people to use STL for the right reasons. I use it myself and love it. But lately I have seen way too many people just reading from the "STL Talking Points" memo without ever questioning if the stuff is true.
I am in no way trying to say you are wrong to want to use STL's advanced features. I am just here to place a little seed of doubt so maybe we can add more people to the seemingly short list of people who question some of what is coming out of the standards committee.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I am all eye, ear, and mind.
Kuphryn
|
|
|
|
|
Hi, I have a project in which I want to have multiple objects (in different VC projects) implement the same interface. Some of the code in my projects is already using the wonderful embedded IDL. The problem is, I don't seem to be able to add a standalone, project-wide .idl file and have it compiled too.
I'd hoped for some way for these interfaces to make it into the .tlb file but they are just ignored. Can you really not mix the embedded IDL and hard-coded IDL files? It seems crazy for my other projects to rely on an auto-generated file!
thanks in advance,
--
Simon Steele
Programmers Notepad - http://www.pnotepad.org/
|
|
|
|
|
Found the solution under "attributed programming" in MSDN:
[ importidl(idlfile) ];
or similar.
--
Simon Steele
Programmers Notepad - http://www.pnotepad.org/
|
|
|
|
|
Hi all.
I was reading WTL documentation: they say that a simple SDI application EXE takes 24 Kb if compiled with some NOWIN98 option.
Does this mean that the program runs on Win2000/XP only ?
Isn't it a limitation ?
The exe generated from my build (with default settings) takes 96 Kb.
Is it the normal size ?
Thanks in advance.
|
|
|
|
|
This is a linker option, which aligns chunks of the file on 4K boundaries by default, or on 512 byte boundaries with the /OPT:NOWIN98.
The larger file loads faster on Win98 with less swapping, according to Q235956, but I think the smaller one still runs.
Is your build a Debug or Release build? If Debug, then the file contains extra stuff.
Steve S
[This signature space available for rent]
|
|
|
|
|
I meant a Release build. I compiled the project with the default settings, as I downloaded it (only Release change, of course).
So the program will still run on Win98, won't it ?
The linker option is not restricted to WTL project, isn't it ?
|
|
|
|
|
puzzolino wrote:
So the program will still run on Win98, won't it ?
Yes it will still run.
puzzolino wrote:
The linker option is not restricted to WTL project, isn't it ?
Correct, you can just add the linker option in your linker options for your project settings. WTL does not change anything about the compiler or environment, it is simply a set of template classes to write windows programs in.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Paul Watt (kilowatt) wrote:
Correct, you can just add the linker option in your linker options for your project settings. WTL does not change anything about the compiler or environment, it is simply a set of template classes to write windows programs in.
Of course, but why they compare MFC program size and WTL's with this option ?
Shouldn't they have applied this option to both projects before comparing ?
Mah...
Maybe they just wanted to upraise the latter....;P
|
|
|
|
|
This is the code from Dinkumware STL hash_map. This is also the same code in VC++.NET hash_map class.
This function gets called for every lookup. I find that the incoming key is compared with the object bothways. I mean
The comp function returns true for greater than . So the comparison is:
if a not greater than b
if b not greater than a,
then both are equal, return value.
Why not test for equality directly than go for this method? Is there any specific reason? Now, the string comparison is done twice to check for equality. The code does not seem to be in the overridable part of the hash_map.
Question: Is there any work around to prevent the comparison from running twice?
Note: I checked the CMapStringToString in MFC and it does just one comparison.
<br />
iterator lower_bound(const key_type& _Keyval)<br />
{ <br />
size_type _Bucket = _Hashval(_Keyval);<br />
iterator _Where;<br />
for (_Where = _Vec[_Bucket]; _Where != _Vec[_Bucket + 1]; ++_Where)<br />
if (!this->comp(this->_Kfn(*_Where), _Keyval))<br />
return (this->comp(_Keyval,<br />
this->_Kfn(*_Where)) ? end() : _Where);<br />
return (end());<br />
}<br />
modified 29-Aug-18 21:01pm.
|
|
|
|
|