C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-13-2008, 04:34 AM   #106
Banned
 
Join Date: Nov 2007
Posts: 678
Thanks for a nice explanation CornedBeed!

Quote:
Originally Posted by CornedBee View Post
(1) Using for loops:
Code:
for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
  std::cout << *it << " bottles of beer.\n";
}
for(std::list<int>::iterator it = l.begin(); it != l.end(); ++it) {
  std::cout << *it << " red balloons.\n";
}
(2) Using for_each:
Code:
std::for_each(v.begin(), v.end(), std::cout << lambda::_1 << " bottles of beer.\n");
std::for_each(l.begin(), l.end(), std::cout << lambda::_1 << " red balloons.\n");
Can you tell me why (2) is better than (1)? (2) Requires so much understanding of underground stuff! Whereas for ( ; ; ) is known to every C++ beginner!?

Quote:
Of course, it's even nicer if you get yet more specialized, using Boost's Range and (candidate) RangeEx libraries:
Code:
rex::for_each(v, std::cout << lambda::_1 << " bottles of beer.\n");
rex::for_each(l, std::cout << lambda::_1 << " red balloons.\n");
Do you know the size of boost? I had a hard time downloading it. I was helping a friend! ()
And what help it has over the plain and simple for() loop?

All the rest you said was more like a learning to me, so I agree!
manav is offline   Reply With Quote
Old 05-13-2008, 04:44 AM   #107
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,263
Quote:
Can you tell me why (2) is better than (1)?
Personally, I find it better because of a change in thought pattern. With a loop, I am inclined to think about how the loop works. With the application of such a generic algorithm, I am inclined to think about the range that is being processed. In this case the code happens to be shorter too, but if you write out a function object separately that would not be the case.

Quote:
(2) Requires so much understanding of underground stuff! Whereas for ( ; ; ) is known to every C++ beginner!?
That should not be a concern though: the question is whether the code would be easy to understand once one understands the constructs.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-13-2008, 04:54 AM   #108
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
If you don't mind me asking, I was going to ask:
The function Find is const because it does not change the class data, but it returns a non-const iterator, allowing the data later to be changed. Am I to interpret that Find should not be const or should I circumvent this?

I get issues either way.
If it's const, then, I cannot do:
Code:
if (uint32_t(pEnd - pStart) < nFindLength) return end(); //iterator(this, m_pUnusedStart);
But if it isn't const, then, this will fail (another function):
Code:
nIndex = strToSplit.Find(strSplitWhat, nLastIndex);
strToSplit is:
Code:
const Strings::CStringEx& strToSplit
So the problem is that if I remove the const, that line won't work. I have to remove const from the reference. And that, in turns, means that I cannot pass a regular string into the function, because that won't break the const only rule for references.

Any ideas?
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-13-2008, 04:57 AM   #109
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,263
Quote:
The function Find is const because it does not change the class data, but it returns a non-const iterator, allowing the data later to be changed. Am I to interpret that Find should not be const or should I circumvent this?
I think there should be two versions of Find. One should be a const member function that returns a const iterator, the other should be a non-const member function that returns a non-const iterator.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-13-2008, 05:07 AM   #110
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
Hmmm.
Does that mean that an "internal" replace must return an integer or perhaps a pointer to the buffer instead of an iterator, then...? Seeing as I cannot call a non-const function from a const function and I cannot assign a const iterator to a non-const iterator.

One more thing, though. If the function was to be separated (ie free), then how would you go about it, since it needs an "internal" function that should not be called directly.
Code:
 template<typename StrType> void Replace(const StrType& strSrc, const StrType&
		strReplaceWhat, const StrType& strReplaceWith);
	//template<typename StrType, typename ItType> void Replace(const StrType&
		strReplaceWhat, const StrType& strReplaceWith, const ItType& vStart);
	template<typename StrType, typename ItType> void Replace(const StrType& strSrc,
		const StrType& strReplaceWhat, const StrType& strReplaceWith, const ItType& vStart, 
		const ItType& vEnd);
	// NOT TO BE CALLED DIRECTLY
	template<typename StrType, typename ItType> int32_t ReplaceInternal(const StrType&
		strSrc, const StrType& strReplaceWhat, const StrType& strReplaceWith, bool bReplace, 
		const ItType& vStart, const ItType& vEnd);
Am I just supposed to do that or go about it another way?
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-13-2008, 05:11 AM   #111
Banned
 
Join Date: Nov 2007
Posts: 678
Enough Hijacking!!!
The people here are 'Concrete Minded'! Like me!
Over to your real problem now!

Have you thought, why you are having so many problems?

You are constantly being bitten by how-to-do-it-in-C++!
Later on poor users of your class may be wondering how-to-use-it-in-my-program?

I guess, either you do not know enough C++ to do this, OR, C++ is so much of a Language, that no body, except Him, knows it well! *_*
manav is offline   Reply With Quote
Old 05-13-2008, 05:14 AM   #112
Afraid of widths
 
medievalelks's Avatar
 
Join Date: Apr 2008
Location: Chicago
Posts: 887
Quote:
Originally Posted by manav View Post
Can you tell me why (2) is better than (1)? (2) Requires so much understanding of underground stuff! Whereas for ( ; ; ) is known to every C++ beginner!?
FYI, there are some good discussions about this on comp.lang.c++.
medievalelks is offline   Reply With Quote
Old 05-13-2008, 05:15 AM   #113
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
Quote:
Originally Posted by manav View Post
Have you thought, why you are having so many problems?
You are constantly being bitten by how-to-do-it-in-C++!
Later on poor users of your class may be wondering how-to-use-it-in-my-program?
Don't be silly. I'm trying to make it as easy as possible.
For example:
Code:
void foo(std::string& mystr) { }
void foo2(const std::string& mystr) { }
int main()
{
    foo("test"); // Won't work
    foo2("test"); // Will work
}
Returning an iterator, for example, allows you to do all kinds of fun manipulations very easily.

Quote:
Originally Posted by manav View Post
I guess, either you do not know enough C++ to do this
Don't be silly. Of course I know ways around it, but I was asking for advice.

Quote:
Originally Posted by manav View Post
...OR, C++ is so much of a Language, that no body, except Him, knows it well! *_*
Screw that religion. It annoys me.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-13-2008, 05:22 AM   #114
Banned
 
Join Date: Nov 2007
Posts: 678
Quote:
Originally Posted by medievalelks View Post
As promised! Thanks, i will see that link

Quote:
Originally Posted by Elysia View Post
Don't be silly.
I was just a little concerned! Guess what, I know you know C++, hundred times more than I know! And I never had to use whatever you are using, even when it is almost a year, since I am working on this C++ project!

Quote:
Don't be silly. Of course I know ways around it, but I was asking for advice.
I was not giving you advice, I was just wondering, that, if *you* are confused, what will happen to me, when I have to code it?!?

Quote:
Screw that religion. It annoys me.
Him - Bjarne
manav is offline   Reply With Quote
Old 05-13-2008, 05:25 AM   #115
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,263
Quote:
One more thing, though. If the function was to be separated (ie free), then how would you go about it, since it needs an "internal" function that should not be called directly.
If it needs access to the internals then it should be a member or a friend. But since it does not need to be a member, it should be a friend. That said, it crosses my mind that it need not access the internals to begin with: the problem is one of finding a subrange in some range, so if you have the begin and end iterators of the range, and the begin and end iterators of the subrange, you can solve the problem. The std::search generic algorithm already solves this, so you just need to write a function to use it.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-13-2008, 05:28 AM   #116
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
Quote:
Originally Posted by manav View Post
I was just a little concerned! Guess what, I know you know C++, hundred times more than I know! And I never had to use whatever you are using, even when it is almost a year, since I am working on this C++ project!
I'll let you use a prototype once I get it in working order!
We'll see how easy you think it is to use!

Quote:
I was not giving you advice, I was just wondering, that, if *you* are confused, what will happen to me, when I have to code it?!?
Your comments don't do justice to your thoughts, really.
You will face these problems too, eventually. And you will struggle. But eventually you will learn.

Quote:
Him - Bjarne
Err OK, that really sounded unambiguous. I take back my comment, then.

Quote:
Originally Posted by laserlight View Post
If it needs access to the internals then it should be a member or a friend. But since it does not need to be a member, it should be a friend. That said, it crosses my mind that it need not access the internals to begin with: the problem is one of finding a subrange in some range, so if you have the begin and end iterators of the range, and the begin and end iterators of the subrange, you can solve the problem. The std::search generic algorithm already solves this, so you just need to write a function to use it.
I think you misunderstood that one.
The "internal" replace as you see requires more arguments (especially the bool) then the "normal" Replace. The "normal" replace comes in two variants and is supposed to be the public interface. It will do whatever is necessary and call the "internal" function.
With classes, I could hide the internal function, but if Replace is free, then I can't hide that internal function. It shouldn't be called directly, so I was wondering the best way of doing it? Should I just let it lie there or put it in a private header or so?
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-13-2008, 05:32 AM   #117
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,263
Quote:
I think you misunderstood that one.
The "internal" replace as you see requires more arguments (especially the bool) then the "normal" Replace. The "normal" replace comes in two variants and is supposed to be the public interface. It will do whatever is necessary and call the "internal" function.
Actually, I am a little confused because you were talking about Find, but now you are talking about Replace

Quote:
With classes, I could hide the internal function, but if Replace is free, then I can't hide that internal function. It shouldn't be called directly, so I was wondering the best way of doing it?
If that is the situation, then Replace should be a member function.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-13-2008, 05:36 AM   #118
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
Quote:
Originally Posted by laserlight View Post
Actually, I am a little confused because you were talking about Find, but now you are talking about Replace
Uhh ya, looking back, I can see why...
Find had the const or not const iterator problem.
Replace was broken out and have the private interface problem.

Quote:
If that is the situation, then Replace should be a member function.
I mean, currently it looks like:
Code:
 template<typename StrType> void Replace(const StrType& strSrc,
		const StrType& strReplaceWhat, const StrType& strReplaceWith)
	{
		Replace(strSrc, strReplaceWhat, strReplaceWith,
			strReplaceWhat.begin(), strReplaceWhat.end());
	}
	//template<typename StrType, typename ItType> void Replace(const
		StrType& strReplaceWhat, const StrType& strReplaceWith, const ItType& vStart);
	template<typename StrType, typename ItType> void Replace(const StrType& strSrc,
		const StrType& strReplaceWhat, const StrType& strReplaceWith, const ItType& vStart, 
		const ItType& vEnd)
	{
		int32_t nNeededSize = ReplaceInternal(strSrc, strReplaceWhat, strReplaceWith, false,
			vStart, vEnd);
		if (nNeededSize > 0) strSrc.resize(nNeededSize); //PrepareBuffer(nNeededSize);
		ReplaceInternal(strSrc, strReplaceWhat, strReplaceWith, true, vStart, vEnd);
	}

	template<typename StrType, typename ItType> int32_t ReplaceInternal(const StrType& strSrc,
		const StrType& strReplaceWhat, const StrType& strReplaceWith, bool bReplace,
		const ItType& vStart, const ItType& vEnd) 
Replace is the public interface of the free function that you should call to do the work.
Then they will wrap ReplaceInternal that will do the actual replacing.
But ReplaceInternal shouldn't be called directly because 1) it takes a bool that should not be passed directly and 2) it has only one version - the one that takes a range. Therefore ReplaceInternal should be hidden away from prying eyes, but since it's free it cannot.
But making it a member will destroy the essence of generic functions...
Unless ReplaceInternal can wrap other generic functions to do its work. Perhaps that might be a good idea? Then ReplaceInternal could be made a member.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 05-13-2008 at 05:40 AM.
Elysia is offline   Reply With Quote
Old 05-13-2008, 05:49 AM   #119
Banned
 
Join Date: Nov 2007
Posts: 678
Thinking along the lines ...
Code:
Replace(DestSeq, startDest, endDest, SrcSeq, startSrc, endSrc) ...
Pretty generic, reusable, ... can replace an Elephant with an Ant also!

// Do not mind the mildly exaggerated tone of my post, but isn't that a possible solution?
manav is offline   Reply With Quote
Old 05-13-2008, 05:51 AM   #120
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,767
No, it's not the interface per se that's the problem, but the internal mechanisms.
And it should be Replace(What, With, Start, End).
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
implementation file bejiz C++ Programming 5 11-28-2005 01:59 AM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM
Memory Problem - I think... Unregistered C Programming 4 10-24-2001 12:14 PM


All times are GMT -6. The time now is 04:06 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22