C++精简基础(二)

C++精简基础二

文章目录


前言

既然是精简基础,意思就是不会过分深入某个知识点,而是把这些知识点先浅浅带过,有个印象,等第二遍的时候再逐个击破。


一、算术运算符与类型转换

1.1 算术运算符

和隔壁C#的有异曲同工之妙,但是在C++的求余运算中,如果是int类型则两边都必须给整型的数字

cpp 复制代码
#include <iostream>
using namespace std;
int main()
{
    // + - * / %
    //int a;
    //cout << "请输入第一个数字:";
    //cin >> a;
    //int b;
    //cout << "请输入第二个数字:";
    //cin >> b;
    //int c = a + b;
    //cout << c << endl;
    //int res1 = a / b;
    //int res2 = a * b;
    //int res3 = a % b;
    //int res4 = a - b;
    //cout << res1 << " " << res2 << " " << res3 << " " << res4 << endl;

    //int a = 7;
    //int b = 2;
    //float res1 = a / b;
    //cout << res1 << endl;

    int res1 = 13 % 5;
    //int res2 = 13 % 5.1; //C++求余运算两边给必须是整数
    cout << res1 << endl;


    int t;
    cin >> t;
    return 0;
}

1.2 类型转换

强制类型转换,在C或C#里是(类型)num,在C++的新标准语法中,可以类型(num),且可以使用auto关键字去识别num类型,但声明auto之前必须要初始化值。

cpp 复制代码
#include <iostream>
using namespace std;

int main()
{
    //float i = 3;//int
    //int b = 4.3;//double,把一个double类型放在int类型会自动把小数去掉
    int c = 3e20;

    //float res = 4 + 4.4;

    //float f = 34.4;

    //int c = (int)23.8;//强制类型转换
    //cout << c << endl;

    //int d = int(23.8);//新的C++标准的语法,和上面是等价的
    //cout << d << endl;

    //int e = int(f);
    //cout << e << endl;

    auto a = 'a';
    cout << a << endl;
    auto b = 100;
    cout << b << endl;

    //auto c;//这样是不允许的,声明auto关键字必须初始化

    auto x = 0.0;//double 可能我们想要float类型,但他会默认当成double类型,即不符合预期效果。所以不要乱用这个auto关键字

    return 0;
}

二、数组

2.1 数组创建

数组的创建和其他语言也是差不多的,如果创建数组的时候开辟了4个空间,但里面给的值并没有给四个值而是给了三个值的话,则剩下的值默认为0

cpp 复制代码
#include <iostream>
using namespace std;
int main()
{
    //int score = 100;
    //char cArray[10];
    //bool bArray[90];
    //float fArray[34];
    //double dArray[90];

    int scoreArray1[4] = { 34,3,234,45 };//数组的声明,20个数据,int类型
    int scoreArray2[4] = { 34,3 };//0 按照默认值0
    int scoreArray3[] = { 34,3 };

    int scoreArray4[]{ 23,2,3 };//索引 下标
    
    cout << scoreArray1[0] << endl;
    cout << scoreArray1[-1] << endl;
    cout << scoreArray1[8] << endl;
    cout << scoreArray2[3] << endl;
    cout << scoreArray4[0] << endl;
    scoreArray4[1] = 100;
    cout << scoreArray4[1] << endl;


    return 0;
}

2.2 字符串

如果开辟的数组类型为char类型的话,则在最后有\0就是字符串,计算字符数组长度的关键字是strlen

cpp 复制代码
#include <iostream>
using namespace std;
int main()
{
    //char website[] = { 's','i','k','i' };//website不是字符串
    //char website2[] = { 's','i','k','i','\0' };
    //空字符 空格字符 website2是一个字符串
    //cout << website2 << endl;
    //cout << website << endl;

    //char website[] = "sikiedu";//字符数组
    //cout << website[7] << endl;

    //cout << strlen(website) << endl;
    //cout << website[3] << endl;

    //char website[20];
    //cin >> website;//sikiedu.com 
    //cout << website << endl;

    //char str[] = "ninininininininin" "wocao";
    //书写的便捷,两个字符串会自动拼接
    //cout << str << endl;

    char name[30];
    char food[40];

    cout << "你的名字是:";
    //cin >> name;
    cin.getline(name, 30);//获取到一行

    cout << "你喜欢的食物是:";
    //cin >> food;
    cin.getline(food, 40);

    cout << name << "喜欢吃:" << food;
    

    return 0;

}

2.3 字符串基于string

string 头文件提供了 std::string 类,它可以使得我们快速创建一个新的字符串数组并对其进行某些操作。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str1;
    string str2 = "www.siki.com";
    cout << str1 << endl;
    cout << str2 << endl;
    str1 = str2;
    cout << str1 << endl;
    //cin >> str1;
    //cout << str1 << endl;
    //getline(cin, str1);
    //cout << str1 << endl;

    cout << str2[5] << endl;
    str1 = "sikiedu";
    str2 = ".com";
    string s = str1 + str2;
    s += str1;//s = s + str1; sikiedu.com + sikiedu sikiedu.comsikiedu
    cout << s << endl;

    cout << s.size() << endl;

    return 0;
}

总结

1、算术运算符与类型转换与其他语言大差不差

2、字符数组的创建可以引用string头文件来进行快速创建字符数组

相关推荐
董董灿是个攻城狮1 小时前
AI视觉连载8:传统 CV 之边缘检测
算法
blasit8 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
AI软著研究员8 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish8 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱9 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者1 天前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮1 天前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者1 天前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考1 天前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx1 天前
CART决策树基本原理
算法·机器学习