目录
定长数组
动态数组
vector与array的嵌套
定长数组
- array
#include<iostream>
#include<array>
#include<string>
using namespace std;
template<class _Ty,size_t size>
class MyArray {
public:
_Ty* begin() {
return Memory + 0;
}
_Ty* end() {
return Memory + size;
}
MyArray() { Memory = new _Ty[size]; }
_Ty& operator[](int index) { return Memory[index]; }
~MyArray() { delete[]Memory; }
class iterator {
public:
void operator=(_Ty* pmove) { this->pmove = pmove; }
bool operator!=(_Ty* pmove) { return this->pmove !=pmove; }
iterator operator++(int) { this->pmove++; return (*this); }
_Ty operator*() { return *pmove; }
iterator(_Ty* pmove=nullptr):pmove(pmove) {}
protected:
_Ty* pmove;
};
protected:
_Ty* Memory;
};
void TestMyArray() {
MyArray<int, 3>::iterator iter;
MyArray<int, 3>array1D;
for (int i = 0; i < 3; i++)
array1D[i] = i;
for (iter = array1D.begin(); iter != array1D.end(); iter++)
cout << *iter << " ";
}
void Testarray() {
//存储类型int
//数组长度3
//类模板都是用的对象,而不是new对象
array<int, 3>array1D;
array<int, 3>* p = new array<int, 3>;
for (int i = 0; i < array1D.size(); i++)
array1D[i] = i;
}
void testEXoperator() {
//使用:和数组一样的用法
array<int, 3> num = { 1,2,3 };
cout << num.empty() << endl;
cout << num.size() << endl;
num.fill(4);
for (auto v : num)//循环输出num中的所有元素
cout << v<<" ";
cout << endl;
array<int, 3> num1 = { 2,3,4 };
num1.swap(num);//交换:长度要一样长
for (auto v : num)
cout << v << " ";
cout << endl;
}
class Miss {
public:
Miss(){}
Miss(string name,int age):name(name),age(age){}
void print() { cout << name << " " << age << endl; }
protected:
string name;
int age;
};
void testMiss() {
array<Miss, 3> array2D;
array<Miss, 3>::iterator iter;
for (int i = 0; i < array2D.size(); i++) {
string name = "name";
array2D[i] = Miss(name + to_string(i), 16 + i);
}
for (auto v : array2D) {
v.print();
}
//end最后一个位置,不是最后一个元素的位置
(*array2D.begin()).print();
(*(array2D.end() - 1)).print();//(*array2D.end()).print();越界访问
//迭代器访问
//对象模仿指针, *迭代器就是取值运算
for (iter=array2D.begin();iter!=array2D.end(); iter++)
(* iter).print();
}
int main() {
testEXoperator();
testMiss();
TestMyArray();
return 0;
}
动态数组
- vector
#include<iostream>
#include<vector>
#include<string>
using namespace std;
template<class _Ty>
void print(vector<_Ty>& temp) {
for (auto v : temp)
cout << v << " ";
}
void testVec() {
//模板类型:存储数据类型
//不带长度的创建方式
vector<int>data;
//只能用成员函数插入
for (int i = 0; i < 3; i++)
data.push_back(i) ;
print(data);
cout << endl;
//带长度
vector<string>strData(3);//当前动态数组的长度是3
//确定长度可以直接用数组插入
for (int i = 0; i < 3; i++){
string name = "name";
strData[i] = name + to_string(i);
}
strData.push_back("name3");//在这个函数中做了自动扩增
print(strData);
cout << endl;
//带初始化
vector<float>dData = { 1.1,1.2,1.3 };//自动算出长度为3
print(dData);
cout << endl;
}
class Miss {
public:
Miss(string name ,int age):name(name),age(age){}
friend ostream& operator<<(ostream& out,const Miss& miss) {
out << miss.name << " " << miss.age;
return out;
}
protected:
string name;
int age;
};
void testMiss() {
vector<Miss>data;
for (int i = 0; i < 3; i++){
string name = "name";
data.push_back(Miss(name + to_string(i),18+i));
}
print(data);
}
void testEXoperator() {
vector<int>idata = { 0,1,2,3,4 };
cout << idata.size() << endl;//当前元素中的元素个素
cout << idata.empty() << endl;//判断是否为空,return size==0;有元素返回false
cout << idata.front() << endl;//访问第一个元素
cout << idata.back() << endl;//访问最后一个元素
cout << idata.at(2) << endl;//下标方式访问
cout << idata[2] << endl;//跟楼上效果一样
//修改
idata.emplace(idata.begin() + 2, 100);//修改下标是2的元素的值为100
idata.emplace_back(999);//在最后插入999
print(idata);
//删除
idata.erase(idata.begin()+2);//数组只有伪删除,没有删除操作
print(idata);
//批量复制
int array[] = { 1,2,3 };
vector<int>vecData;
vecData.assign(array, array + 3);//不需要起始长度
print(vecData);
}
int main() {
testVec();
testMiss();
testEXoperator();
return 0;
} vector与array的嵌套 #include<iostream>
#include<string>
#include<array>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
void arrayVSarray()//定态数组嵌套定态数组
{
array<array<int, 3>, 4>data;
for(int i=0;i<4;i++){
for (int j = 0; j < 3; j++){
data[i][j] = i * j;
cout << data[i][j]<<" ";
}
cout << endl;
}
}
void vectorVSvector()//动态数组嵌套动态数组
{
vector<vector<int>>data;
srand((unsigned int)time(nullptr));
for (int i = 0; i < 4; i++) {
vector<int>temp;
for (int j = 0; j < rand()% 3 + 2; j++) {
temp.push_back(i * j);
}
data.push_back(temp);
}
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
void arrayVSvector()//定态数组嵌套定态数组
{
array<vector<int>, 3>data;
vector<int>vec[] = { {1,2,3},{2,3,5},{3,4,6} };
for (int i = 0; i < data.size(); i++)
data[i] = vec[i];
for (int i = 0; i < data.size(); i++){
for (int j = 0; j < data[i].size();j++)
cout << data[i][j] << " ";
}
cout << endl;
}
void vectorVSarray()//定态数组嵌套定态数组
{
vector<array<int, 3>>data;
array<int, 3>arr[] = { {1,2,3},{2,3,5},{5,6,7} };
for (int i = 0; i < 3; i++)
data.push_back(arr[i]);
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data[i].size(); j++)
cout << data[i][j] << " ";
}cout << endl;
}
int main() {
vectorVSvector();
arrayVSvector();
vectorVSarray();
return 0;
}
|