初学C++_java基础(一):基本类型和结构

0.如何运行一个cpp文件

linux系统

g++ test.cpp -o test.out
./test.out

1.using指令的使用

在类中或者函数中包含using namespace std;
如,使用cout的时候,需要访问std中定义的cout才能进行编译。

2.cin的使用

类似于Java中的Scanner类,键盘输入。

#include <iostream>  //引入输出流包
using namespace std; //相当于包的库
int main()
{
    
    
	//1.cin
	int aaa;
	cout << "请输入幸运数字:";
	cin >> aaa;     //键盘输入
    cout << "您的幸运数字是:" << aaa << "!" << endl; //输出   <<endl 换行

	//cin.getline(stringValue, num);  //获取前num-1长度的字符串
	cout << "输入:" << endl;
    char stringValue[5];
	cin.getline(stringValue,6);
    cout << stringValue << endl;
    return 0;
}

3.const的使用

相当于java中final static。

int test_0 = 10; 
const int test = test_0;

//不要使用以下代码
const int test;
test = 10;

4.类型转换

只会记录相较于java,特殊的算术运算符。

4.1.float—>int

#include <iostream>
using namespace std;
void testTypeChange();

int main()
{
    
    
    testTypeChange();
    return 0;
}

void testTypeChange(){
    
    
    float test_0 = 6;
    int intVaule_1 = 2.34123;
    int intValue_2 = 5.613212;
    cout << "test_0 = " << test_0 << endl;
    cout << "intVaule_1 = " << intVaule_1 << endl;
    cout << "intValue_2 = " << intValue_2 << endl;
}

结果:

test_0 = 6
intVaule_1 = 2
intValue_2 = 5

会降低精度,和java基本保持一致。

5.数组

初始化

int nums[5] = {
    
    1, 2, 3, 4, 5};
float nums[4] {
    
    1.123, 2.4123, 5.214, 6.234}; // 省略“=”号
float nums_2[4] {
    
    }; // 初始化时,元素值为0
//or
int nums[5];

// 不允许使用
int test[] = {
    
    1, 2, 3.3}; // 其中3.3为float,不能缩窄变为int

使用

cout << nums_2[1] <<  endl;

6.字符

#include <iostream>
using namespace std;
void testChar();

int main()
{
    
    
    testChar();
    return 0;
}

void testChar(){
    
    
    char bb[] = {
    
    'a','b','c','d'};
    cout << bb << endl;
}

7.string类

7.1.string定义

string aa = "abcdeda";
cout << aa << endl;

7.2.string拼接

类似于java的StringBuffer中的append方法.

#include <iostream>  //引入输出流包
#include <sstream>  //引入流包
using namespace std; //相当于包的库
int main()
{
    
    
	int aaa = 1;
    int b = aaa +1;
    //第一种拼接
	ostringstream allStr; //定义流
    allStr << "asdad_" << b; //流的赋值
    cout << allStr.str()  << endl; //输出   <<endl 换行

	//第二种拼接
	string cc = "bbb" "aaa";
    cout << cc << endl;
    return 0;
}

7.3.string复制和拼接

strcpy(复制)和strcat(拼接),将字符数组/字符串,复制/拼接到字符数组中,也就是说第一个参数是字符数组,第二个参数是字符数组或者字符串。

#include <iostream>
#include <string>
#include <cstring>

using namespace std;
void testString();

int main()
{
    
    
    testString();
    return 0;
}
void testString(){
    
    
    char char1[20];
    char char2[20] = "asasdasdasd";

    strcpy(char1, char2);//复制
    strcat(char1, " 222qwe111");//拼接
    cout << char1 << endl;
	
	string str3 = "ertwtr" + "asdasd";//字符串之间的拼接
}

8.结构

类似于java中的类,但是结构中的属性类型只能是基本类型,如使用string则会报错。

8.1.单个结构

#include <iostream>
struct test_entity{
    
    
    int id;
    char name[20];
};

using namespace std;
void testStrust();

int main()
{
    
    
    testStrust();
    return 0;
}

void testStrust(){
    
    
    test_entity entity1 = {
    
    
        1,
        "fracong"
    };
    cout << "id-->" << entity1.id << " name--->"<< entity1.name << endl;
}

结果:
id–>1 name—>fracong

8.2.结构数组

#include <iostream>
struct test_entity{
    
    
    int id;
    char name[20];
};

using namespace std;
void testStrust();

int main()
{
    
    
    testStrust();
    return 0;
}

void testStrust(){
    
    
    test_entity entity1[2] = {
    
    
        {
    
    
            1,
            "fracong1"
        },{
    
    
            2,
            "fracong2"
        },
    };
    cout << "id-->" << entity1[0].id << " name--->"<< entity1[0].name << endl;
    cout << "id-->" << entity1[1].id << " name--->"<< entity1[1].name << endl;
}

结果:
id–>1 name—>fracong1
id–>2 name—>fracong2

9.共用体

union只能存储int、long、double的一种,当多个属性都被赋值过,以最后一个属性的值为准,且所有的属性值均为该值。

#include <iostream>

union test_union_entity{
    
    
    int id;
    long name;
};

using namespace std;

void testUnion();

int main()
{
    
    
    testUnion();
    return 0;
}

void testUnion(){
    
    
    test_union_entity entity2;
    entity2.id = 18;
    cout << entity2.id << endl;
    cout << entity2.name << endl;
    
	cout << endl;
	
    entity2.name = 20;
    cout << entity2.id << endl;
    cout << entity2.name << endl;
}

结果:
18

20
20

10.枚举

和java中的枚举有些类似

10.1.无值的枚举

下面代码说明:enum的第一个属性如果没有指定值的话,默认为0,后面的如果也没有设置,则依次递增加1。
也就是说,red =0;black=1;white=2

#include <iostream>
enum test_color{
    
    red, black, white};

using namespace std;
void testEnum();

int main()
{
    
    
    testEnum();
    return 0;
}

void testEnum(){
    
    
    test_color color;
    color = black;
    cout << color << endl;
}

结果:1

10.2.设置值的枚举

如下代码:red默认为0,black递增为1,而当white赋值之后,后面的属性如果没有赋值,则根据white依次递增加1。

#include <iostream>
enum test_color{
    
    red, black, white=1, blue};

using namespace std;
void testEnum();

int main()
{
    
    
    testEnum();
    return 0;
}

void testEnum(){
    
    
    test_color color;
    color = blue;
    cout << color << endl;
}

11.地址和指针

11.1.指针的赋值

#include <iostream>

using namespace std;
void testPointer();

int main()
{
    
    
    testPointer();
    return 0;
}

void testPointer(){
    
    
    int i = 10;
    cout << i << endl;// 打印10
    cout << &i << endl;// 打印地址,为16进制

    int * test_int;
    int test_1 = 100;
	cout << *test_int << endl;// 打印10
    cout << test_int << endl;// 打印地址,为16进制

    // 第一种赋值方式
    test_int = &test_1;// 请将地址赋值给带有*的变量的地址,也就是带有指针的变量的地址
    cout << *test_int << endl;
    // 第二种赋值方式
    *test_int = test_1;
    cout << *test_int << endl;
    // 第三种赋值方式
    int * test_int_2;
    cout << test_int_2 << endl; // 这种地址为0,说明还未进行内存的分配
    int * test_int_1 = new int;
    cout << test_int_1 << endl;// 打印出十六进制地址
}

说明:
1.i 为不带指针的变量,地址为 &i
2.*test_int 为带有指针的变量,其地址为test_int,给 *test_int 变量赋值的时候,需要赋值给地址;
3.在给指针赋值的时候,要注意不要将简单的将整数(地址默认也是整数)赋值给指针。当指针根据整数地址找到具体的值的时候,如果找到还好,没有找到,说明这个整数(地址)是无效地址;
4.不要将指针赋值给指针(带*的不能赋值给代*的)。

11.2.new的使用

11.1.已经使用了使用new进行内存的分配,但是要注意内存的释放。

11.2.1.释放内存

#include <iostream>
using namespace std;
void testNew();

int main()
{
    
    
    testNew();
    return 0;
}

void testNew(){
    
    
    int * test_int_1 = new int;
    cout << test_int_1 << endl;// 打印出十六进制地址
    
    *test_int_1 = 100;
    cout << test_int_1 << endl;// 打印出十六进制地址,且不变,说明赋值之后地址不会发生变化
    cout << *test_int_1 << endl;// 打印出值为100
    
    delete test_int_1;// 释放内存
    cout << test_int_1 << endl;// 打印出十六进制地址,且不变,说明释放内存之后地址不会发生变化
    cout << *test_int_1 << endl; // 打印出结果为0,说明释放内存之后,值清空
}

说明:
1.delete释放一定是使用new分配的内存;
2.不要对同一个new出来的内存释放多次;
3.如果是空指针,可以进行释放。

11.2.2.new数组

#include <iostream>
using namespace std;
void testNew();

int main()
{
    
    
    testNew();
    return 0;
}

void testNew(){
    
    
    int * test_int = new int;
    int * test_ints = new int[100];
	
	test_ints[0] = 0;
	test_ints[1] = 1;
	test_ints[2] = 2;
	test_ints[3] = 3;

	cout << test_ints[2] << endl;// 打印出 2

	// 数组和普通数字的内存释放
    delete test_int;
    delete [] test_ints;

	short test_array[3] = {
    
    1,2,3};
    short * ta = &test_array[0];
    
    cout << ta << endl; // 打印为地址
    cout << *ta << endl; //打印为1
}

C++将数组名解释为地址。将数组中的值赋值给指针,需要将[数组的某个值]的地址赋值给指针。

11.3.指针和字符串

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    
    
    char name[10] = "fracong";
    cout << "name:" << (int *)name << endl;
    char * test_ps;
    test_ps = name;
    cout << "test_ps:" << (int *)test_ps << endl;
    test_ps = new char[20];
    strcpy(test_ps,name);
    cout << "test_ps:" << (int *)test_ps << endl;
    delete [] name;
    return 0;
}

当将test_ps = new char[20];注释之后,运行的结果如下:

name:0x7ffd05f6d10e
test_ps:0x7ffd05f6d10e
test_ps:0x7ffd05f6d10e

当不进行注释,运行的结果如下:

name:0x7ffd5c04e1fe
test_ps:0x7ffd5c04e1fe
test_ps:0x5574ca41f280

说明:
当test_ps没有进行new的时候,也就是没有对指针test_ps分配内存空间的时候,使用=或者strcpy是一致的,都是将name的地址赋值给test_ps;但是当test_ps进行new之后,test_ps分配完新的内存空间,使用strcpy的时候,是将值赋值给新的内存空间。
在初始化,使用赋值运算符;否则使用strcpy或者strncpy方法。

12.“.”和“->”的使用区别

“.”使用于实际的对象,间接成员运算符“->”使用于指针对象的指针,二者都是对内部属性进行赋值使用。

13.vector类和array类

13.1.vector类

#include <vector>
// 创建一个0长度的数组
vector<int> a;
// 创建一个指定长度的数组
vector<int> b(4);
b[0] = 1;
b[1] = 2;
b[2] = 3;
b[3] = 4;
count << b[1] << endl;
count << &b[1] << endl;

vector对象存储在自由存储区或堆中

13.2.array类

#include <array>
// 创建一个0长度的数组
array<int,4> a;
// 创建一个指定长度的数组
array<int,4> b = {
    
    1,2,3,4};
count << b[1] << endl;
count << &b[1] << endl;
a = b;

array对象和数组存储在栈中。array可以直接进行赋值,但数组需要逐个元素进行复制。

猜你喜欢

转载自blog.csdn.net/m0_37356874/article/details/103007672
今日推荐