`
chrisongs
  • 浏览: 26007 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    C++ 基础教程笔记

    这是C++基础知识的笔记,对C++学习有很大的帮助

    C++Primer读书笔记:C++概述.pdf

    C++Primer中文第三版(C++从入门到精通)第一章的读书笔记,主要是C++程序、预处理器指示符、iostream库等的基础知识点读书笔记。

    C++自学笔记 各种计算机基础知识,比如简单的原码、补码和反码等

    各种计算机基础知识,比如简单的原码、补码和反码等。C++自学笔记。各种计算机基础知识,比如简单的原码、补码和反码等。C++自学笔记。各种计算机基础知识,比如简单的原码、补码和反码等。C++自学笔记。各种计算机...

    C++基础知识总结入门级

    学习C++必经之路,入门级知识总结,笔记本知识概况,走一步再走一步

    C++基础知识.md

    本人在c++学习过程中的一些随笔,比较乱,但是很实用。

    C++学习笔记

    该文档主要是C++学习的笔记记录,是一些C++的基础知识,主要用于互联网公司面试用途。

    C++基础知识总结

    该文档是我学完C语言再学C++的笔记,关于C语言与C++的区别做了一个全面的对比。包括类、继承与派生、多态性与虚函数等。如果你原就有一定的C语言基础,这份文档会让你学起C++来事半功倍,当然没有C语言基础的也可...

    C++个人整理笔记

    适合模糊者看到完美笔记,C++基础知识整理,欢迎观看,

    C++基础知识markdown文件,适用初学者备忘复习

    都是基础知识,俗话说“基础不牢,地动山摇”,学好基础知识能为后续的进一步深入学习打下牢固的基础。基础学的好以后深入学习也会更加轻松,打牢基础也会在今后的学习中又更大的底气。望诸君能在学习C++的路程中走...

    C++笔记源代码100例

    C++笔记源代码100例 适合初学者 ,有C++基础知识讲解以及例题

    C++基础知识1.docx

    C++初学者,C++信息学奥赛准备者,前三个月的学习笔记

    C++基础笔记,容易学习

    我自己整理得一些关于C++的笔记,非常有用,希望对大家有帮助!涵盖了许多C++的知识,比较好,适合初学者。

    c++学习笔记(零基础进阶之路).md

    个人整理的c/c++学习进阶方法,零基础轻松掌握关键知识点,是你在面向过程——&gt;面向对象学习过程中的辅助利器! 话不多说,开始吧!!!

    Visual+C++程序设计学习笔记

    本书适合于有 C++基础知识,但没有Visual C++编程经验的初学者,也可作为专业 VC开发程序员的参考书。 本书独一无二的优势 本书由具有多年开发和教学经验的资深老师执笔写作,笔者具有多年的 Visual C++开发和教学...

    sherpahu#AlgorithmsNotes#刷题笔记:C++基础知识——STL之pair&tuple1

    PAT基础知识——STL之pair&tuple定义用法初始化:使用大括号直接对first,second赋值查询用途替换二元结构体,初始化、获取元素值更方便用于m

    关于c++的笔记和体会心得

    c++笔记,介绍c++的基础知识。和一些基本的编程心得

    cpp基础知识笔记zzzz

    c++知识总结,内容细致。本人也是刚刚学c++ ,希望给刚刚学习c++的同学能有一点帮助,有些知识点有本质描述,有助于真正理解知识

    C和C++学习笔记

    C、C++、UNIX 下C开发的培训时笔记。用简洁的语言将C、C++基础知识讲解,并附带小程序,对初学者比较有用,资深玩家请勿下载,作用不大

    VS环境下的C++学习笔记20191209.rar

    VS环境下的C++学习笔记 包含C++基础操作&功能的测试笔记(基础...MCF框架的部分基础知识笔记 模型图形学|线性代数相关的基础笔记: OpenGL Eigen库 vcgLib MatLab 构建图形的方式 图像运算较为底层的业务逻辑等

Global site tag (gtag.js) - Google Analytics