Replaced my custom linear search with the stl library version
This commit is contained in:
parent
0bd0cf3435
commit
f14d4e4bc2
1 changed files with 9 additions and 4 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue