C++基础知识笔记(1)

1. sizeof操作符 与 strlen()函数的区别:
#include <iostream>
#include <cstring>
using namespace std;

int main() {
int p1[5] = {1,2,3,4,5};
char p2[20] = "hello world!";

cout << sizeof(p1) << endl;
cout << sizeof(p2) << '\t' << strlen(p2) << endl;
return 0;
}

输出为:20
20 12
总结:(1)sizeof操作符指出整个数组的长度,但strlen()函数返回的是存储在数组中的字符串的长度。
(2)strlen()函数只能用于存储字符串的数组(即char数组),否则编译器会报错


2. string类与char[]数组的区别:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main() {
char p1[30] = "hello world!";
string s1 = "hello world!";

cout << strlen(p1) << endl;
cout << s1.size() << endl;
return 0;
}

输出为:12
12
总结:函数size()的功能与strlen()基本相同,但句法不同:s1不是作为函数参数,而是位于函数名之前,它们之间用句点连接.表明,s1是一个对象,而size()是一个类方法.


3. C++中的结构简介:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

struct Test {
private:
int i;
string s ;

public:
void setIValue(int i);
int getIValue();

void setSValue(string s);
string getSValue();

void HW();

};

void Test::setIValue(int i) {
Test::i = i;

}

int Test::getIValue() {
return Test::i;
}

void Test::setSValue(string s) {
Test::s = s;
}

string Test::getSValue() {
return Test::s;
}

void Test::HW() {
for (int j=0; j<getIValue(); j++) {
cout << getSValue() << endl;
}
}

int main() {

Test test;
test.setIValue(2);
test.setSValue("hello world!");
test.HW();

return 0;
}

输出为:hello world!
hello world!
总结:在C++中,类和结构之后记得加“;”号.结构和类的用法几乎完全一致,但是结构中的变量和函数默认都为public.另外,在结构中,不能直接初始化其中的变量.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

struct Test {
string name;
int age;
char sex;

void hello();
};

void Test::hello() {
cout << "hello " << name << endl;
}

int main() {
/*
Test test = {
"chris",
24,
'm'
};


cout << test.name << endl << test.age << endl << test.sex << endl;
*/

Test test1[2] =
{
{"chris", 24, 'm'},
{"harry", 23, 'f'}

};

for(int i=0; i<2; i++) {
cout << test1[i].name << endl << test1[i].age << endl << test1[i].sex << endl;
test1[i].hello();
}

return 0;
}

输出为:
chris
24
m
hello chris
harry
23
f
hello harry
总结:可像以上代码一样初始化结构中的变量.


4. 枚举:
#include <iostream>
#include <cstring>
#include <string>

using namespace std;


int main() {
enum color {red, blue, green, yellow};
cout << red << '\t' << blue << '\t' << green << '\t' << yellow << endl;

enum number {first, second=100, third=125, fourth=125, fifth, sixth};
cout << first << '\t' << second << '\t' << third << '\t' << fourth << '\t' << fifth << '\t' << sixth << endl;

color myFlag1,myFlag2;
myFlag1 = color(2);
cout << myFlag1 << endl;
myFlag2 = color(5);
cout << myFlag2 << endl;

return 0;
}

输出为:
0 1 2 3
0 100 125 125 126 127
2
5
总结:熟悉枚举的使用规则.


5 指针和数字:
#include <iostream>
#include <cstring>
#include <string>

using namespace std;


int main() {

int num = 5;
int *p1, *p2;

p1 = &num;
p2 = new int;

*p2 = 6;

cout << *p1 << '\t' << p1 << endl;
cout << *p2 << '\t' << p2 << endl;

delete p2;

cout << *p2 << '\t' << p2 << endl;

return 0;
}

输出为:
5 0xbfc91268
6 0x9fc5008
0 0x9fc5008


6. 使用new来创建动态数组:
在编译时给数组分配内存被称为静态联编,意味着数组是在编译时加入到程序中的;但使用new时,如果在运行阶段需要数组,则创建它;如果不需要,则不创建.还可以在程序运行时选择数组的长度.这被称为动态联编,意味着数组是在程序运行时创建的.这种数组叫做动态数组.使用静态联编时,必须在编写程序时指定数组的长度;使用动态联编时,程序将在运行时确定数组的长度.
#include <iostream>
#include <cstring>
#include <string>

using namespace std;


int main() {

int* pt = new int[5];
for(int i=0; i<5; i++) {
pt[i] = i;
}

for(int j=0; j<5; j++) {
cout << *(pt+j) << endl;
}

delete [] pt;

return 0;
}

输出为:
0
1
2
3
4


7. 二维数组:
#include <iostream>
#include <cstring>
#include <string>

using namespace std;


int main() {

int numArray[3][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};

for(int i=0; i<3; i++) {
for(int j=0; j<4; j++) {
cout << numArray[i][j] << '\t';
}
cout << endl;
}

return 0;
}

输出为:
1 2 3 4
2 3 4 5
3 4 5 6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值