C++ STL 和 类的构造
STL
- vertor 向量数组,就和数组差不多
- string 字符串容器, 能够对字符串进行修改、排序、插入等等
- deque 双端队列容器, 能够往队列的前面和后面都可以进行插入(push)
- list 列表容器, 双向链表,可以删除某个值, 在某个位置前面插入,在链表尾部插入
- set/multiset 集合类容器, 可以自动对关键字进行升序排序,可以返回容器中出现的elem元素的次数和寻找某个关键字
- map/multimap 映射类容器, 主要通过insert插入,可以查找某个元素(key)
- stack 栈容器, push压入,pop推出,top输出栈顶元素
- queue 队列容器, push进队, pop出队,front输出队头元素
个例(vector 排序)
#include <bits/stdc++.h>
using namespace std;
struct Student
{
int no;
string sex;
int score;
};
void printfStu(Student stu) {
cout << stu->no << " " << stu->sex << " " << endl;
}
bool cmp(Student a, Student b) {
if (a.no == b.no)
return a.score < b.score;
else
return a.no < b.no;
}
int main(){
vector<Student> a;
vector<Student>::iterator it;
Student student = {.no = 12, .sex="男", 99};
Student stu = {13, "女", 100};
Student stu1 = {11, "男", 67};
Student stu2 = {11, "女", 100};
a.push_back(student);
a.push_back(stu);
a.push_back(stu1);
a.push_back(stu2);
sort(a.begin(), a.end(), cmp);
for (it = a.begin(); it != a.end(); it++) {
cout << it->no << " " << it->sex << endl;
}
system("Pause");
return 0;
}
类的构造(面向对象)
#include <bits/stdc++.h>
using namespace std;
class Box{
public:
int getLength(void){
return length;
}
void setLength(int len){
this->length = len;
}
int getSize(void){
return size;
}
void setSize(int size){
this->size = size;
}
Box(int len, int size);
void toString() {
cout << this->getLength() << " " << this->getSize() << endl;
}
Box();
~Box(){};
private:
int length;
int size;
};
Box::Box(int len, int size) {
this->length = len;
this->size = size;
}
void printfBox(Box box) {
cout << box.getLength() << " " << box.getSize() << endl;
}
int main(){
Box box1(1,3);
box1.toString();
system("Pause");
return 0;
}