STL的使用

来源:互联网 发布:淘宝c店女装排行榜 编辑:程序博客网 时间:2024/06/05 07:25

一,STL理论基础

STL的从广义上讲分为三类
1,algorithm (算法)
2,container (容器)
3,iterator (迭代器)
在C++标准中,STL被组织为下面的13个头文件,,,,,,,,,,,和

下面是一个简单的使用

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <vector>#include <algorithm>using namespace std;void main11(){    vector<int> vl;    vl.push_back(1);    vl.push_back(2);    vl.push_back(3);    /*      */    for (vector<int>::iterator it =vl.begin(); it != vl.end(); it++)    {        cout << *it << " ";    }     //3,算法  算法和迭代器 进行无缝连接    int num1 = count(vl.begin(), vl.end(), 3);    cout << "num1:" << num1 << endl;}void main22(){    Teacher t1, t2, t3;    t1.age = 11;    t2.age = 22;    t3.age = 33;    Teacher *p1, *p2, *p3;    p1 = &t1;    p2 = &t2;    p3 = &t3;    /*vector<Teacher> v1;    v1.push_back(t1);    v1.push_back(t2);    v1.push_back(t3);    for (vector<Teacher>::iterator i = v1.begin(); i != v1.end(); i++)    {        cout << i->age << " ";    }*/    vector<Teacher *> v1;    v1.push_back(p1);    v1.push_back(p2);    v1.push_back(p2);    for (vector<Teacher *>::iterator it = v1.begin(); it != v1.end(); it++);    {        cout << (*it)->age << endl;    }}int main(void){    main11();    printf("\n");    system("pause");    return 0;}

#define D_SCL_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <iostream>#include <string>#include <algorithm>using namespace std;void stringmain(){    string s1 = "dfk";    string s2("cccc");    string s3 = s2;    string s4 = (10, "ddd");    cout << "s1" << s1 << endl;    cout << "s2" << s2 << endl;    cout << "s3" << s3 << endl;    cout << "s4" << s4 << endl;}void stringiterator(){    string s1 = "chensongli";    int i;    for (i = 0; i < s1.length(); i++)    {        cout << s1[i] << " ";   //异常用[]    }    for (string::iterator it = s1.begin(); it != s1.end(); it++)    {        cout << *it << " ";    }    cout << endl;    try    {        int j;        for (j = 0; j < s1.length() + 3; j++)            cout << s1.at(i) << "  ";    }    catch (const std::exception&)    {        cout << "dddd" << endl;    }}/*char*--->string*/void charstring(){    string s1 = "chenlieeee";    //1, sl ===> char *    printf("s1:%s\n", s1.c_str());    //2,    /*size_type copy(_Out_writes_(_Count) _Elem * const _Ptr,        size_type _Count,         const size_type _Off = 0) const    {   // copy [_Off, _Off + _Count) to [_Ptr, _Ptr + _Count)        _Check_offset(_Off);*/    //3,s1的内容 copy buf中    char buf[128] = { 0 };    //basic_string <char *>::pointer array1Ptr = buf;    //s1.copy(buf, 3, 0);    cout << "buf:" << buf << endl;}//////void chen()//{//  using namespace std;//  string str1("1234567890");//  basic_string <char>::iterator str_Iter;//  char array1[20] = { 0 };//  char array2[10] = { 0 };//  basic_string <char>::pointer array1Ptr = array1;//  basic_string <char>::value_type *array2Ptr = array2;//  cout << "The original string str1 is: ";//  for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)//      cout << *str_Iter;//  cout << endl;//  basic_string <char>::size_type nArray1;//  // Note: string::copy is potentially unsafe, consider//  // using string::_Copy_s instead.//  nArray1 = str1.copy(array1Ptr, 12);  // C4996//  cout << "The number of copied characters in array1 is: "//      << nArray1 << endl;//  cout << "The copied characters array1 is: " << array1Ptr << endl;//  basic_string <char>::size_type nArray2;//  // Note: string::copy is potentially unsafe, consider//  // using string::_Copy_s instead.//  nArray2 = str1.copy(array2Ptr, 5, 6);  // C4996//  cout << "The number of copied characters in array2 is: "//      << nArray2 << endl;//  cout << "The copied characters array2 is: " << array2Ptr << endl;////  ////注意一定要使array3有足够的空间//  //char array3[5]={0};//  //basic_string<char>::pointer array3Ptr=array3;//  //basic_string<char>::size_type nArray3;//  //nArray3 = str1.copy(array3,9); //错误!!!!//  //cout<<"The number of copied characters in array3 is: "//  //  <<nArray3<<endl;//  //cout<<"The copied characters array3 is: "<<array3Ptr<<endl;//}void string_string(){    string s1 = "dfd";    string s2 = "bbbb";    //字符串拼接    s1 = s1 + s2;    cout <<"s1:" <<  s1 << endl;    string s4 = "cccc";    string s3 = "bbbbxb";    s3.append(s4);    cout << "s3:" << s3 << endl;}//字符串查找void find_string(){    string s1 = "chenli  www chensong www222li wwwp";    //look for [_Ptr, <null>) beginning at or after _Off    int index = s1.find("www", 0);    cout << "index: " << index << endl;    string s5 = "chenli";    s5.replace(0, 2, "陈丽");    cout << s5 << endl;    //每一次数组的下标    int offindex = s1.find("www", 0);    while (offindex != string::npos)    {        cout << "offindex:" << offindex << endl;        offindex++;   //平移量        offindex = s1.find("www", offindex);    }    //替换   小写改成大写    offindex = s1.find("www", 0);    while (offindex != string::npos)    {        cout << "offindex:" << offindex << endl;        s1.replace(offindex, 3, "WWW");        offindex++;   //平移量        offindex = s1.find("www", offindex);    }    cout << "s1后:" << s1 << endl;}//删除void string_erase(){    string s1 = "chenli_song cenli chenli";    string::iterator it = find(s1.begin(), s1.end(), 'l');    if (it != s1.end())    {        s1.erase(it);    }    cout << "s1:erase后结果-->" << s1 << endl;    /*s1.erase(s1.begin(), s1.end());    cout << "s1 全部删除: " << s1 << endl;*/    s1.insert(0, "chensogn" );    cout << "s1 全部intsert: " << s1 << endl;}int main(void){    stringmain();    stringiterator();    charstring();    string_string();    find_string();    string_erase();    printf("\n");    system("pause");    return 0;}

运行效果图
这里写图片描述

二,STL的算法

//STL算法void stl_transform(){    string s1 = "AAAbb";    transform(s1.begin(), s1.end(),s1.begin(), toupper);    cout << s1 << endl;    string s2 = "CCCLLLLLLLlllll";    transform(s2.begin(), s2.end(), s1.begin(), tolower);    cout << s2 << endl;}
原创粉丝点击