Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Small fix for Find in Algo.h
Small fix for Find in Algo.h [message #20327] Wed, 11 March 2009 18:38 Go to next message
gridem is currently offline  gridem
Messages: 45
Registered: August 2008
Member
I think that this patch solves the problem for Find function (it should compare the values):
 template <class T, class V>
 T Find(T ptr, T end, const V& value)
 {
-	return Find(ptr, end, value, StdEqual<T>());
+	return Find(ptr, end, value, StdEqual<V>());
 }
Re: Small fix for Find in Algo.h [message #20328 is a reply to message #20327] Wed, 11 March 2009 21:15 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Thanks!

Mirek
icon13.gif  BUG for Find in Algo.h [message #34776 is a reply to message #20328] Mon, 12 December 2011 11:14 Go to previous messageGo to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Hello,

I have a little bug when i use find algorithm. Here is my test :
#include <Core/Core.h>

using namespace Upp;

class CAppli:Moveable<CAppli>
{
	public:
		
		String m_appli;
                int m_count;
		
		CAppli(const String& appli)
		{
			m_appli = appli; m_count = 0;
		}
		
		String ToString()
		{
			String mycout;
			
			mycout << " APPLI : " << m_appli;
			
			return 	mycout;
		}

		void update()
		{
 		   m_count++;
		}
		
};

bool operator== (const CAppli& oAppli, const CAppli& oAppliTest)
{
	Cout() << "\n    -> test : " << oAppliTest.m_appli << " =? " << oAppli.m_appli;
		
 	if( oAppli.m_appli ==  oAppliTest.m_appli)
 	{
 		Cout() << "\n    -> result : " << oAppliTest.m_appli << " is equal to " << oAppli.m_appli;
 		return true;
 	}
 	else 
 	{
 		Cout() << "\n    -> result : " << oAppliTest.m_appli << " is different of " << oAppli.m_appli;
 		return false; 
 	}
}

class CJSON: public Json
{
	public :
	
		Vector<CAppli> vAppli;
		
		CJSON operator<< (const CAppli& oAppli)
		{
			Cout() << "\nTry to add " << oAppli.m_appli;
			
			Vector<CAppli>::Iterator it;
			it = Find(vAppli.Begin(), vAppli.End(), oAppli);
			
			if(it != vAppli.End())
			{			
				//CAppli apptmp = *it;              //<---- IT'S BUG !!!!!!!!
				//apptmp.update();
				//Cout() << "\n  <- Why I fall here even if " << apptmp.m_appli << " is different of " << oAppli.m_appli;
				
				Cout() << "\n  <- Why I fall here?";
			}			
			else
			{
				Cout() << " not yet in the vector. ";
				
				vAppli.Add(oAppli);
				Cout() << oAppli.m_appli << " added";
				
			}
			
			Cout() << "\n-----------\n\n";
			
			return (*this);
		}
};


CONSOLE_APP_MAIN
{
	CAppli appl1("APP1"), appl2("APP2"), appl3("APP3");
	
	CJSON ojson;	
	ojson << appl1 << appl2 << appl3 << appl2;
}


Thanks for your analyse and reply

Best regards,

Ratah

[Updated on: Mon, 12 December 2011 11:20]

Report message to a moderator

Re: BUG for Find in Algo.h [message #34813 is a reply to message #34776] Tue, 13 December 2011 17:16 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
U++ iterators are allowed to be assigned NULL, so it makes more sense to return NULL in 'fail' case by U++ algos.

It is written somewhere in doc, but perhaps should have been emphasized more...

So:

			if(it)
			{			
				//CAppli apptmp = *it;              //<---- IT'S BUG !!!!!!!!
				//apptmp.update();
				//Cout() << "\n  <- Why I fall here even if " << apptmp.m_appli << " is different of " << oAppli.m_appli;
				
				Cout() << "\n  <- Why I fall here?";
			}			


Notes:

U++ prefers index notation, so FindIndex(...) < 0 would be more 'U++ way'.

And based on the name of class, U++ now has pretty good JSON support:

http://www.ultimatepp.org/reference$JSON$en-us.html

Mirek
Re: BUG for Find in Algo.h [message #34942 is a reply to message #34813] Mon, 19 December 2011 09:26 Go to previous messageGo to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Thanks a lot
Re: BUG for Find in Algo.h [message #34943 is a reply to message #34776] Mon, 19 December 2011 10:10 Go to previous message
gridem is currently offline  gridem
Messages: 45
Registered: August 2008
Member
There are 2 issues:

1. You should check iterator for NULL. This is difference between STL and U++.

2. You should use reference instead of value:


The correct code is:
			if (it != NULL)
			{			
				CAppli& apptmp = *it;              // use reference
				apptmp.update();
Previous Topic: A probably simple question about pick semantics
Next Topic: Preferred way to access VectorMap
Goto Forum:
  


Current Time: Thu Mar 28 12:55:18 CET 2024

Total time taken to generate the page: 0.01726 seconds