day8: coding
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// stl 标准模板库开发
|
||||
// 初次使用:
|
||||
// vector 存放基础类型的数据示例
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> v1;
|
||||
v1.push_back(1);
|
||||
v1.push_back(2);
|
||||
v1.push_back(3);
|
||||
v1.push_back(4);
|
||||
v1.push_back(5);
|
||||
v1.push_back(6);
|
||||
|
||||
// 创建迭代器
|
||||
vector<int>::iterator it;
|
||||
for (it = v1.begin(); it != v1.end(); it++)
|
||||
{
|
||||
cout << *it << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// 存放类对象的数据 vector
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Student
|
||||
{
|
||||
friend ostream &operator<<(ostream &out, const Student &stu);
|
||||
|
||||
private:
|
||||
string name;
|
||||
int age;
|
||||
|
||||
public:
|
||||
Student(const string &name, int age)
|
||||
{
|
||||
this->name = name;
|
||||
this->age = age;
|
||||
}
|
||||
};
|
||||
|
||||
ostream &operator<<(ostream &out, const Student &stu)
|
||||
{
|
||||
out << "name: " << stu.name << ", age: " << stu.age;
|
||||
return out;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<Student> v;
|
||||
v.push_back(Student("zhangsan", 20));
|
||||
v.push_back(Student("lisi", 21));
|
||||
v.push_back(Student("wangwu", 22));
|
||||
v.push_back(Student("zhaoliu", 23));
|
||||
|
||||
v.pop_back(); // 删除最后一个元素
|
||||
|
||||
// 创建迭代器
|
||||
vector<Student>::iterator it = v.end();
|
||||
while (it != v.begin())
|
||||
{
|
||||
cout << *(--it) << endl; // 重载了<<运算符
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// 基于 d3.cpp 的改进
|
||||
// vector 存放对象的指针
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Student
|
||||
{
|
||||
friend ostream &operator<<(ostream &out, const Student *stu);
|
||||
|
||||
private:
|
||||
string name;
|
||||
int age;
|
||||
|
||||
public:
|
||||
Student(const string &name, int age)
|
||||
{
|
||||
this->name = name;
|
||||
this->age = age;
|
||||
}
|
||||
};
|
||||
|
||||
ostream &operator<<(ostream &out, const Student *stu)
|
||||
{
|
||||
out << "name: " << stu->name << ", age: " << stu->age;
|
||||
return out;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<Student *> v;
|
||||
v.push_back(new Student("张三", 18));
|
||||
v.push_back(new Student("李四", 19));
|
||||
v.push_back(new Student("王五", 20));
|
||||
|
||||
v.pop_back(); // 删除最后一个元素
|
||||
|
||||
// 创建迭代器
|
||||
vector<Student *>::iterator it = v.end();
|
||||
while (it != v.begin())
|
||||
{
|
||||
it--;
|
||||
cout << *it << endl; // 重载了<<运算符
|
||||
delete *it; // 释放空间
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
// 注意: vector<vector<int> > vs; 防止编辑器自动格式化为 >>
|
||||
vector<vector<int> > vs; // 嵌套容器
|
||||
vector<int> v1, v2, v3;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user