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