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

相关推荐
檀越剑指大厂几秒前
【Python系列】Flask 应用中的主动垃圾回收
开发语言·python·flask
檀越剑指大厂7 分钟前
【Python系列】使用 memory_profiler 诊断 Flask 应用内存问题
开发语言·python·flask
笠码9 分钟前
JVM Java虚拟机
java·开发语言·jvm·垃圾回收
CoovallyAIHub21 分钟前
避开算力坑!无人机桥梁检测场景下YOLO模型选型指南
深度学习·算法·计算机视觉
橙小花24 分钟前
C语言:指针、变量指针与指针变量、数组指针与指针数组
c语言·开发语言
YouQian77225 分钟前
问题 C: 字符串匹配
c语言·数据结构·算法
yanxing.D30 分钟前
408——数据结构(第二章 线性表)
数据结构·算法
Cyanto43 分钟前
MyBatis-Plus高效开发实战
java·开发语言·数据库
艾莉丝努力练剑1 小时前
【LeetCode&数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
YuTaoShao1 小时前
【LeetCode 热题 100】51. N 皇后——回溯
java·算法·leetcode·职场和发展