C++基本语法

2020-04-05  本文已影响0人  m2fox

以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++98 (1.0)
C++11 (2.0, leetcode-cn用此版本)
C++14
# 更新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文件中:

3. 变量

字符

数组

4. 控制结构

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

#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

#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++

11. leetcode.cn C++语法模板题解

上一篇 下一篇

猜你喜欢

热点阅读