STL 是“Standard Template Library”的缩写,中文译为“标准模板库”。STL 是 C++ 标准库的一部分,不用单独安装。
C++ 对模板(Template)支持得很好,STL 就是借助模板把常用的数据结构及其算法都实现了一遍,并且做到了数据结构和算法的分离。例如,vector 的底层为顺序表(数组),list 的底层为双向链表,deque 的底层为循环队列,set 的底层为红黑树,hash_set 的底层为哈希表。
下面是取反,平方,立方的简单实现
template <typename T> void transInvT(T a[],T b[],int nNum) { for(int i=0;i<nNum;i++) { b[i] = -a[i]; } } template <typename T> void transSqr(T a[],T b[],int nNum) { for(int i=0;i<nNum;i++) { b[i] = a[i]*a[i]; } } template <typename T> void transcube(T a[],T b[],int nNum){ for(int i=0;i<nNum;i++){ b[i]=a[i]*a[i]*a[i]; } }
输出方式
template <typename T> void outputCont(string strNme,ostream& os, T begin, T end) { os<<strNme<<":"; for(;begin!=end;begin++) { os<<*begin<<"\t"; } os<<endl; }
迭代器输出并使用向量容器
template<typename T> T InvT(T a) { return -a; } template<typename T> T sqr(T a){ return a*a; } template<typename T> T cube(T a){ return a*a*a; } template <typename inputIter, typename outputIter, typename MyOperator> void trans(inputIter begInput, inputIter endInput, outputIter begOutPut, MyOperator op) { for(;begInput!=endInput;begInput++,begOutPut++) { // *begOutPut = ‐ (*begInput); *begOutPut = op(*begInput); } }
测试函数
void Test() { const int N = 5; int a[N] = {1,2,4,3,5}; outputCont("a",cout,a,a+N); int b[N]; vector<double> vb(N); vector<double> vc(N); transInv(a,b,N); outputCont("Inv a",cout,b,b+N); transSqr(a,b,N); outputCont("Sqr a",cout,b,b+N); transcube(a,b,N); outputCont("cube a",cout,b,b+N); transInvT(a,b,N); outputCont("Inv a T",cout,b,b+N); trans(a,a+N,vb.begin(),InvT<int>);//相反数 outputCont("Inv a by iter",cout,vb.begin(),vb.end()); trans(a,a+N,vb.begin(),sqr<int>);//平方 outputCont("sqr a by iter",cout,vb.begin(),vb.end()); trans(a,a+N,vb.begin(),cube<int>);//立方 outputCont("cube a by iter",cout,vb.begin(),vb.end()); }
运行测试函数
a:1 2 4 3 5
Inv a:-1 -2 -4 -3 -5
Sqr a:1 4 16 9 25
cube a:1 8 64 27 125
Inv a T:-1 -2 -4 -3 -5
Inv a by iter:-1 -2 -4 -3 -5
sqr a by iter:1 4 16 9 25
cube a by iter:1 8 64 27 125
首先,我们定义一个存储学生信息的类
class studentInfo{ public: studentInfo(string strNo,string strName){ _strNo = strNo; _strName = strName; } string _strNo; string _strName; friend ostream& operator<<(ostream& os, const studentInfo& info) { os<<info._strNo<<" "<<info._strName; return os; } friend bool operator<(const studentInfo& info1, const studentInfo& info2){ return info1._strNo<info2._strNo; } };
insert():增加一个数据
erase(iterator) ,删除定位器iterator指向的值
erase(first,second),删除定位器first和second之间的值
erase(key_value),删除键值key_value的值
find():返回给定值值得定位器,如果没找到则返回end()
void TestSet() { vector<studentInfo> students; students.push_back(studentInfo("10021","Zhang san")); students.push_back(studentInfo("10002","Li si")); students.push_back(studentInfo("10003","Wang wu")); students.push_back(studentInfo("10011","Wang Liu")); students.push_back(studentInfo("10010","Wu Liu")); set<studentInfo> studentSet(students.begin(),students.end());//给集合初始化 studentSet.insert(studentInfo("10030","jmlogi"));//增 studentSet.erase(studentSet.begin());//删 auto found = studentSet.find(studentInfo("10003","Wang wu"));//查 cout<<"found:"<<*found; outputCont("student set",cout,studentSet.begin(),studentSet.end()); }
found:10003 Wang wustudent set:10003 Wang wu 10010 Wu Liu 10011 Wang Liu 10021 Zhang san 10030 jmlogi
void TestMap() { map<char,int> count; string a = "hello my favorite world"; for(int i=0;a[i]!='\0';i++){ auto iter = count.find(a[i]); if(iter==count.end()){ count[a[i]]=1; } else{ count[a[i]]=count[a[i]]+1; } } for(map<char,int>::iterator it=count.begin();it!=count.end();it++){ cout<<it->first<<" "<<it->second<<endl; } }
3
a 1
d 1
e 2
f 1
h 1
i 1
l 3
m 1
o 3
r 2
t 1
v 1
w 1
y 1
QT+opencv
#include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); Mat image=imread("C:/darkpic.jpg",1);//路径最好不要带中文 Mat result; threshold(image, result, 125, 255, 0); namedWindow( "Display window", WINDOW_AUTOSIZE ); imshow( "Display window", image ); namedWindow("二值化后的图像"); imshow("二值化后的图像",result); } MainWindow::~MainWindow() { delete ui; }
熟练使用STL能给编写代码带来极大的便利,使得某一些操作更容易实现,以及未来打算结合opencv做大作业。