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 » Community » Newbie corner » Upp::Vector FindIndex by value of Object
Upp::Vector FindIndex by value of Object [message #52553] Tue, 22 October 2019 11:12 Go to next message
Xemuth is currently offline  Xemuth
Messages: 127
Registered: August 2018
Location: France
Experienced Member
Hello community,

I'm looking for something like this :
	Vector<String> m;
	String& find = m.GetIndex([](String toFind,String inMyVector){return toFind.IsEqual(inMyVector);});

It allow me to retrieve index of object by calling lambda function.
Do it exist ?

Thanks in advance.
Best regards


Re: Upp::Vector FindIndex by value of Object [message #52598 is a reply to message #52553] Fri, 25 October 2019 11:01 Go to previous messageGo to next message
Xemuth is currently offline  Xemuth
Messages: 127
Registered: August 2018
Location: France
Experienced Member
Here is how I did it
#include <Core/Core.h>

using namespace Upp;


template <class T>
T& FindOne(Vector<T>& data, bool(predicate)(const T& t2)){
	for(T& t : data){
		if(predicat(t)) return t;
	}
	throw std::exception();
}

template <class T>
Vector<T> Find(Vector<T>& data, bool(predicate)(const T& t2)){
	Vector<T> buffer;
	for(T& t : data){
		if(predicat(t)) buffer.Create(t);
	}
	return buffer;
}

CONSOLE_APP_MAIN
{
	Vector<String> allStrings{"one","two","three","four","five","six","seven","eight","nine"};	
	try{
		String& theStringFinded=FindOne<String>(allStrings,[](const String& s1){
			return s1.IsEqual("six");
		});
		Cout() << theStringFinded <<"\n"; //six
		
		auto result = Find<String>(allStrings,[](const String& s1){
			return (s1[0] == 'f');
		});
		
		for(const String& res : result){
			Cout() << res << EOL;	 //four;five
		}
	}catch(...){
		Cout() << "Error" << EOL;
		return;	
	}
}


[Updated on: Fri, 25 October 2019 11:27]

Report message to a moderator

Re: Upp::Vector FindIndex by value of Object [message #52626 is a reply to message #52553] Thu, 31 October 2019 19:43 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12105
Registered: November 2005
Ultimate Member
Xemuth wrote on Tue, 22 October 2019 11:12
Hello community,

I'm looking for something like this :
	Vector<String> m;
	String& find = m.GetIndex([](String toFind,String inMyVector){return toFind.IsEqual(inMyVector);});

It allow me to retrieve index of object by calling lambda function.
Do it exist ?


int i = FindIndex(m, toFind);

Mirek
Re: Upp::Vector FindIndex by value of Object [message #52654 is a reply to message #52553] Thu, 07 November 2019 13:55 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 127
Registered: August 2018
Location: France
Experienced Member
Thanks

Previous Topic: [solved]An U++ equivalent of bzero ? (if not a sin)
Next Topic: How to include files .h or .cpp as external library?
Goto Forum:
  


Current Time: Tue Nov 19 06:12:14 CET 2019

Total time taken to generate the page: 0.03756 seconds