Обе версии следующие (следующие (
Код: Выделить всё
g.getEdges()Код: Выделить всё
bool Search::marked(const int &v) {
return std::any_of(begin(g.getEdges(v)), end(g.getEdges(v)), [this](const int &w) { return w == this->s; });
}
< /code>
bool Search::marked(const int &v) {
auto it = std::find_if(begin(g.getEdges(v)), end(g.getEdges(v)), [this](const auto &w) { return w == this->s; });
return it != end(g.getEdges(v));
}
Код: Выделить всё
class Graph {
public:
Graph() : vertex(0), edges(0) {
adj = std::make_unique(0);
}
const std::vector getEdges(int v) { return (*adj)[v]; }
// ... rest of public
private:
std::shared_ptr adj; // Shared for Search class
}
class Search {
public:
Search(Graph &g_, int s_) : g(g_), s(s_) {}
int count();
bool marked(const int &v);
private:
Graph &g;
int s;
};
< /code>
Функция gtest, которую я использовал для проверки кода: < /p>
TEST(searchClassTest, HandlesPositiveInput) {
// ... init myGraph ...
Search mySearch(myGraph, 2); // 2 is s!
EXPECT_EQ(mySearch.count(), 2);
EXPECT_EQ(mySearch.marked(1), true);
EXPECT_EQ(mySearch.marked(3), true);
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... -stdany-of
Мобильная версия