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

相关推荐
全貌2 分钟前
C++笔记 --基本语法(命名空间/函数重载/缺省参数/引用/inline/nulltpr)
开发语言·c++·笔记
斯派的曼6 分钟前
学习C++的第七天!
开发语言·c++·学习
街 三 仔7 分钟前
【C语言零基础入门篇 - 15】:单链表
c语言·开发语言
苏格拉没有底1117 分钟前
数据结构——顺序表、链表
c语言·开发语言·数据结构·笔记·学习·算法·链表
抓哇能手16 分钟前
王道408考研数据结构-树与二叉树-第五章-第三四节
c语言·数据结构·考研·算法·408
hhhcbw17 分钟前
C++ STL容器(二) —— list 底层剖析
c++·链表·list·msvc·c++容器
Pandaconda25 分钟前
【计算机网络 - 基础问题】每日 3 题(二十三)
开发语言·网络·笔记·后端·计算机网络·面试·职场和发展
界面开发小八哥28 分钟前
如何用LightningChart Python实现地震强度数据可视化应用程序?
开发语言·python·信息可视化
野生派蒙28 分钟前
IDEA 关闭自动补全功能(最新版本)
java·开发语言·ide·后端·学习·intellij-idea
林小果129 分钟前
观察者模式
java·开发语言·观察者模式·设计模式