C++?????????????10??C++11????
???????????? ???????[ 2014/11/26 11:37:42 ] ????????C++ ????? ???????
???????????
????C++11?????for?????????????????????“foreach”??????????μ???????????C???????顢??????б?????κ??????????????begin()??end()??????????
????This for each for is useful when you just want to get and do something with the elements of a collection/array and don't care about indexes?? iterators or number of elements.
?????????????????????е??????????????????????????е???????????????????????????????????for??????????????
1 std::map<std::string?? std::vector<int> > map;
2 std::vector<int> v;
3 v.push_back(1);
4 v.push_back(2);
5 v.push_back(3);
6 map["one"] = v;
7
8 for(const auto& kvp : map)
9 {
10 std::cout << kvp.first << std::endl;
11
12 for(auto v : kvp.second)
13 {
14 std::cout << v << std::endl;
15 }
16
17 }
18
19 int arr[] = {1??2??3??4??5};
20 for(int& e : arr)
21 {
22 e = e*e;
23 }
????override??final???
????????????C++???????????????????У????????У??????????????????????????????е???д?????virtual??????????????????????????????????????????ж??????????????????????????????????????????????????????virtual???????????????????????????????????Щ?????????????????????
????1 class B
????2 {
????3 public:
????4 virtual void f(short) {std::cout << "B::f" << std::endl;}
????5 };
????6
????7 class D : public B
????8 {
????9 public:
????10 virtual void f(int){std::cout << "D::f" << std::endl;}
????11 };
????D::f??????дB::f???????????????????????short?????????int???????????????????B::f??????????????????????????????????????????????б???д????????????????B????????f()???????D::f??????????????B::f??
????Here is another subtle error: the parameters are the same?? but the method in the base class is marked const?? while method in the derived is not.
??????????????????????????????????????и÷?????????const??????????????б???
????1 class B
????2 {
????3 public:
????4 virtual void f(int) const {std::cout << "B::f" << std::endl;}
????5 };
????6
????7 class D : public B
????8 {
????9 public:
????10 virtual void f(int) {std::cout << "D::f" << std::endl;}
????11 };
??????????????????????????????????д?????????????B????????f()???????B::f??????D::f??
???????????????????????C++11??????????????????????????override??final??override?????????????????????????????д??final???????????е??????????д????????????????????£?
????1 class B
????2 {
????3 public:
????4 virtual void f(short) {std::cout << "B::f" << std::endl;}
????5 };
????6
????7 class D : public B
????8 {
????9 public:
????10 virtual void f(int) override {std::cout << "D::f" << std::endl;}
????11 };
?????????е?д???????????????????????const??????????????override??????????????????????
????“ 'D::f': ??????д?????'override'??????????д?κλ????? ”
??????????????????????????????????д?????????????????????????ж????????д???????final?????????????????????final??????????κ????????????final?????????м????????override???????????????final???????
????1 class B
????2 {
????3 public:
????4 virtual void f(int) {std::cout << "B::f" << std::endl;}
????5 };
????6
????7 class D : public B
????8 {
????9 public:
????10 virtual void f(int) override final {std::cout << "D::f" << std::endl;}
????11 };
????12
????13 class F : public D
????14 {
????15 public:
????16 virtual void f(int) override {std::cout << "F::f" << std::endl;}
????17 };
???????????????'final'??????????F::f??д??
?????????????????
????C++?д??????????????Щ???????????????????Χ???У???????????????????????ж?????????????????????????????????????????????????????????????????????????????????
??????Щ??????C++11?е????????C++11???????μ???????????????????????????????????“enum class”??????????????????????????Χ???У???????????????????????????????????????????????????????????????????У???
????1 enum class Options {None?? One?? All};
????2 Options o = Options::All;
???????????
???????к??????????????????????????????????????????ü?????????????????????棺
????unique_ptr:?????????????????????????????????и????????????????????????????????unique_ptr????????????????????
????shared_ptr:???????????????????????????????????????
????weak_ptr:??????????shared_ptr?????????????????????????????????ü????????????????????????????????????????????????????ж???????????shared_ptr?????????????????ж???????????????????????????shared_ptr?????????????????????????????????????????
????auto_ptr???????????????????????????????unique_ptr????????????????????????shared_ptr???????????????????????
??????????????????unique_ptr???÷??????????????????????????????????unique_ptr?????????std::move??????????????????????????????????????????????????????????????????get()???????????nullptr??
????1 void foo(int* p)
????2 {
????3 std::cout << *p << std::endl;
????4 }
????5 std::unique_ptr<int> p1(new int(42));
????6 std::unique_ptr<int> p2 = std::move(p1); // transfer ownership
????7
????8 if(p1)
????9 foo(p1.get());
????10
????11 (*p2)++;
????12
????13 if(p2)
????14 foo(p2.get());
??????????????????shared_ptr???÷?????unique_ptr???÷???????????????????壺shared_ptr??????????????
????1 void foo(int* p)
????2 {
????3 }
????4 void bar(std::shared_ptr<int> p)
????5 {
????6 ++(*p);
????7 }
????8 std::shared_ptr<int> p1(new int(42));
????9 std::shared_ptr<int> p2 = p1;
????10
????11 bar(p1);
????12 foo(p2.get());
????????????????p1???????????????????????
????auto p3 = std::make_shared<int>(42);
????????????make_shared<T>????????????????????????????????????????????????棬?????shared_ptr????????????????????????????η?????????η??????????????????Щ????????????й????????????????????seed()????????????????????????й???
????1 void foo(std::shared_ptr<int> p?? int init)
????2 {
????3 *p = init;
????4 }
????5 foo(std::shared_ptr<int>(new int(42))?? seed());
??????????make_shared??????????????????????????weak_ptr????????????????????weak_ptr????????????????weak_ptr??lock()?????????????????shared_ptr????????
????1 auto p = std::make_shared<int>(42);
????2 std::weak_ptr<int> wp = p;
????3
????4 {
????5 auto sp = wp.lock();
????6 std::cout << *sp << std::endl;
????7 }
????8
????9 p.reset();
????10
????11 if(wp.expired())
????12 std::cout << "expired" << std::endl;
???????????????????????weak_ptr?????????????????????????????????????shared_ptr??
????Lambdas????
????C++11?????????????????????????lambda????????????????????????????????????????н????????????????????????????????????????????????????????????????κο????ú????????????std::function?????????????lambda??????????????????????????????
????1 std::vector<int> v;
????2 v.push_back(1);
????3 v.push_back(2);
????4 v.push_back(3);
????5
????6 std::for_each(std::begin(v)?? std::end(v)?? [](int n) {std::cout << n << std::endl;});
????7
????8 auto is_odd = [](int n) {return n%2==1;};
????9 auto pos = std::find_if(std::begin(v)?? std::end(v)?? is_odd);
????10 if(pos != std::end(v))
????11 std::cout << *pos << std::endl;
???????????????lambda????????????lambda?????????Fibonacci????????????????????????auto???????????±??????
????auto fib = [&fib](int n) {return n < 2 ? 1 : fib(n-1) + fib(n-2);};
????error C3533: 'auto &': a parameter cannot have a type that contains 'auto'
????error C3531: 'fib': a symbol whose type contains 'auto' must have an initializer
????error C3536: 'fib': cannot be used before it is initialized
????error C2064: term does not evaluate to a function taking 1 arguments
????????????auto???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????std::function????????????????????????
????std::function<int(int)> lfib = [&lfib](int n) {return n < 2 ? 1 : lfib(n-1) + lfib(n-2);};
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11