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头文件来进行快速创建字符数组

相关推荐
Rabitebla18 小时前
【C++】string 类:原理、踩坑与对象语义
linux·c语言·数据结构·c++·算法·github·学习方法
邪修king18 小时前
UE5 零基础入门第四弹:UMG UI 系统入门,从静态界面到逻辑联动
c++·ui·ue5
小雅痞19 小时前
[Java][Leetcode middle] 167. 两数之和 II - 输入有序数组
java·算法·leetcode
傻啦嘿哟19 小时前
如何在 Python 中使用 colorama 库来给输出添加颜色
开发语言·python
CN-Dust19 小时前
【C++】输入cin例题专题
java·c++·算法
数模竞赛Paid answer20 小时前
2025年MathorCup数学建模A题汽车风阻预测解题文档与程序
算法·数学建模·mathorcup
geovindu20 小时前
go: Visitor Pattern
开发语言·设计模式·golang·访问者模式
宣宣猪的小花园.20 小时前
C语言重难点全解析:内存管理到位运算
c语言·开发语言·单片机
方安乐1 天前
python之向量、向量和、向量点积
开发语言·python·numpy
Old Uncle Tom1 天前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent