Make by 12527 鄭竣陽
函式
可能是常識然後我是井底之蛙
#include <iostream>
using namespace std;
void print(int input) {
cout << input << '\n';
}
void print(double input) {
cout << input << '\n';
}
void print(string input) {
cout << input << '\n';
}
void print() {
cout << '\n';
}
int main() {
print();
print(15);
print(1.5);
print("16");
}
# PRESENTING CODE
#include <iostream>
using namespace std;
class Domain {
public:
Domain() {}
Domain(int sideLength, int middle):
l(middle - sideLength), r(middle + sideLength),
u(middle - sideLength), d(middle + sideLength) {}
Domain(int left, int right, int up, int down):
l(left), r(right), u(up), d(down) {}
long long getArea() { return (r - l) * (d - u); }
void print() {
cout << "l: " << l << '\n';
cout << "r: " << r << '\n';
cout << "u: " << u << '\n';
cout << "d: " << d << '\n';
}
private:
int l = 0;
int r = 0;
int u = 0;
int d = 0;
};
int main() {
Domain defaultDomain;
Domain squareDomain(12, 5);
Domain rectangleDomain(5, 13, 2, 12);
Domain* declareWithNew = new Domain(2, 3);
cout << defaultDomain.getArea();
cout << squareDomain.getArea();
rectangleDomain.print();
declareWithNew->print();
}
# PRESENTING CODE
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
struct Temp {
Temp(int v): value(v) {}
Temp(int v, int p): value(v), priority(p) {}
~Temp() { cout << "deleted object with priority " << this->priority << '\n'; }
int value;
int priority = 0;
};
int main() {
Temp(5, INT32_MAX);
if (true) {
vector<Temp> myList;
for (int i = 10; i > 0; i--) myList.push_back(Temp(5, i));
cout << "done\n";
}
Temp current(15, 0);
cout << current.value << '\n';
cout << "hello world!\n";
}
# PRESENTING CODE
進入正題
[]() mutable throw() -> TYPE {
doSomething();
return aTYPEObject;
}
[&] [=]
[&, a] [=, &a]
[a, &b]
[]
(int &x)
(string name, int age)
(auto output)
(vector<int> v)
#include <iostream>
using namespace std;
int main() {
[]{ cout << "hello world!\n"; }();
}
#include <iostream>
using namespace std;
int main() {
auto sayHi = []{ cout << "hello world!\n"; };
sayHi();
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
auto initializeVector = [&](int length, int value) {
v.resize(length);
for (int& n: v) n = value++;
};
initializeVector(4, 6);
for (int& n: v) cout << n << ' ';
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v{60,12,30,21,71,50,48,90,87,69,47};
int standard;
cin >> standard;
stable_partition(v.begin(), v.end(), [&](int x) {
return x < standard;
});
for (int& n: v) cout << n << ' ';
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void quicksort(vector<int>::iterator head, vector<int>::iterator tail) {
if (tail - head <= 1) return;
int pivot = *head;
auto mid = stable_partition(head, tail, [&](int x) {
return x < pivot;
});
quicksort(head, mid);
quicksort(mid + 1, tail);
}
int main() {
vector<int> v{60,12,30,21,71,50,48,90,87,69,47};
quicksort(v.begin(), v.end());
for (int& n: v) cout << n << ' ';
}
#include <iostream>
using namespace std;
int main() {
auto print = [](auto x) -> void {
cout << x << '\n';
};
print("hello");
print(420.69);
print(true);
}
#include <iostream>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
auto sum = [](auto a, auto b) {
return a + b;
};
cout << sum(a, b);
}
#include <iostream>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
int o = [&, n]() mutable { return ++m + ++n; }();
cout << m << ", " << n << ", " << o;
}
一些酷酷的知識
所以為什麼要叫這個名字?
感謝聆聽~