Archived
1
0
Fork 0

Replaced my custom linear search with the stl library version

This commit is contained in:
Brandon Rozek 2018-09-27 23:30:58 -04:00
parent 0bd0cf3435
commit f14d4e4bc2

View file

@ -1,5 +1,6 @@
#include <iostream>
#include <string>
#include <algorithm>
#include "environment.hpp"
#include "variable.hpp"
@ -9,11 +10,15 @@ struct Environment* create_environment(void) {
}
struct Variable* find_variable(struct Environment* env, std::string id) {
for (uint i = 0; i < size(env->vars); i++) {
if (id.compare(env->vars[i]->id) == 0) {
return env->vars[i];
}
auto result = std::find_if(env->vars.begin(), env->vars.end(),
[id](const Variable* element) {
return element->id == id;
});
if (result != env->vars.end()) {
return *result;
}
return nullptr;
}