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

相关推荐
李妍.5 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy5 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
YH55269845 小时前
GPT‑5.6 Sol 原本支持 1M 上下文,Codex 现已放开此前限制,如何看待这次调整?
java·jvm·人工智能·gpt·算法·chatgpt
吃着火锅x唱着歌5 小时前
Effective C++ 学习笔记 条款43 学习处理模板化基类内的名称
c++·笔记·学习
AI_小站5 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
felixking6 小时前
C++20 协程
开发语言·c++·协程
遥感知识服务7 小时前
从局部阈值、双极化到暗地表剔除:NASA OPERA DSWx-S1全球动态水体算法拆解
大数据·人工智能·深度学习·神经网络·算法·机器学习
晚风醉蝶7 小时前
1-13-TimSort
算法
ZJU_统一阿萨姆7 小时前
【算子开发】算子融合与Softmax_LayerNorm实现
人工智能·算法·语言模型·硬件架构
Zane1994128 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言