C++ Primer 第五版

本文介绍了C++中的基本概念,如for和while循环的适用场景,引用的初始化规则,以及指针和常量指针的用法。强调了指针和引用在常量处理上的差异,并讨论了复合类型的别名typedef和auto关键字的特性。此外,还涉及了动态内存管理、拷贝构造函数和拷贝赋值运算符的重要性,以及模板和泛型编程的基础知识。
摘要由CSDN通过智能技术生成

第一章

cout<<endl; //结束当前行,并将与设备关联的缓冲区(buffer)中的内容刷到设备中。如果程序崩溃,输出可能还停留在缓冲区。
cout<<"/*";
cout<<"*/";
cout<</* "*/" */";
cout<</* "*/" /* "/*" */;  //打印出 /**/ */ /* 

for和while的区别:最大的区别就是for循环一般应用于循环次数已知的情况,而while循环一般应用于循环次数未知的情况。在一般情况下,这两者是可以相互转化的。

当使用istream为对象时:例如cin,只有当遇到文件结束符时才检测为假,window:ctrl+z。

当计算表达式中无符号数和有符号数混用时:结果若为负数,则其为补码按照无符号数的值

short+int = int(会将short转化为int)

第二章

引用必须进行初始化,且只能绑定一个对象(const也是),int &i=42是错的(但是const int &i=42是允许的)

在函数体外定义的变量拥有固定地址,能够进行默认初始化。有固定地址,能给constexpt初始化

![image-20221106094958357](…/…/AppData/Roaming/Typora/typora-user-images/image-20221106094958357.png

image-20221004204613302

int *p;
int *&r=p; //r是一个对指针p的引用

image-20221004212436032

const int ci=1024;
int &ni=ci;//错误试图让一个非常量引用指向一个常量对象。


//要想引用常量,必须要常量指针,但是常量指针不一定指向常量。(指针同理,常量指针和常量引用可以指向不同类型,其余情况指针和引用类型必须相等如int*只能指向int型)
double nihao = 3.12634;
const int &i=nihao; //如果实参和引用参数不匹配,C++将生成临时变量。仅当参数为const引用的时候,C++才允许这样做。总结:当传入为左值时,都可以为其创建引用,而不需要临时变量,而右值,因为没有具体的名称所以需要临时变量接受值,在让引用指向临时变量。
nihao=2.123123;//i的值还是3,因为在进行赋值时,使用了临时变量(指针同理,下图指针也同理)

//指针独有  也有常量引用
int i=0;
int *const cur=&i;//说明该指针是一个常量(常量指针),不变的是指针本身而非指向的那个值。
//从右往左看离cur最近的是const说明其是一个常量指针,而离p最近的是int*说明其是一个指向常量的指针
const int i=3;
const int *p=&i;//指向常量的指针
*p=3; //错误不能通过指针修改常量
int j=5;
p=&j;//但是可以将指针指向其他对象

image-20221004215425812

//当为复合类型指定别名时:
typedef char *pstring; //pstring就相当于char *

const pstring cstr = 0;和const char *cstr = 0;//两条语句有区别吗?
//将这种复合类型别名还原成原来的样子并不是直接替换,前者pstring的基本数据类型是常量指针,后者的数据类型为常量字符型。也就是说,为char*取别名时*是跟着char的,也就是字符指针类型,前面的const也是直接修饰的cstr,也就是常量指针

auto会忽略顶层const,保留底层const,顶层const:不能改变其值,其余为底层const(指针和引用时保留顶层属性)

image-20221005092437169

image-20221005093901727

第三章

image-20221005104619823

getline((cin,line));//能够读入空白字符 遇到\n结束
cin>>line;//不能读入空白字符 

string nihao1="shdgf";
string nihao2=nihao1;
nihao2[0]='o';//赋值相当于对左值进行拷贝  改变nihao2的值不会改变nihao1的值  与java不通  对于类对象也是

int *p=0;//p被定义为空指针

image-20221005114806697

image-20221005114952596

image-20221005143500126

image-20221005143818334

image-20221005144225457

int a[]={1,2,3};
for(int i:a)
	cout<<i;

int a[3][4]={1,1,1,1,1,1,1,1,1,1,1,1};
for(auto i=a;i!=a+3;++i){//i是一个指向四个整数的数组
    for(auto q:*i)//q是int类型
        cout<<q;
}

char a[]="nihao";//数组a的大小为5  但是会加上一个'\0'  注意书上讲的有些和实际运行不一样  比如初始化数组不能用变量和这个   在本机运行环境下可以使用变量初始化也不会在该方式下加入'\0'

第四章

image-20221005192903272

使用++i

image-20221005203335014

image-20221005203631990

image-20221005204123464

使用& | >>等运算符时,低位类型 会被转化为int在进行运算

image-20221005224633259

double slope=static_cast<double>(j) / i;//新写法
double slope = double(j);//旧C++写法

image-20221005232908503

image-20221005232937114

第五章

image-20221005234702628

第六章

image-20221006100315376

image-20221006112720098

image-20221006113538525

image-20221006113912919

image-20221006121931260

image-20221006122436326

image-20221006122828394

image-20221006162522648

class Screen{
public:
	int nihao;
	Screen(int i){
		this->nihao=i;
	}
	Screen() = default;//默认构造函数
}

image-20221007092847973

image-20221007100040236

image-20221007102329540

image-20221007103004976

image-20221007111007500

image-20221007114036987

需要在类外初始化才能在main函数中使用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YEjNtJlN-1682344817672)(…/…/AppData/Roaming/Typora/typora-user-images/image-20221007190021121.png)]

第九章

image-20221009170853929

image-20221009170837725

image-20221009170811908

image-20221009170801161

image-20221009170749388

image-20221009170735593

image-20221009170716679

image-20221009170705194

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zOLCKAHG-1682344822415)(null)]

image-20221009170634863

image-20221009170624387

image-20221009170558191

image-20221009170608895

image-20221009170510054

image-20221009170446682

image-20221009170434120

第十章

image-20221009170316780

image-20221009170256883

image-20221009170238275

image-20221009170226846

"ashdasd"  //这是字面值  const char* 类型 不能两个相加
string("sadjsad") //这是string对象
cbegin()和cend() 返回const 迭代器  用于不改变值
begin和end  可以改变值

image-20221008213047614

fill等泛型函数没有办法改变容器大小(元素的大小size不是capacity)back_inserter可以

image-20221008214616743

image-20221008223158536

[]可以捕获当前作用域的变量

image-20221008232312123

第十一章 关联容器

image-20221009111238900

map<string,int> m;
pair<map<string,int>::iterator,bool> hh = m.emplace(make_pair("nihao",5));
cout<<hh.first->first<<endl<<hh.second;//insert也是一样的

image-20221009170111631

image-20221009170042545

image-20221009170029255

image-20221009170001580

image-20221009165942678

image-20221009165926033

第十二章 动态内存

image-20221009193217105

image-20221009193954965

image-20221010091145466

image-20221010092645608

p.get();//获得的是普通指针  作用域结束后就会被销毁  可能会把智能指针指向的对象也销毁
void nihao(initializer_list<int> args){
    for(auto i=args.begin();i!=args.end();i++){
        cout<<*i<<endl;
    }
}
nihao({1,2,3,4,5});  //initializer_list  用法需要导入initializer_list

const char* str1="ajsgfdasda";//获取字符串数组长度 strlen(str1)

image-20221010114934446

第十三章 拷贝控制

class Sales_data{
public:
	Sales_data(const Sales_data&){//拷贝构造函数     
        
    }   
    Sales_data& operator=(const Sales_data&){//拷贝复制运算符
        
    }
    Hasptr f(Sales_data p){
        Sales_data ret=p;//拷贝给ret
        return ret;
    }//返回时p被析构其中的ps被销毁  如果使用默认的拷贝复制运算符则ret中的指针成员也被销毁
    ~Sales_data(){
        delete ps;
    }
private:
    char* ps;
}
//三五法则,当类中有自定义的析构函数时,应该要有拷贝构造函数和拷贝复制运算符,例如:当类中有指针时,析构函数需要有delete(不会自己销毁),

image-20221010183708427

image-20221010184607675

image-20221010193151837

第十四章 重载

image-20221011111515459

greater<int> nihao;
cout<<nihao(10,20);  //标准库定义的函数对象

image-20221012082610494

第十五章 面向对象程序设计

动态绑定只存在引用和指针中(即基类对象的函数可能是派生类中的)其他没有例如:

基类A 派生类B
B b1;
A a1;
a1=b1;//a1中的所有函数都是基类中定义的
A* a2;
a2 = &b1;//a2中的虚函数是s

image-20230228204301703

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D5AXMtnM-1682344829442)(null)]

因为只会使用基类中的默认参数,不会使用派生类中的默认参数。

class Quote{
    public:
    Quote() = default;
    Quote(const string &book,double sales_price):
        bookNo(book),price(sales_price) {}
    string isbn() const {return bookNo;}
    virtual void println(){
        cout<<"sb"<<endl;
    }
    virtual double net_price(size_t n) const{
        return n * price;
    }

    virtual ~Quote() = default;

    private:
        string bookNo;
    protected:
        double price = 0.0;

};
class bulk:public Quote{
    public:
    void println(){
        cout<<"woshi"<<endl;
    }
};

double print_total(ostream &os,const Quote &item, size_t n){
    double ret = item.net_price(n);
    os<<"ISBN: "<<item.isbn()<<" % sold: "<<n<<" total due: "<<ret<<endl;
    return ret;
}
int main(){
    Quote nihao;
    bulk nihao1;
    nihao.println();
    nihao1.println();
    Quote &nihao2=nihao1;
    nihao2.println();//有virtual 结果是sb woshi woshi  去掉virtual结果sb woshi sb
    return 0;
}



struct cmp{
    bool operator()(const rec&a,const rec&b){
        return a.x<b.x||a.x==b.x&&a.y<b.y;
    }
};
//可以直接用来用在泛型函数上     
multiset<int,cmp>

"nihao"//这个是一个字符串指针
    
int i=max<double>(1,2.0);//对
int i=max(1,2.0);//错   必须相同类型不然无法进行转换   当显示指出后即可

image-20221012153108792

image-20221012154119505

第十六章 模板实参推断

左值:表达式执行结束后依然存在的数据,即持久性数据;右值是指那些在表达式执行结束不再存在的数据,即临时性数据。一个区分的简单方法是:对表达式取地址,如果编译器不报错就是左值,否则为右值

第十七章 tuple类型

image-20221014095750956

image-20221014100334874

image-20221014110510983

image-20221014120100661

#include <random>
unsigned int rand_int(){
    static uniform_int_distribution<unsigned> u(0,9999);
    //static uniform_real_distribution<double> u(0,1); 生成0,1之间的浮点数
    //static normal_distribution<double> n(4,1.5);  均值为4 标准差1.5
    static default_random_engine e;
    return u(e);
}//也可以给e加入时间种子    e.seed(time(0))   time在ctime中
string* nihao=new string("asdasd");//返回的是指针
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值