【C++】基础,数据类型,函数,字符串,引用,输入输出,数据结构

C++

#include <iostream>
using namespace std;    //  使用std命名空间
int main()
{
    cout << "Hello World" <<endl;   //  endl插换行符
    return 0;
}

数据类型

【C语言】数据类型、存储类

【C语言】枚举、结构体、共用体

  • bool, char, int, float, double, void
  • wchart: 宽字符型: typedef short int wchar_t
  • 浮点: 314159E-5L
  • 说明符
    • mutable: 仅适用于类的对象, 允许对象成员替代常量
    • thread_local: 声明的变量仅可在其上创建的线程上访问, 创建线程时创建, 销毁线程时销毁

函数

lambda函数

[](int x, int y){return x<y;}
[]{++global_x;}
[](int x, int y) -> int {int z = x+y; return z+x;}

字符串

#include<iostream>
using namespace std;
int main()
{
    char site[7] = {'S','h','e','n','Z','h','e','n','\0'};
    cout << site << endl;
    return 0;
}

字符串处理函数

【C语言】字符串&字符串处理函数

string类

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1 = "Tianjin";
    string str2 = "ShenZhen";
    string str3;
    int len;
    str3 = str2;        //  复制字符串到str3
    str3 = str1 + str2; //  连接字符串
    len = str3.size();  //  获取字符串长度
    return 0;
}

C++指针


引用

  • 引用变量是某个已存在变量的别名
  • 与指针的区别
    • 不存在空引用, 必须连接一块合法内存; 存在空指针
    • 引用被初始化为一个对象, 就不能指向另一个对象; 指针任何时候可指向另一个对象
    • 引用必须创建时被初始化; 指针可在任何时候初始化
int i = 17;
int& r = i;
cout << i << endl;
cout << r << endl;

引用作参数

类似指针

void swap(int& x, int& y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}

引用作返回值

double vals[] = {10.1, 44.5, 67.8, 33.2};
double& setValues(int i)
{
    double& ref = vals[i];
    return ref;
}
void main(){
    setValues(3) = 555.5;   //  改变第四个元素
}

输入输出

头文件

  • <iostream>: 定义了cin标准输入流, cout标准输出流, cerr非缓冲标准错误流, clog缓冲标准错误流对象
  • <iomanip>: 参数化流操纵器(setw, setprecision),声明对执行标准化IO有用的服务
  • <fstream>: 为用户控制的文件处理声明服务

标准输出流

  • 预定义的对象cout是iostream的一个实例, 连接到标准输出设备(显示屏)
char str[] = "JiangMen";
cout << "Vlue" << str << endl;

标准输入流

  • 预定义的对象cin是iostream类的一个实例, cin对象附属到标准输入设备(键盘)
char name[50];
cin >> name;
cin >> sex >> age;

标准错误流

  • 预定义的对象cerr是iostream的一个实例, 附属到标准输出设备(显示屏)
    • 非缓冲, 每个流插入cerr立即输出
char str[] = "Unable to read...";
cerr << "Error:" << str << endl;

标准日志流

  • 预定义的对象clog是iostream类的一个实例, clog对象附属到标准输出设备(显示屏)
    • 缓冲, 每个流插入clog先存储在缓冲区, 直到缓冲填满/缓冲区刷新才会输出

数据结构

猜你喜欢

转载自blog.csdn.net/weixin_46143152/article/details/126725079