vector:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myVec;
//新增元素
myVec.push_back(1);
myVec.push_back(2);
myVec.push_back(3);
myVec.push_back(4);
cout << "初始化元素:" << endl;
for (vector<int>::iterator it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << endl;
cout << "计算元素个数:" << endl;
int count = myVec.size(); // 计算元素个数
cout << count << endl;
if (count > 2)
{
auto it = myVec.begin();
myVec.erase(it, (it+1)); //删除元素
}
cout << "删除第一个元素后:" << endl;
for (vector<int>::iterator it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << endl;
myVec.clear(); //清除所有元素
cout << "清除元素后:" << endl;
for (vector<int>::iterator it = myVec.begin(); it != myVec.end(); ++it)
cout << *it << endl;
return 0;
}
map:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef struct itemstruct
{
int a;
char b[20];
}itemS;
itemS s[4] =
{
{102,"what"},
{33, "hello"},
{198,"world"},
{45, "c++"}
};
int main()
{
map<string, itemS> mymap;
string str[4] = {"1st","2nd","3rd","4th"};
for(int i=0; i<4; i++)
{
mymap.insert(make_pair(str[i], s[i])); // 新增元素
}
cout << "初始化元素:" << endl;
for(map<string, itemS>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
cout << it->first << " " << it->second.a << " " << it->second.b << endl;
}
for(map<string, itemS>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
if(it->second.a >100){
it = mymap.erase(it); //删除元素
}
}
cout << "删除 second.a > 100 的元素后:" << endl;
for(map<string, itemS>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
cout << it->first << " " << it->second.a << " " << it->second.b << endl;
}
cout << "查找键值为 2nd 的元素:" << endl;
auto it = mymap.find("2nd"); //查找元素
if (it != mymap.end())
cout << it->first << " " << it->second.a << " " << it->second.b << endl;
mymap.clear(); //清除所有元素
cout << "清除后再打印:" << endl;
for(map<string, itemS>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
cout << it->first << " " << it->second.a << " " << it->second.b << endl;
}
return 0;
}
set:
#include <iostream>
#include <set>
using namespace std;
typedef struct {
int a;
char s;
}newtype;
struct compare
{
bool operator()(const newtype &a, const newtype &b) const
{
return a.s<b.s;
}
};
set<newtype, compare>element;
int main()
{
newtype a,b,c,d;
a.a=1; a.s='f';
b.a=2; b.s='b';
c.a=4; c.s='d';
d.a=3; d.s='c';
//插入元素, 自动根据compare排序
element.insert(a);
element.insert(b);
element.insert(c);
element.insert(d);
cout<<"初始元素: "<<endl;
for(set<newtype, compare>::iterator it=element.begin(); it!=element.end();++it)
cout<<(*it).a<<" " << (*it).s << endl;
//插入元素
newtype e, f;
e.a = 10, e.s = 'a';
f.a = 20, f.s = 'f';
cout<<"插入 e : "<<endl;
if (element.insert(e).second)
cout<<"Insert e OK!"<<endl;
else
cout<<"Insert e Failed!"<<endl;
cout<<"插入 f: "<<endl;;
if (element.insert(f).second)
cout<<"Insert f OK!"<<endl;
else
cout<<"Insert f Failed!"<<endl;
cout<<"插入后元素:"<<endl;
for(set<newtype, compare>::iterator it=element.begin(); it!=element.end();++it)
cout<<(*it).a<<" " << (*it).s << endl;
cout<<"删除元素:"<<endl;
element.erase(e); //删除元素
cout<<"删除后元素:"<<endl;
for(set<newtype, compare>::iterator it=element.begin(); it!=element.end();++it)
cout<<(*it).a<<" " << (*it).s << endl;
cout<<"查找元素: "<<endl;
if (element.find(b) != element.end()) //查找元素
cout<<"find OK!"<<endl;
else
cout<<"not found!"<<endl;
element.clear(); //清除所有元素
cout<<"清除所有元素后:"<<endl;
for(set<newtype, compare>::iterator it=element.begin(); it!=element.end(); ++it)
cout<<(*it).a<<" " << (*it).s << endl;
return 0;
}
未完待续。。。