#include<algorithm> #include<vector> #include<iostream> #include"point.h" using namespace std; using namespace bme; static void step(const Point& p) { static Point start = Point(0., 0.); cout << p-start << endl; start = p; } int main() { vector<Point> v = { Point(1.,2.), Point(3.,6.), Point(-1.,2.)}; for_each(v.begin(), v.end(), step); return 0; }
void eraseVals(int bound, list<int>& ls) { ls.remove_if([bound](int x) { return x > bound; }); }
#include <iostream> using namespace std; int main() { []() {cout << "Hello" << endl; }(); int x = [](int x, int y) {return x + y; } (10, 5); [x](int y) {cout << "Sum: " << (x += y) << endl; }(5); }
#include <iostream> using namespace std; int main() { []() {cout << "Hello" << endl; }(); int x = [](int x, int y) {return x + y; } (10, 5); [&x](int y) {cout << "Sum: " << (x += y) << endl; }(5); }
#include <iostream> using namespace std; int main() { []() {cout << "Hello" << endl; }(); int x = [](int x, int y) {return x + y; } (10, 5); [x](int y) mutable {cout << "Sum: " << (x += y) << endl; }(5); }
#include <iostream> using namespace std; int main() { []() {cout << "Hello" << endl; }(); int x = [](int x, int y) {return x + y; } (10, 5); [x](int y) {cout << "Sum: " << (x + y) << endl; }(5); }
#include<map> #include<iostream> using namespace std; static int recur(int n) { static map<int, int> lookup; if (n <= 0) { lookup.clear(); return 0; } if (n == 1) { return 3; } if (lookup.count(n) > 0) { return lookup[n]; } int ans = 1; for (int k = 1; k <= n / 2; k++) { if (n % k == 0) { ans *= recur(k) + (k % 2 == 0 ? 1 : -1); } } return lookup[n] = ans; } int main() { for (int i = 1; i <= 32; ++i) { cout << "a(" << i << ") = " << recur(i) << endl; } return 0; }
#ifndef GRAPH_H #define GRAPH_H #include <map> #include <list> using namespace std; namespace bme { class Graph; class Edge { private: unsigned int vfrom; unsigned int vto; public: Edge(unsigned int _vfrom, unsigned int _vto) : vfrom(_vfrom), vto(_vto) {} unsigned int vFrom() const { return vfrom; } unsigned int vTo() const { return vto; } bool isLoop()const { return vfrom == vto; } bool operator<(const Edge& other) const; bool operator==(const Edge& other) const; friend Graph; }; class Vertex { private: unsigned int label; list<Edge> edges; public: Vertex(unsigned int _label) : label(_label) {} Vertex() : label(0) {} unsigned int getLabel()const { return label; } int nNeighbors()const { return edges.size(); } const list<Edge>& edgeList() const{ return edges; } friend Graph; }; class Graph { private: unsigned int nextlabel; map<unsigned int, Vertex> vertices; public: Graph(unsigned int n); void addEdge(unsigned int u, unsigned int v); void removeEdge(unsigned int u, unsigned int v); unsigned int addVertex(); void removeVertex(unsigned int v); int vertexCount() const; int edgeCount() const; const list<Edge>& neighbors(unsigned int label) const; bool areConnected(unsigned int v1, unsigned int v2) const; int degree(unsigned int v) const; bool isSimple() const; void simplify(); unsigned int nextLabel()const { return nextlabel; } }; inline bool isLoop(const Edge& e) { return e.isLoop(); } inline bool Edge::operator<(const Edge& other) const { if (vfrom < other.vFrom()) { return true; } else if (vfrom == other.vFrom() && vto < other.vTo()) { return true; } else { return false; } } inline bool Edge::operator==(const Edge& other) const { return vfrom == other.vFrom() && vto == other.vTo(); } } #endif
graph.cpp
#include "graph.h" #include <vector> #include <map> #include <list> using namespace std; namespace bme { Graph::Graph(unsigned int n) { for (unsigned int i = 0; i < n; ++i) { vertices[i] = Vertex(i); } nextlabel = n; } void Graph::addEdge(unsigned int u, unsigned int v) { if (vertices.count(u) > 0 && vertices.count(v) > 0) { vertices[u].edges.push_back(Edge(u, v)); vertices[v].edges.push_back(Edge(v, u)); //hurokéleket duplán tárolunk (szándékosan) } } void Graph::removeEdge(unsigned int u, unsigned int v) { if (vertices.count(u) == 0 || vertices.count(v) == 0) { return; } vertices[u].edges.remove_if([v](const Edge& e) {return e.vto == v; }); if (u!=v) { vertices[v].edges.remove_if([u](const Edge& e) {return e.vto == u; }); } } unsigned int Graph::addVertex() { vertices[nextlabel] = Vertex(nextlabel); return nextlabel++; } void Graph::removeVertex(unsigned int v) { if (vertices.count(v) == 0) { return; } for (Edge& e : vertices[v].edges) { if (!e.isLoop()) { list<Edge>& li_other = vertices[e.vTo()].edges; li_other.remove_if([v](const Edge& _e) { return _e.vTo() == v; }); } } vertices.erase(v); } int Graph::vertexCount() const { return vertices.size(); } int Graph::edgeCount() const { int count = 0; for (unsigned int i = 0; i < nextlabel; ++i) { if (vertices.count(i) != 0) { count += vertices.at(i).nNeighbors(); } } return count / 2; } const list<Edge>& Graph::neighbors(unsigned int label) const { return vertices.at(label).edgeList(); } bool Graph::areConnected(unsigned int v1, unsigned int v2) const { if (vertices.count(v1) == 0 || vertices.count(v2) == 0) { return false; } for (const Edge& e : vertices.at(v1).edgeList()) { if (e.vTo() == v2) { return true; } } return false; } int Graph::degree(unsigned int v) const { if (vertices.count(v) == 0) { return -1; } return vertices.at(v).nNeighbors(); } bool Graph::isSimple() const { for (unsigned int v = 0; v < nextlabel; ++v) { if (vertices.count(v) == 0) { continue; } vector<bool> seen(nextlabel, false); for (const Edge& neighbor : vertices.at(v).edges) { if (neighbor.isLoop() || seen[neighbor.vTo()]) { return false; } seen[neighbor.vTo()] = true; } } return true; } void Graph::simplify() { for (unsigned int v = 0; v < nextlabel; ++v) { if (vertices.count(v) == 0) { continue; } list<Edge>& neighbors = vertices[v].edges; neighbors.sort(); neighbors.unique(); neighbors.remove_if(isLoop); } } }
Egy teszt:
#include<iostream> #include "graph.h" using namespace std; int main() { bme::Graph g(5); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 1); g.addEdge(3, 4); g.addEdge(4, 0); g.addEdge(1, 1); g.removeEdge(1, 2); cout << "Csucsok szama: " << g.vertexCount() << "\n"; cout << "Elek szama: " << g.edgeCount() << "\n"; cout << "Egyszeru graf-e: " << (g.isSimple() ? "Igen" : "Nem") << "\n"; g.simplify(); cout << "Egyszerusites utan egyszeru graf-e: " << (g.isSimple() ? "Igen" : "Nem") << "\n"; cout << "Csucsok szama: " << g.vertexCount() << "\n"; cout << "Elek szama: " << g.edgeCount() << "\n"; return 0; }