不废话直接上代码,测试过,可运行!
/*****************************
**文件名称:Vector_Test.cpp
**功能简介:STL 模版知识汇总
**创建日期:2018-09-21
**修改日期:
**创建人员:viture_li 601233682@qq.com
**文件版本:
*****************************/
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <list>
#include <map>
using namespace std;
//vector模版
template <typename T>
void showVector(vector<T> v)
{
for(vector<T>::iterator it = v.begin();it != v.end();it++)
{
cout << *it;
cout << endl;
}
cout << endl;
}
//set模版
template <typename T>
void showSet(set<T> s)
{
for(set<T>::iterator it = s.begin();it != s.end();it++)
{
cout << *it;
cout << endl;
}
cout << endl;
}
//list模版
template <typename T>
void showList(list<T> s)
{
for(list<T>::iterator it = s.begin();it != s.end();it++)
{
cout << *it;
cout << endl;
}
cout << endl;
}
// map
void showmap(map<string,int> s)
{
for(map<string,int>::iterator it = s.begin();it != s.end();it++)
{
cout << it->first << " " << it->second << endl;
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
//模版之vector
cout << "初始化" << endl;
string str[] = {"hello","This","is","a","STL_Vector","demo"};
vector<string> v_str(str,str+6);
showVector(v_str);
cout << "复制元素" << endl;
vector<string> v_str1(v_str);
showVector(v_str1);
cout << "重新调整vector容量大小" << endl;
v_str.resize(3);
showVector(v_str);
cout << "访问第一个元素" << endl;
cout << v_str1.front()<<endl;
cout << "访问最后一个元素" << endl;
cout << v_str1.back()<<endl;
cout << "删除最后一个元素" << endl;
v_str1.pop_back();
showVector(v_str1);
cout << "新加元素" << endl;
v_str1.push_back("Add_string");
showVector(v_str1);
cout << "在第二个位置插入新元素" << endl;
v_str1.insert(v_str1.begin()+1,"2_add_string");
showVector(v_str1);
cout << "删除第四个位置的元素" << endl;
v_str1.erase(v_str1.begin()+3);
showVector(v_str1);
cout << "连续插入新元素" << endl;
v_str1.insert(v_str1.begin()+1,7,"mybe");
showVector(v_str1);
cout << "清除所有内容" << endl;
v_str1.clear();
showVector(v_str1);
//模版之set
cout << "初始化(自动排序) set<int> s" << endl;
int intset[] = {1,4,5,7,8,2,3,6,9,0};
set<int> s(intset,intset+10);
showSet(s);
cout << "新建一个set<string> s_str,自动字典排序" << endl;
string str_set[] = {"a","f","b","c","d","k"};
set<string> s_str(str_set,str_set+6);
showSet(s_str);
cout << "insert一个已经存在的数" << endl;
s.insert(9);
showSet(s);
cout << "insert一个不存在的string" << endl;
s_str.insert("e");
showSet(s_str);
//模版之list
cout << "list<int> l 初始化" << endl;
int a[] = {1,4,5,7,9,100,23,43,56,5};
list<int> l(a,a+10);
showList(l);
cout << "将元素都初始化为9" << endl;
list<int> nine(l.size(),9);
showList(nine);
cout << "自动排序" << endl;
int aa[] = { 8,5,7,6,1,2,3,4,5,5,6,7,7 };
list<int> so(aa,aa+10);
so.sort();
showList(so);
// 模版之map
cout << "模版之 map" << endl;
map<string,int> m;
m["medicine"] = 1;
m["faming"] = 2;
m["industry"] = 3;
m["technology"] = 4;
string fy("fy");
m[fy] = 90;
cout << "打印出map元素" << endl;
showmap(m);
cout << "medicine: " << m["medicine"] << endl;
cout << "error: " << m["error"] << endl;
cout << "通过关键字来删除" <<endl;
m.erase("fy");
showmap(m);
cout << "通过 insert 来增加" <<endl;
m.insert(pair<string,int>("fy",90));
showmap(m);
cout << "使用 count()来判断是否存在key" << endl;
if(m.count("fy"))
{
cout << "fy is in map !" << endl;
}
else
{
cout << "fy is't in map !" << endl;
}
cout << "清空" << endl;
m.clear();
showmap(m);
system("pause");
return 0;
}