76 学习C++

0 引言

C++语言特性记录,提高对这门语言的理解,进而带动对编程语言特性的理解。

相关网站及教程

# W3Cschool C++教程
https://www.w3cschool.cn/cpp/

# W3Cschool C语言教程
https://www.w3cschool.cn/c/

# C++ API reference
http://www.cplusplus.com/reference/
http://en.cppreference.com/w/

# geeksforgeeks上的c++ 教程等
https://www.geeksforgeeks.org/c-plus-plus/

1 C++中的const 引用参数

(1) 在C++中,很多时候不希望函数调用时使用值传递(这样做需要得到实参的一个拷贝,降低了效率),而使用引用参数(引用本质上就是指针)。由于对引用参数值的改变就是对实参值的改变,有时候不希望改变实参的值,为了防止出错,而使用const 引用参数。这样既有引用参数的效率,而又不会改变实参的值。举个opencv中的例子。

CV_EXPORTS void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
                             const Mat& img2, const vector<KeyPoint>& keypoints2,
                             const vector<DMatch>& matches1to2, Mat& outImg,
                             const Scalar& matchColor=Scalar::all(-1), const Scalar& singlePointColor=Scalar::all(-1),
                             const vector<char>& matchesMask=vector<char>(), int flags=DrawMatchesFlags::DEFAULT );

该函数将传入的函数参数用const + 引用的形式传入,将输出参数用引用的形式输出,提高了数据传输效率的同时,保证了实参的安全。

(2) const 类型的类对象无法调用非const类型的function

2 ofstream

ios::app:  append,只能在文件末尾插入,对数据的保护性更好。

ios::ate, at end,打开文件的时候程序把光标指向末尾,但是可以任意移动(seek等操作),灵活性更高。

3 STL

(1)std::pair

// pair::pair example
#include <utility>      // std::pair, std::make_pair
#include <string>       // std::string
#include <iostream>     // std::cout

int main () {
  std::pair <std::string,double> product1;                     // default constructor
  std::pair <std::string,double> product2 ("tomatoes",2.30);   // value init
  std::pair <std::string,double> product3 (product2);          // copy constructor

  product1 = std::make_pair(std::string("lightbulbs"),0.99);   // using make_pair (move)

  product2.first = "shoes";                  // the type of first is string
  product2.second = 39.90;                   // the type of second is double

  std::cout << "The price of " << product1.first << " is $" << product1.second << '
';
  std::cout << "The price of " << product2.first << " is $" << product2.second << '
';
  std::cout << "The price of " << product3.first << " is $" << product3.second << '
';
  return 0;
}

(2)

4 google开源项目风格指南-C++版本

https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/headers/

(1)  

5 C++ 类

(1)拷贝构造函数:是指一类以类对象为参数的函数,其作用是复制类对象的成员。

https://www.cnblogs.com/alantu2018/p/8459250.html
原文地址:https://www.cnblogs.com/ghjnwk/p/11268509.html