这里主要是对C++11比较重要的一些的东西的使用进行讲解和如何去使用。 C++11
#include <iostream>
#include <string>
#include <vector>
#include <complex>
#include <set>
using namespace std;
double f() { return 1.0; }
auto d = f();
void test01()
{
auto i = 42;
vector<string>v;
v.push_back("123456");
auto pos = v.begin();
auto l = [](int x)->bool {
cout << "输入的参数为" << x << endl;
cout << "lamuda表达式" << endl;
return true;
};
{
cout << i << endl;
cout << d << endl;
cout << *pos << endl;
cout << l(1) << endl;
}
}
class Test02
{
public:
int values[3]{ 1,2,3 };
std::vector<int>v{ 2,3,4,5,8 };
std::vector<std::string>cities{
"Berlin","New York","London","Cairo","Cologne"
};
std::complex<double>c{ 4.0,3.0 };
int i;
int j{};
int* p;
int* q{};
void test() {
cout << "i的值:" << i << endl;
cout << "j的值:" << j << endl;
cout << "p的值:" << p << endl;
cout << "q的值:" << q << endl;
}
char c1{ 7 };
std::vector<int>v1{ 1,2,3,4,5 };
void print(std::initializer_list<int>vals)
{
for (auto p = vals.begin(); p != vals.end(); ++p) {
cout << *p << "\n";
}
}
};
void test02()
{
Test02 test;
test.test();
test.print({ 12,3,5,7,11,13,17 });
}
void test03()
{
for (int i : {2, 3, 5, 7, 9, 13, 17, 19})
{
cout << i << endl;
}
std::vector<double>vec{ 2.3,5.5,2.6,4.2,3.6 };
for (auto& elem : vec)
{
elem *= 3;
}
}
template<class T>
void printElements(const T& coll)
{
for (const auto& elem : coll)
{
std::cout << elem << std::endl;
}
}
template<class X>
void createAndInsert(std::set<X>& coll)
{
X x;
coll.insert(x);
coll.insert(std::move(x));
}
void test05()
{
string a = "\\\\n";
string b = R"(\\n)";
string c = R"nc(\dfg dfj\fd p90()(000))()())nc";
cout << u8"hello" << endl;
cout << u"hello" << endl;
cout << U"hello" << endl;
cout << L"hello" << endl;
}
void foo() noexcept;
constexpr int square(int x)
{
return x * x;
}
void test07()
{
float a[square(9)];
cout << sizeof(a) / sizeof(float) << endl;
}
void test09()
{
[] {cout << "hello lambda" << endl; }();
auto l = [] {cout << "hello lambda2" << endl; };
l();
char16_t q='a';
cout << sizeof(q) << endl;
}
int main()
{
test09();
}
已经分小点测试了,需要学习的直接拿走,自己动手实践一下就能够加深印象和理解的,希望都不要觉得C++难。
|