美文网首页
C++基本语法

C++基本语法

作者: m2fox | 来源:发表于2020-04-05 20:28 被阅读0次

以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输出:2cout << '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++

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

  • 1: 两数之和(基本语法,类,map,vector)
  • 876: 链表的中间结点(struct,指针)

相关文章

  • C++学习(1) ---- 基本语法介绍

    C++ 基本语法 C++基础语法说明基本形式NA命名空间NA构造函数和析构函数NA实例化方法NA指针和引用NA静态...

  • C++ 学习(2) ---- 基本语法介绍

    C++ 基本语法(2) C++基础语法说明模板NA运算符重载NA强制类型转换static_cast,const_c...

  • C++基本语法

    C++程序可以理解为对象的集合,其基本语法和C相似,首先需要相应的环境,才能开始进行程序的编译并运行,结尾也必须使...

  • C++ 基本语法

    C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。 对象 - 对象具有状态和行为。例如:一只狗...

  • C++基本语法

    命名空间认识一下C++ 《命名空间》 cout = console output 控制台输出 参考资料C++ 基本语法

  • C++ 基本语法

    ——注释 单行注释,多行注释都与java相同 ——数据类型 七种基本的数据类型 bool char int flo...

  • C++ 基本语法

    C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。现在让我们简要地看一下什么是类、对象,方法、...

  • C++基本语法

    以leetcode刷题为驱动,学习C++基本语法。 1. 代码文件基本结构 hello world程序 假如上述代...

  • C++ 基础知识点大纲

    C++ C++对C的加强 namespace命名空间 C++命名空间基本概念 C++命名空间定义,使用语法,意义 ...

  • C和C++混合编程之 extern “C”的使用

    首先要明白: C++号称是C语言的超集,也确实,从语言的基本语法上,C++是包含所有C语言的语法的,而且C++为了...

网友评论

      本文标题:C++基本语法

      本文链接:https://www.haomeiwen.com/subject/kkrrphtx.html