C++刷题常用语法
介绍容器等常用的C++模块
C++的一些常用定义方法
定义结构体
| struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };
|
定义和调用类中的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream> using namespace std; class Point{
public: void setPoint(int x, int y) xPos = x; yPos = y; } void printPoint() cout<< "x = " << xPos << endl; cout<< "y = " << yPos << endl; }
private: int xPos; int yPos; }; int main(){ Point M; M.setPoint(10, 20); M.printPoint(); return 0; }#include <iostream> using namespace std; class Point{
public: void setPoint(int x, int y) xPos = x; yPos = y; } void printPoint() cout<< "x = " << xPos << endl; cout<< "y = " << yPos << endl; }
private: int xPos; int yPos; }; int main(){ Point M; M.setPoint(10, 20); M.printPoint(); return 0; }
|
常用数据类型
C++常用容器
Vector