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   |
 |
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
|
|
|
|
|
Goto Forum:
Current Time: Tue May 13 14:37:33 CEST 2025
Total time taken to generate the page: 0.00513 seconds
|