以leetcode刷题为驱动,学习C++基本语法。
1. 代码文件基本结构
hello world
程序
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
return 0;
}
假如上述代码保存在test.cpp
文件中,使用命令行命令:g++ test.cpp -o test
进行编译,生成一个名为test
的可执行文件,然后使用命令./test
执行,输出:
hello world
main
函数
一个程序必须要有一个返回值为int
类型的main
函数。
#include
语句
在代码中使用的cout
输出函数,就依赖于导入iostream
输入输出标准库:#include <iostream>
命名空间
使用std
命名空间:using namespace std;
C++版本
- C++版本列表:
C++98 (1.0)
C++11 (2.0, leetcode-cn用此版本)
C++14
- 当前最普遍使用的C++版本:C++11
- leetcode-cn使用的C++版本是C++11
- 在运行时指定C++版本为C++11版本:
g++ -std=c++11 test.cpp -o test
- macos升级g++版本并配置环境变量:
# 更新g++版本:
brew search gcc
brew install gcc@4.9
# 打开~/.bash_profile并添加如下3行内容:
alias gcc="gcc-4.9 -std=c++11"
alias g++="g++-4.9 -std=c++11"
alias c++="c++-4.9 -std=c++11"
# 使修改生效:
source ~/.bash_profile
2. 代码运行与调试
假如代码保存在test.cpp
文件中:
- 编译:
g++ test.cpp -o test
,编译完后会生成一个名为test
的可执行文件。 - 添加调试选项进行编译:
g++ test.cpp -g -o test
- 运行可执行文件:
./test
3. 变量
字符
- 字符型变量可以作为整型进行运算:
cout << 'c' - 'a' << endl
输出:2
;cout << 'c'*'a' << endl
输出:9603
数组
- 定义一个长度为10的整型数组,并默认给每个元素赋值为默认值0:
int a[10] = {0};
4. 控制结构
for循环
- for循环的几种形式:
int main(){
string s = "01234";
for (int i = 0; i < s.size(); i++) {
cout << s[i] << " ";
}
cout << endl;
for (char c: s) {
cout << c << " ";
}
return 0;
}
上面代码输出:
0 1 2 3 4
0 1 2 3 4
5. 输入输出
6. 函数
先声明,再使用
7. 指针
8. 面向对象
struct
- 创建一个struct并使用:
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(NULL) {} // 带一个参数的构造函数
ListNode(int x, ListNode *n): val(x), next(n) {} // 带两个参数的构造函数
};
int main() {
ListNode n2(2);
ListNode head(1, &n2); // 用"&"取地址
cout << head.val << endl << head.next->val << endl; // 用"."取对象的字段,用"->"取指针的字段
return 0;
}
输出:
1
2
class
- 创建一个class并使用对象:
#include <iostream>
using namespace std;
class MyObj {
public: // 公共方法
int sum(int a, int b) {
return a + b;
}
void print(int n) {
cout << n << endl;
}
}; // 这里必须有分号
int main() {
MyObj obj;
cout << obj.sum(1, 2) << endl;
obj.print(5);
return 0;
}
输出:
3
5
9. 标准库
字符串
string
- 基本用法:
#include <iostream>
#include <string>
using namespace std;
int main(){
// - 获取某个下标的字符(下标从0开始)
string s = "01234";
cout << s[1] << endl;
// - 获取字符串的长度
cout << s.length() << endl;
cout << s.size() << endl;
// - 在末尾追加字符串
s.append("abc");
cout << s << endl;
// - 获取子串
// 获取子串,子串是从下标为4到末尾的子串
cout << s.substr(4) << endl;
// 获取子串,子串是从下标为1到下标为3的子串(包含下标3)
cout << s.substr(1, 3) << endl;
return 0;
}
输出:
1
5
5
01234abc
4abc
123
容器
vector
- 基本用法:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// 用列表初始化vector
vector<int> nums = {1, 2, 3};
cout << nums.size() << endl << nums.empty() << endl << nums[1] << endl << "---" << endl;
// 第二种创建方式
vector<int> nums2 = vector<int>{4,5};
cout << nums2.size() << endl << "---" << endl;
// 创建一个空的vector
vector<int> nums3 = vector<int>();
cout << nums3.empty() << endl << "---" << endl;
return 0;
}
输出:
3
0
2
---
2
---
1
---
map
- 基本用法:
#include <iostream>
#include <map>
using namespace std;
int main() {
// 字符串键值对map
map<string, string> myMap;
myMap["key1"] = "value1";
myMap["key2"] = "value2";
cout << myMap["key3"] << endl;
cout << myMap["key1"] << endl;
cout << myMap["key2"] << endl;
// 指定默认值的map
struct IntWithDefault{
int val = -1;
};
map<int, IntWithDefault> defaultMap;
defaultMap[1001] = {98};
cout << defaultMap[1001].val << endl;
cout << defaultMap[1002].val << endl;
return 0;
}
输出:
value1
value2
98
-1
pair
#include <iostream>
using namespace std;
int main() {
pair<int, int> p(1,2);
cout << p.first << " " << p.second << endl;
return 0;
}
输出:
1 2
10. DevC++
- Windows和DevC++配置g++8.2的方法: https://blog.yangjerry.tw/2019/11/30/devcplusplus-gplusplus-8/
- 配置代码格式化快捷键:工具->快捷键选项进行修改
- 配置代码格式化选项:AStyle->格式化选项
11. leetcode.cn C++语法模板题解
- 1: 两数之和(基本语法,类,map,vector)
- 876: 链表的中间结点(struct,指针)
网友评论