week1_cpp

week 1

2 start

  1. 函数头对函数与其他部分之间的接口进行了总结

  2. int main(void)//表明不再接受其他类型的参数

  3. 命名空间类似于生产商对自家产品的说明

  4. 运算符重载:即同一个符号在不同地方有不同的意义

  5. 特别地,对于c++,可以这样做

    int a,b,c;
    a=b=c=0;

  6. 类描述了一种数据类型的全部属性(包括它可执行的操作),对象是根据这些描述创建的实体,例如类的对象cout,就有getline操作?

3 数据处理

3.1

  • 关于进制呈现

    #include

    using namespace std;

    int main(){
        char ch='h';
        int a=ch;
        cout<<"char:"<<ch<<" ASCALL"<<a<<"\n";
        //cout<<hex;
        cout<<hex<<a<<endl;
        cout<<a;
        //hex指令之后,所有输出都为八进制
        return 0;
    }
    
    
  • 字符变量和字符常量

cout.put(‘$’)用于解决早期字符常量被存储和打印为ascall码的问题,字符变量则cout为字符类型

3.2 const限定

相较于#define类型更加明确,作用域更规范

3.3 浮点数

有基准值和缩放因子组成,小数点浮动例如(9.12e-12) ,cout输出时会省去最后几位0,而c不会

此外,浮点数范围大,但是精度有限,同时还会减慢计算速度

#include <iostream>  
#include <iomanip> // 包含 setf 和其他格式控制函数  

int main() {  
    double num = 123.456;  

    // 设置浮点数以科学计数法输出  
    std::cout.setf(std::ios::scientific);  
    std::cout << num << std::endl; // 输出: 1.234560e+02  

    // 设置浮点数以固定小数点格式输出,并设置精度为2  
    std::cout.setf(std::ios::fixed, std::ios::floatfield);  
    std::cout.precision(2);  
    std::cout << num << std::endl; // 输出: 123.46  

    // 设置布尔值以 true/false 而不是 1/0 输出  
    std::cout.setf(std::ios::boolalpha);  
    bool flag = true;  
    std::cout << flag << std::endl; // 输出: true  

    // 重置格式标志  
    std::cout.unsetf(std::ios::scientific | std::ios::fixed | std::ios::boolalpha);  
    std::cout.precision(6); // 重置精度  

    std::cout << num << std::endl; // 输出: 123.456  
    std::cout << flag << std::endl; // 输出: 1  

    return 0;  
}

3.4

  • 9/5 9L/5L

  • 类型转换

      1.   初始化时进行转换,例如double a=1double a={1}后者不允许缩窄;
    
      2. 强制类型转换 int a=(long)b;int a=long(b)//c++不同于c的用法
    
       3. 使用auto自动识别
    

4 复合类型

4.1数组

1. 初始化

//初始化的几种方法
int a[]={1,2,3,4};//这个数组就只有四个元素
int b[9]={1,0};
int c[9]{0};
//此外,禁止缩窄

4.2字符串

#include<iostream>
#include<cstring>

using namespace std;

int main(){
    char a[9]{'h','e',' ','l','\0'};
    //char b[9]{'h','e',' ','l'};
    //字符串常量或字符串字面值
    char c[]="hello world?";
    cout<<a<<endl<<c<<"\n"<<strlen(c);

    //字符串的输入
    char d[20];
    cin.getline(d,15);
    //getline通过换行符确定结尾,并且用\0替换
    cin.get(d,15).get();
    //get()读取并丢弃下一个字符
    int year;
    cin>>year;
    cin.getline(d,15);
    //year后面读取的换行符并未被舍弃
    //所以需要变成cin>>year.get()
    return 0;
}

4.3 string

#include<iostream>
#include<string>
#include<cstring>

using namespace std;

int main(){
    string str1,str2;
    cin>>str1;
    cin>>str2;
    cout<<str1+" "+str2<<endl;
    /* char a[20],str[20];
    strcpy(a,str1);
    strcat(a,str2); 
    等效于c语言的做法*/
    int len=str1.size();
    //对于对象str1使用size()类方法
    //int len=sizeof(str1);
    getline(cin,str1);
    //此处的getline()不是一个类方法,函数?
    return 0;
}

4.4 结构体

#include<iostream>

using namespace std;

struct student{
    string name;
    int height;
    int weight;
}/*若名称在这里,则是一次性的结构体*/;
int main(){
    student student1={"jake",166,70};
    cout<<student1.name;
    student students[50]={{0},{0}};
    students[0].name="meme";
    cout<<students[0].name;
    return 0;
}

4.5 共用体

struct student{
    union name{
        string name_str;
        int name_int;
    };//允许多种数据类型用一个地址,但是共用体只会保存最新的一个数据
    int height;
    int weight;
}

4.6 枚举

// C语言中的枚举示例  
enum Season {  
    Spring=1,  
    Summer=9,  
    Autumn=15,  
    Winter=7  
};  

enum Season currentSeason = Spring; // 将currentSeason变量赋值为Spring
currentSeason=Season(3);
//枚举的范围为0~15

4.7指针

#include<iostream>
#include<string>
using namespace std;

int main(){

    int a=9;
    int *b=&a;
    //int *b;有了储存地址的地址空间
    //*b=2233;
    //2233没有指定的地址储存
    int *c;
    c=(int *)0xB8000000;
    //在c语言中c=0xB8000000即可

    //用new分配内存
    int *d=new int;
    *d=99;
    delete d;
    //释放d开始被分配的地址,仅限于new
    //静态联编,即在编译时就确定了数组的大小

    //数组和指针的区别
    int *e=new int [10];//节约内存
    //int a[10];a是第一个值的地址;&a
    e[0]=1;
    //此时*e=1;
    ++e;
    *e=2;
    //此时e[1]=2;
    delete [] e;
    char *f=new char;
    cin>>*f;
    cout<<&f<<endl<<*f;
    delete f;
    string str1;
    getline(cin,str1);
    cout<<str1<<&str1;
    return 0;
}



#include<iostream>
#include<string>
using namespace std;

struct student{
    string name;
    int height;
};
int main(){
    student *ps=new student;
    ps->name="jake";
    //*ps是结构体本身
    cout<<ps->name;
}

4.8 其他类型(数组替代)

#include<iostream>
#include<vector>
#include<array>
using namespace std;


int main(){
    int n;
    cin>>n;
    vector<double> vd(n);
    vd[0]=0;

    array<int,4>a1;
    a1[0]=0;


}

5 循环关系表达式

  1. cin.get()与cin.get(ch)的区别

    char a;
    cin.get(a);
    //区别于c,这里的a为引用

    while(cin.fail()==0)//检测EOF

2.指针名n1和&n1的区别

#include <iostream>  
#include <iomanip> // 用于设置输出格式  
  
int main() {  
    int n1[3];  
    std::cout << "Address of n1 (interpreted as pointer to first element): " << static_cast<void*>(n1) << std::endl;  
    std::cout << "Address of the array n1 itself: " << static_cast<void*>(&n1) << std::endl;  
    // 注意:直接打印指针时,最好将其转换为 void* 以避免潜在的未定义行为  
    return 0;  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值