ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c++ STL基本使用

2021-12-03 21:01:47  阅读:196  来源: 互联网

标签:基本 begin cout STL vb c++ outputCont int studentInfo


文章目录

c++STL基本使用

STL简要介绍

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

用set存储学生信息,并进行增删改查操作;

首先,我们定义一个存储学生信息的类

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

输入一个字符串,用map统计每个字符出现的次数并输出字符及对应的次数

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;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3LofEjwR-1638535875539)(c++ STL基本使用/image-20211203204824191.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ewZWxSpK-1638535875542)(c++ STL基本使用/image-20211203204833518.png)]

结语

熟练使用STL能给编写代码带来极大的便利,使得某一些操作更容易实现,以及未来打算结合opencv做大作业。

标签:基本,begin,cout,STL,vb,c++,outputCont,int,studentInfo
来源: https://blog.csdn.net/qq_46618854/article/details/121706864

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有