前言:
这里做了比较全面的vector的增删查改操作,以及加入对象,可以理解为JAVA的Collection类,只是API的定义名称有差异而已。
Maqi对象:
#include <iostream>
#include <cstring>
using namespace std;
class Human {
public:
int weight;
int height;
public:
int getWeight() {
return weight;
}
virtual int getHeight() {
return height;
}
public:
Human(int weight, int height) : weight(weight = 100), height(height = 170) {}
Human() : weight(weight = 100), height(height = 170) {}
};
class SuperMan : public Human {
int getHeight() {
return height + 100;
}
};
class Maqi : public Human {
public:
string name;
int age;
string getName() {
return this->name;
}
void toString() {
cout << "for(auto item : num) :" << name << endl;
}
int getWeight() {
return Human::getWeight() + 100;
}
Maqi(int weight, int height) : Human(weight, height) {
cout << " Maqi()" << endl;
}
Maqi(string name) : Human() {
this->name = name;
}
Maqi() : Human() {
cout << " Maqi()" << endl;
}
virtual ~Maqi() {
cout << name << " ~Maqi()" << endl;
}
//注意const: 拷贝函数为maqi引用常量
Maqi(const Maqi &maqi) : Human() {
age = maqi.age;
name = maqi.name;
}
};
实现:
#include "collections.h"
#include <vector> //注意,没有 .h
#include <algorithm>
#include <iostream>
#include "reference.h"
using namespace std; //在 std 标准命名空间中
void getCollections() {
vector<int> one; //定义一个空的、元素类型是 int 的 vector 动态数组
vector<int> two(4, 100); //定义一个包含4个元素,每个元素的值都是100的 vector 动态数组
vector<int> three(two.begin(), two.end()); //使用 two 这个对象的迭代器,从开始到结束的所有元素来初始化当前对象
vector<int> four(three); // 使用 three 这个对象来初始化当前对象
vector<Maqi> mans; // 使用 three 这个对象来初始化当前对象
int myints[] = {16, 2, 77, 29};
//使用一个普通的 int 数组来初始化当前对象
vector<int> five(myints, myints + sizeof(myints) / sizeof(int));
cout << "==============clear=================" << endl;
five.clear();
cout << "==============push_back===add==============" << endl;
five.push_back(1);
five.push_back(2);
int &front = five.front();
int &back = five.back();
cout << "=====front==== " << front << endl;
cout << "=====back===== " << back << endl;
cout << "==============insert=================" << endl;
five.insert(five.begin(), 102);//插入到第0位 102
five.insert(five.begin() + 1, 103);//插入到第1位103
five.insert(five.begin() + 2, 3, 105);//插入到第3位,添加三个105
int sz_int[] = {4, 7, 9};
five.insert(five.begin() + 5, sz_int, sz_int + 3);//添加数组 类似于java的addAll()
cout << "=======pop_back========删除================" << endl;
five.pop_back();//弹出最后一个元素
five.pop_back();
cout << "=======erase========范围删除================" << endl;
five.erase(five.begin());//删除第一个元素
five.erase(five.begin(), five.end() - 1);//删除0~five.size()-1范围的元素
cout << "===============swap================" << endl;
five.swap(two);//类似于java的replace,这里替换的是five本身。
cout << "===============for_each================" << endl;
for (auto item : two) {
cout << "for(auto item : num) :" << item << endl;
}
cout << "===============================" << endl;
for (auto item : three) {
cout << "for(auto item : num) :" << item << endl;
}
cout << "===============================" << endl;
for (auto item : four) {
cout << "for(auto item : num) :" << item << endl;
}
cout << "===============================" << endl;
for (auto item : five) {
cout << "for(auto item : num) :" << item << endl;
}
cout << "===========操作对象====================" << endl;
cout << "mans.size() : " << mans.size() << endl;
cout << "mans.empty() : " << mans.empty() << endl;
Maqi maqi("maqi_1");
Maqi maqi2("maqi_2");
Maqi maqi3("maqi_3");
Maqi *maqi_5 = new Maqi("maqi_5");
mans.insert(mans.begin(), maqi);
mans.push_back(maqi2);
mans.push_back(maqi3);
mans.push_back(*maqi_5);
//迭代器遍历
vector<Maqi>::iterator it;
cout << "mans.size()" << mans.size() << endl;
it = mans.begin();
while (it != mans.end()) {
cout << "age:" << (*it).age << " name:" << (*it).name << endl;
it++;
}
//普通遍历
for (Maqi &item : mans)
item.toString();
}
输出结果:
==============clear=================
==============push_back===add==============
=====front==== 1
=====back===== 2
==============insert=================
=======pop_back========删除================
=======erase========范围删除================
===============swap================
===============for_each================
for(auto item : num) :9
===============================
for(auto item : num) :100
for(auto item : num) :100
for(auto item : num) :100
for(auto item : num) :100
===============================
for(auto item : num) :100
for(auto item : num) :100
for(auto item : num) :100
for(auto item : num) :100
===============================
for(auto item : num) :100
for(auto item : num) :100
for(auto item : num) :100
for(auto item : num) :100
===========操作对象====================
mans.size() : 0
mans.empty() : 1
maqi_1 ~Maqi()
maqi_1 ~Maqi()
maqi_2 ~Maqi()
mans.size()3
age:10 name:maqi_1
age:1990672645 name:maqi_2
age:1991256352 name:maqi_3
for(auto item : num) :maqi_1
for(auto item : num) :maqi_2
for(auto item : num) :maqi_3
for(auto item : num) :maqi_5
//自动释放
maqi_3 ~Maqi()
maqi_2 ~Maqi()
maqi_1 ~Maqi()
maqi_1 ~Maqi()
maqi_2 ~Maqi()
maqi_3 ~Maqi()
总结:
1.C++不想java更趋向于底层,数组对象的插入涉及到拷贝函数,且拷贝函数需要重写
这里做出了解释https://blog.csdn.net/weixin_41066275/article/details/80746002
2.iterator的方式遍历,更具有逼格。