lambda 表达式(匿名函数)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void Func02()
{
int a=10,b=20,c=3;
[]()->int { cout << "这是一个lambda表达式" << endl; };
[]{};
[=]{ cout << a << endl; int number = 156; number = 9; };
[=,&b] {cout << a << endl; int number = 156; number = 9;};
[&]{cout << a << endl; int number = 156; number = 9;};
[&,c] {cout << c << endl; };
[](int a, int b) { cout << "函数体" << endl; };
[](int a, bool b=true ) { cout << "函数体" << endl; };
[]()->int {return 0;};
[](int i)->int {return i;};
auto lambda = [=] {cout << a << endl; };
lambda();
cout << "~~~~~~~~~~~" << endl;
auto lam1 = [](int i = 9)->int { cout << "aa" << endl; return i;};
lam1(10);
cout << "~~~~~~~~~~~" << endl;
}
auto Function01 = ([]{
std::cout << "Hello World!" << std::endl;
}
);
int main() {
Func02();
Function01();
}
STL编程:容器 迭代器(遍历元素)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void PrintInt(int value);
void Func01()
{
vector<int> numbers;
numbers.push_back(4);
numbers.push_back(5);
numbers.push_back(0);
numbers.push_back(12);
numbers.push_back(78);
numbers.push_back(1);
numbers.push_back(6);
numbers.push_back(8);
numbers.push_back(12);
cout << "容器中存放了:" << numbers.size() << endl;
cout << numbers[0] << endl;
cout << numbers.at(0) << endl;
for (unsigned int i = 0; i < numbers.size(); i++) {
cout << "第" << i+1 << "个元素:" << numbers.at(i) << endl;
}
vector<int>::iterator itBegin = numbers.begin();
vector<int>::iterator itEnd = numbers.end();
for (vector<int>::iterator itBegin = numbers.begin(); itBegin != numbers.end(); itBegin++) {
cout << *itBegin << ",";
}
cout << endl << "============" << endl;
for_each(numbers.begin(),numbers.end(),PrintInt);
cout << endl << "================" << endl;
sort(numbers.begin(),numbers.end());
for_each(numbers.begin(),numbers.end(),PrintInt);
cout << endl << "================" << endl;
}
int main() {
Func01();
}
void PrintInt(int value) {
cout << value << ",";
}