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
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 previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior 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

 
Read Message
Read Message
Read Message
Read Message
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: Thu Mar 28 09:53:15 CET 2024

Total time taken to generate the page: 0.01198 seconds