?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;//无参
vector<int> v2(5,10);//五个十
//判断是否为空
cout << v1.empty() << endl;
cout << v2.empty() << endl;
//返回其实元素的引用
//cout << v1.front() << endl;没有值执行会错误
cout << v2.front() << endl;
//返回最后成员的引用
cout << v2.back() << endl;
//赋值到vector中
v1.assign(5,20);
//查询单个位置的元素
cout << v1.at(2) << endl;
//返回指向起始位置的迭代器
//cout << v1.begin() << endl;
cout << *(v1.begin()) << endl;
//迭代器,类似于指针,可以直接取*操作,但是不能直接输出
//返回末尾位置的迭代器
//cout<< *(v1.end()) << endl;最后一个位置后一位的值
cout<< *(v1.end()-1) << endl;
//vector返回重新分配内存前容纳的个数
cout << v1.capacity() << endl;
v1.push_back(100);
cout << v1.capacity() << endl;
cout << v1.size() << endl;
return 0;
}
#include <iostream>
#define MAXSIZE 10
using namespace std;
template<typename T>
class Member
{
T* list;
T len = 0;
public:
Member ():list(new T){}
Member (T a):list(new T(a)){}
Member (T a,T b):list(new T(a)),len(b){}
~Member(){delete list;}
//增加
bool list_insert(T a);
//删除
bool list_delect();
//元素查找
int list_search(T a);
//下标修改
bool list_data(T a,T b);
void show();
};
//添加
template<typename T>
bool Member<T>::list_insert(T a)
{
if(len<0||len>MAXSIZE)
return false;
T *temp=list;
*(temp+len)= a;
len++;
return true;
}
template<typename T>
bool Member<T>::list_delect()
{
if(len==0)
return false;
T* temp=list;
cout << "删除 " << *(temp+len-1) << endl;
len--;
return true;
}
template<typename T>
void Member<T>::show()
{
for(int i=0;i<len;i++)
{
cout << *(list+i) << endl;
}
}
template<typename T>
int Member<T>::list_search(T a)
{
if(len == 0)
return -1;
T* temp=list;
int i=0;
while((i++)<len)
{
if(*(temp+i) == a)
return i;
}
return -1;
}
template<typename T>
bool Member<T>::list_data(T a,T b)
{
if(a<0||a>=len)
return false;
T*temp=list;
*(temp+a)=b;
return true;
}
int main()
{
Member<int> arr;
arr.list_insert(10);
arr.list_insert(20);
arr.list_insert(30);
arr.list_insert(40);
arr.list_insert(50);
arr.show();
arr.list_delect();
arr.show();
int post=arr.list_search(20);
cout << "下标为 " << post << endl;
arr.list_data(2,100);
arr.show();
return 0;
}