先写一个HelloWorld
#include <iostream>
//std指标准库
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
运行结果
Hello, World!
数组
#include <iostream>
//std指标准库
using namespace std;
int main() {
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
// sizeof可以获取数据类型的大小
int len = sizeof(arr) / sizeof(arr[0]);
for (int index = 0; index < len; ++index) {
cout << arr[index] << endl;
}
arr[2] = 5;
// 获取数组地址指针
int *p = arr;
// 获取数组地址偏移量2的值,即为arr[2]
cout << *(p + 2) << endl;
// 二维数组
int a[2][4] = {{1,2,3,4},{5,6,7,8}};
for (int row = 0; row < 2; ++row) {
for (int col = 0; col < 4; ++col) {
cout << a[row][col] << " ";
}
cout << endl;
}
return 0;
}
运行结果
0
1
2
3
4
5
6
7
8
9
5
1 2 3 4
5 6 7 8
动态数组Vector
使用简单数组无法实现动态扩容插入元素,因为容量有限
#include <iostream>
#include <vector>
//std指标准库
using namespace std;
//结构体
struct Display {
// 重载运算符()
void operator()(int i) {
cout << i << " ";
}
};
int main() {
// 可扩容的数组
vector<int> vec = {1,2,3,4};
// 从尾部插入
vec.push_back(5);
// 从第二个位置后插入
vec.insert(vec.begin() + 2, 9);
// 从最后一个位置删除
vec.pop_back();
// 从头部位置删除
vec.erase(vec.begin());
for (int index = 0; index < vec.size(); ++index) {
cout << vec[index] << endl;
}
// 获取动态数组的容量
cout << vec.capacity() << endl;
// 动态数组当前存储的容量
cout << vec.size() << endl;
int arr[] = {1,2,3,4,5};
// 将数组转化为动态数组
vector<int> aVec(arr,arr + 5);
for_each(aVec.begin(),aVec.end(),Display());
return 0;
}
运行结果
2
9
3
4
8
4
1 2 3 4 5
字符串
字符串变量:字符串是以空字符'\0'结束的字符数组,空字符'\0'自动添加到字符串的内部表示中。在声明字符串变量的时候,应该为这个空结束符预留一个额外元素空间。
#include <iostream>
//std指标准库
using namespace std;
int main() {
// 如果要写数字的话,这里要写11而不是10
// char str[11] = {"helloworld"};
char str[] = {"helloworld"};
cout << str << endl;
return 0;
}
运行结果
helloworld
字符串常量:字符串常量是一对双引号括起来的字符序列,字符串中每个字符作为一个数组元素存储。
#include <iostream>
//std指标准库
using namespace std;
int main() {
char str[] = "helloworld";
cout << str << endl;
return 0;
}
字符串指针:char[]和char*的区别,一个是地址,一个是地址存储的信息;一个可变,一个不可变。
#include <iostream>
//std指标准库
using namespace std;
int main() {
char str[] = "helloworld";
char* pStr1 = "helloworld";
char* pStr2 = str;
char str1[] = "helloworld";
// 此时str和str1相同,返回0
cout << strcmp(str,str1) << endl;
// 数组下的内容可以修改
for (int index = 0; index < 10; ++index) {
str[index] += 1;
}
cout << str << endl;
// 常量指针下的内容不可修改
// for (int index = 0; index < 10; ++index) {
// pStr[index] += 1;
// }
// 指向数组地址的指针下的内容可以修改
for (int index = 0; index < 10; ++index) {
pStr2[index] += 1;
}
cout << pStr1 << endl;
cout << pStr2 << endl;
// 获取字符串长度
cout << strlen(str) << endl;
// 获取字符串的容量,包含了结束符
cout << sizeof(str) << endl;
// 此时是jqnnqyqtnf与helloworld比较,j比h大2,返回2
cout << strcmp(str,str1) << endl;
// 将str拼接到str1的末尾
strcat(str1,str);
cout << str1 << endl;
// 将str复制到str1上
strcpy(str1,str);
cout << str1 << endl;
// 查找q字符在str中第一次出现的位置,并返回后面的字符串
cout << strchr(str,'q') << endl;
// 查找qy字符串在str中第一次出现的位置,并返回后面的字符串
cout << strstr(str,"qy") << endl;
return 0;
}
运行结果
0
ifmmpxpsme
helloworld
jgnnqyqtnf
10
11
2
helloworldjgnnqyqtnf
gnnqyqtnf
qyqtnf
qyqtnf
新型字符串
#include <iostream>
#include <string>
//std指标准库
using namespace std;
int main() {
string s1 = "hello";
string s2 = "world";
cout << s1.length() << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
cout << s1[0] << endl;
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
// 转换为C风格的字符串
const char* c_str = s1.c_str();
cout << c_str << endl;
// 拷贝字符串
string s = s1;
cout << s << endl;
// 字符串的连接
cout << s + s1 << endl;
return 0;
}
运行结果
5
5
22
h
0
1
hello
hello
hellohello
链表
list就是数据结构中的双向链表,因此它的内存空间是不连续的,通过指针来进行数据的访问。
#include <iostream>
#include <list>
//std指标准库
using namespace std;
//结构体
struct Display {
// 重载运算符()
void operator()(int i) {
cout << i << " ";
}
};
int main() {
list<int> aList = {1,2,3,4,5};
aList.push_back(9);
aList.push_back(8);
aList.push_front(7);
aList.pop_back();
// 逆序
aList.reverse();
for_each(aList.begin(),aList.end(),Display());
return 0;
}
运行结果
9 5 4 3 2 1 7
队列
deque是一个双向队列,优化了对序列两端元素进行添加和删除操作的基本序列容器。通常由一些独立的区块组成,第一个区块朝某方向发展,最后一个区块朝另一个方向发展。它允许较为快速地随机访问但它不像vector一样把所有对象保存在一个连续的内存块,而是多个连续的内存块。并且在一个映射结构中保存对这些块以及顺序的跟踪。
#include <iostream>
#include <deque>
#include <queue>
//std指标准库
using namespace std;
//结构体
struct Display {
// 重载运算符()
void operator()(int i) {
cout << i << " ";
}
};
int main() {
deque<int> deque = {1,2,3,4,5};
// 从尾部插入
deque.push_back(5);
// 从第二个位置后插入
deque.insert(deque.begin() + 2, 9);
// 从最后一个位置删除
deque.pop_back();
// 从头部位置删除
deque.erase(deque.begin());
for_each(deque.begin(),deque.end(),Display());
cout << endl;
// 优先队列
priority_queue<int> priorityQueue;
priorityQueue.push(1);
priorityQueue.push(2);
priorityQueue.push(5);
while (!priorityQueue.empty()) {
cout << priorityQueue.top() << endl;
priorityQueue.pop();
}
return 0;
}
运行结果
2 9 3 4 5
5
2
1
栈
stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,也就是说实现了一个先进后出(FILO)的数据结构
#include <iostream>
#include <stack>
//std指标准库
using namespace std;
int main() {
stack<int> sk;
sk.push(1);
sk.push(3);
sk.push(5);
while (!sk.empty()) {
cout << sk.top() << " ";
sk.pop();
}
return 0;
}
运行结果
5 3 1
以上都是序列式容器,还有一种是关联式容器。
集合
set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。
#include <iostream>
#include <set>
//std指标准库
using namespace std;
int main() {
set<int> s;
// 迭代器
set<int>::iterator iter;
s.insert(5);
s.insert(2);
s.insert(3);
s.insert(2);
cout << s.size() << endl;
for (iter = s.begin(); iter != s.end() ; ++iter) {
cout << *iter << endl;
}
return 0;
}
运行结果
3
2
3
5
映射
map是STL的一个关联容器,它提供一对一的hash。
#include <iostream>
#include <map>
//std指标准库
using namespace std;
struct Display {
void operator()(pair<string,double> info) {
cout << info.first << ":" << info.second << endl;
}
};
int main() {
map<string,double> studentScores;
studentScores["李明"] = 95.0;
studentScores.insert(pair<string,double>("刘刚",100));
studentScores.insert(map<string,double>::value_type("张丽",98.5));
for_each(studentScores.begin(),studentScores.end(),Display());
map<string,double>::iterator iter;
iter = studentScores.find("李明");
if (iter != studentScores.end()) {
cout << iter->second << endl;
}
return 0;
}
运行结果
刘刚:100
张丽:98.5
李明:95
95
内存由很多内存单元组成。这些内存单元用于存放各种类型的数据。计算机对每个内存单元都进行了编号,这个编号就称为内存地址,地址决定了内存单元在内存中的位置。记住这些内存单元地址不方便,于是C++语言的编译器让我们通过名字来访问这些内存位置。
#include <iostream>
//std指标准库
using namespace std;
int main() {
int a = 112, b = -1;
float c = 3.14;
int* d = &a;
float* e = &c;
// 打印内存地址编号
cout << d << endl;
cout << e << endl;
// 打印内存地址的内容
cout << *d << endl;
cout << *e << endl;
int f[4] = {1,2,3,4};
// 数组的指针
int(*g)[4] = &f;
for (int i = 0; i < 4; ++i) {
cout << (*g)[i] << " ";
}
return 0;
}
运行结果
0x7ffee5200218
0x7ffee5200210
112
3.14
1 2 3 4
引用
引用是一种特殊的指针,不允许修改的指针。使用引用不存在空引用,必须初始化,一个引用永远指向它初始化的那个对象。可以认为是指定变量的别名,使用时可以认为是变量本身。
#include <iostream>
#include <assert.h>
//std指标准库
using namespace std;
//使用引用传递可以不产生栈变量
void swap(int& a,int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 1,y = 3;
// 定义一个引用
int& rx = x;
rx = 2;
cout << x << endl;
cout << rx << endl;
rx = y;
cout << x << endl;
cout << rx << endl;
int a = 3,b = 4;
swap(a,b);
assert(a == 4 && b == 3);
return 0;
}
运行结果
2
2
3
3
C++使用struct,class来定义一个类,struct的默认成员权限是public,class的默认成员权限是private,除此之外,二者基本无差别。
#ifndef UNTITLED1_COMPLEX_H
#define UNTITLED1_COMPLEX_H
class Complex {
public:
// 构造函数
Complex();
Complex(double r,double i);
// 析构函数
virtual ~Complex();
double getReal() const;
void setReal(double r);
double getImage() const;
void setImage(double i);
// 运算符重载
Complex operator+(const Complex& x);
Complex& operator=(const Complex& x);
private:
double _real;
double _image;
};
#endif //UNTITLED1_COMPLEX_H
#include "Complex.h"
#include <iostream>
using namespace std;
Complex::Complex() {}
Complex::Complex(double r,double i) {
_real = r;
_image = i;
cout << "Complex" << endl;
}
Complex::~Complex() {
cout << "~Complex" << endl;
}
double Complex::getReal() const {
return _real;
}
void Complex::setReal(double r) {
_real = r;
}
double Complex::getImage() const {
return _image;
}
void Complex::setImage(double i) {
_image = i;
}
//此处使用引用可以不必再分配内存产生栈对象
Complex Complex::operator+(const Complex &x) {
Complex temp;
temp._real = _real + x._real;
temp._image = _image + x._image;
return temp;
}
Complex& Complex::operator=(const Complex &x) {
// 这里的&x是一个地址,不是引用
if (this != &x) {
_real = x._real;
_image = x._image;
}
return *this;
}
int main() {
Complex c(1.0,2.0);
cout << c.getReal() << endl;
cout << c.getImage() << endl;
c.setReal(3.0);
c.setImage(4.0);
cout << c.getReal() << endl;
cout << c.getImage() << endl;
Complex d(8.0,9.0);
Complex e;
e = c + d;
cout << e.getReal() << endl;
cout << e.getImage() << endl;
return 0;
}
运行结果
Complex
1
2
3
4
Complex
~Complex
11
13
~Complex
~Complex
~Complex