vector_map_set 常用操作小结
来源:互联网 发布:c语言入门教程txt 编辑:程序博客网 时间:2024/05/30 04:06
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;
}
未完待续。。。
- vector_map_set 常用操作小结
- git 常用操作小结
- java 字符串常用操作小结
- vim 常用操作自我小结
- jQuery操作表单常用控件方法小结
- js,jQuery数组常用操作小结
- MongoDB使用小结:一些常用操作分享
- MongoDB使用小结:一些常用操作分享
- MongoDB使用小结:一些常用操作分享
- MongoDB使用小结:一些常用操作分享
- MongoDB使用小结:一些常用操作分享
- MongoDB使用小结:一些常用操作分享
- splay学习小结&常用基本操作模板
- MongoDB使用小结:一些常用操作分享
- shell变量字符串常用操作小结
- MongoDB使用小结:一些常用操作分享
- MongoDB使用小结:一些常用操作分享
- MyEclipse常用的的操作小结
- 四件在我步入职业软件开发生涯那天起就该知道的事情
- Spring MVC 教程,快速入门,深入分析
- HashMap与Hashtable的区别
- GridControl 列自动匹配宽度
- Linux下各种软件的安装
- vector_map_set 常用操作小结
- c#中 get {return 字段;} set{字段=value;} 有什么用啊?
- jquery mobile
- java 面向对象编程(模拟老师用电脑上课)
- Spring初始化集合
- 第十一周-调用函数输出星号符
- 调用函数输出星号图(1)
- [week9]每周总结与工作计划
- c# 加密