Note02

书:C++高级编程 第三版
1. using指令不要过度使用,如果使用过多,实际上就是完全取消了名称空间。
2. 切勿在头文件中使用using指令或者using声明,否则每个人都会包含你的头文件。
3. 变量声明时建议初始化;
4. auto关键字用途:
(1) auto x = 123; 用于变量
(2) auto result = getFoo(); 用于函数返回值、
(3) 替代函数;
(4) 用于通用的lambda表达式;
5. int x = 123; decltype(x) y = 456;
6. 动态分配数组

int arraySize;
cin >> arraySize;
int* data = new int[arraySize];
  1. C++中,避免使用malloc()和free(),而使用new和delete,或者使用new[]和delete[];每次调用new时,都应该相应地调用delete,每次调用new[]时,都应该相应地调用delete[],以避免内存泄露。
  2. 真正的空指针nullptr;
  3. 3种智能指针:
    std::unique_ptr; std::shared_ptr; std::weak_ptr; 均定义在头文件中;unique_ptr只属于它指向的对象,在超出作用或被删除时,会自动释放内存或资源;
  4. const引用参数的主要价值在于效率。当向函数 传递值时,会制作一个完整的副本。当传递引用时,实际上只是传递了一个指向原始数据的指针,这样计算机就不需要执着副本。通过传递const引用,可以做到二者兼顾——不需要副本,原始变量也不需要修改。在处理对象时,const引用会变得更加重要,因为对象可能比较庞大,复制对象可能需要很大的代价。
  5. 最好将不改变对象的任何数据成员的成员函数声明为const。(const-correctness原则)
  6. vector基本功能和迭代器
#include<string>
#include<vector>
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
    vector<string> myVector = {"A first string", "A second string"};
    myVector.push_back("A third string");
    myVector.push_back("The last string in the vector");
    cout << "第一种遍历方式:\n";
    for (const auto& str : myVector)
        cout << str << endl;
    cout << "第二种遍历方式:\n";
    for (string& str : myVector)
        cout << str << endl;
    cout << "第三种遍历方式:\n";
    for (auto iterator = cbegin(myVector); iterator != cend(myVector); ++iterator)
    {
        cout << *iterator << endl;
    }
    system("pause");
    return 0;
}
  1. 求字符串长度strlen(“A first string”)
  2. 数值转换为字符串:to_string(参数)
long double d = 3.14L;
string s = to_string(d);
  1. 字符串转为数值
int stoi(const string& str,size_t *idx=0, int base=10)
long stol(const string& str, size_t *idx=0, int base=10)
unsigned long stoul(const string&str, size_t *idx=0, int base=10)
long long stoll(const string& str, size_t *idx=0, int base=10)
unsigned long long stoull(const string& str, size_t *idx=0, int base=10)
float stof(const string& str, size_t *idx=0)
double stod(const string& str,size_t *idx=0)
long double stold(const string& str,size_t *idx=0)
//eg:
const string s = "1234";
int i = stoi(s);
  1. 调用复制构造函数
    无论什么时候给函数或者方法传递一个对象,编译器都会调用新对象的复制构造函数进行初始化。
    当函数或者方法返回对象时,也会调用复制构造函数。此时,编译器使用复制构造函数创建一个临时的、没有名称的对象。可将传递的参数作为const引用,从而避免调用复制构造函数的开销。
    显式调用复制构造函数:
SpreadsheetCell myCell2(4);
SpreadsheetCell myCell3(myCell2);
当向函数或者方法传递对象时,为了避免复制对象,可以让函数或者方法采用对象的引用做参数。按引用传递对象通常比按值传递对象的效率更高,因为只需要复制对象的地址,而不需要复制对象的全部内容。,此外,按引用传递可以避免对象动态内存分配的问题。如果不想修改对象,可以添加const。
最好按const引用传递而不是按值传递对象。
复制构造函数在下列情况下使用:
(1) 根据另一个同类型的对象显示或隐式初始化一个对象;
(2) 复制一个对象,将它作为实参传给一个函数;
(3) 从函数返回时复制一个对象;
(4) 初始化顺序容器中的元素;
(5) 根据元素初始化式列表初始化数组元素;

17. 求最大公约数和最小公倍数

#include <iostream>
#include <string>
int main()
{
    int i, j;
    cin >> i >> j;
    int temp;
    if (i < j)
    {
        temp = j;
        j = i;
        i = temp;
    }
    int amass = i * j;
    while (j != 0)
    {
        temp = i%j;
        i = j;
        j = temp;
    }
    cout << "最大公约数:" << to_string(i) << endl;
    cout << "最小公倍数: " << to_string(amass / i) << endl;
    system("pause");
    return 0;
}
  1. 流类型不能作为函数的形参或返回类型,必须使用流类型的指针或引用。
//正确
ostream& print(ostream& os);
//错误
ostream print(ostream os);
  1. 对于open_file函数,在调用open前先调用clear函数。如果程序执行close函数失败,则不能用文件流in打开给定的文件。
    例子:
ifstream& open_file(ifstream &in, const string &file)
{
    //关闭以防它已经打开
    in.close();
    //清除内部状态
    in.clear();
    //打开给定文件
    in.open(file.c_str());
    return in;
}
  1. 不可以使用容器来存储iostream,因为容器元素类型必须支持赋值操作及复制,而iostream类型不支持赋值和复制。
  2. 使用循环将list容器的元素逆序输出:
list<int> lst1;
list<int>::iterator iter1 = lst1.begin(),iter2 = lst1.end();
while(iter2!=iter1)
    cout << *(--iter2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[root@QAQ ~]# sudo tail -n 50 /var/log/mysql/error.log 2023-07-14T02:45:21.370949Z 0 [Note] Shutting down plugin 'partition' 2023-07-14T02:45:21.370952Z 0 [Note] Shutting down plugin 'BLACKHOLE' 2023-07-14T02:45:21.370954Z 0 [Note] Shutting down plugin 'ARCHIVE' 2023-07-14T02:45:21.370956Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2023-07-14T02:45:21.370993Z 0 [Note] Shutting down plugin 'MRG_MYISAM' 2023-07-14T02:45:21.370995Z 0 [Note] Shutting down plugin 'MyISAM' 2023-07-14T02:45:21.371003Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL' 2023-07-14T02:45:21.371011Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' 2023-07-14T02:45:21.371013Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES' 2023-07-14T02:45:21.371015Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS' 2023-07-14T02:45:21.371017Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN' 2023-07-14T02:45:21.371018Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS' 2023-07-14T02:45:21.371020Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS' 2023-07-14T02:45:21.371022Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES' 2023-07-14T02:45:21.371024Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS' 2023-07-14T02:45:21.371026Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES' 2023-07-14T02:45:21.371028Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2023-07-14T02:45:21.371030Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2023-07-14T02:45:21.371032Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG' 2023-07-14T02:45:21.371033Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2023-07-14T02:45:21.371035Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED' 2023-07-14T02:45:21.371037Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2023-07-14T02:45:21.371039Z 0 [Note] Shutting down plugin 'INNODB_METRICS' 2023-07-14T02:45:21.371041Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO' 2023-07-14T02:45:21.371043Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2023-07-14T02:45:21.371045Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2023-07-14T02:45:21.371047Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE' 2023-07-14T02:45:21.371049Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2023-07-14T02:45:21.371050Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2023-07-14T02:45:21.371052Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET' 2023-07-14T02:45:21.371054Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM' 2023-07-14T02:45:21.371056Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET' 2023-07-14T02:45:21.371058Z 0 [Note] Shutting down plugin 'INNODB_CMP' 2023-07-14T02:45:21.371060Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS' 2023-07-14T02:45:21.371062Z 0 [Note] Shutting down plugin 'INNODB_LOCKS' 2023-07-14T02:45:21.371064Z 0 [Note] Shutting down plugin 'INNODB_TRX' 2023-07-14T02:45:21.371066Z 0 [Note] Shutting down plugin 'InnoDB' 2023-07-14T02:45:21.371100Z 0 [Note] InnoDB: FTS optimize thread exiting. 2023-07-14T02:45:21.371135Z 0 [Note] InnoDB: Starting shutdown... 2023-07-14T02:45:21.471280Z 0 [Note] InnoDB: Dumping buffer pool(s) to /www/server/data/ib_buffer_pool 2023-07-14T02:45:21.471421Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 230714 10:45:21 2023-07-14T02:45:22.992635Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2767468 2023-07-14T02:45:22.993964Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1" 2023-07-14T02:45:22.993980Z 0 [Note] Shutting down plugin 'MEMORY' 2023-07-14T02:45:22.993985Z 0 [Note] Shutting down plugin 'CSV' 2023-07-14T02:45:22.993989Z 0 [Note] Shutting down plugin 'sha256_password' 2023-07-14T02:45:22.993991Z 0 [Note] Shutting down plugin 'mysql_native_password' 2023-07-14T02:45:22.994103Z 0 [Note] Shutting down plugin 'binlog' 2023-07-14T02:45:22.994915Z 0 [Note] /www/server/mysql/bin/mysqld: Shutdown complete
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值