C++学习笔记——STL

STL

容器

序列式

关联式

#include<iostream>

using namespace std;

#include"vector"

#include"algorithm"



void main1()

{

    //1.容器,把元素拷贝到容器中

    vector<int> v1;

    v1.push_back(-1);

    v1.push_back(1);

    v1.push_back(2);

    v1.push_back(1);

    v1.push_back(3);

    v1.push_back(4);

    v1.push_back(5);

    //访问容器



    //2.迭代器,相当于一个指针

    for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)

    {

        cout << *it << " ";

    }

    //3.算法

    int num = count(v1.begin(), v1.end(), 1);

    cout << "num:" << num << endl;





}



void main()

{

    main1();

    system("pause");

}

string

string 是一个类


```#include<iostream>

using namespace std;





<div class="se-preview-section-delimiter"></div>

#include"string"

//字符串的初始化

void main21()

{

    string s1 = "abc";

    string s2("qwe");

    string s3 = s2;

    string s4(11, 'a');//11个a



    cout << s1 << endl;

    cout << s2 << endl;

    cout << s3 << endl;

    cout << s4 << endl;

}

//字符串的遍历

void mian22()

{

    string s1 = "abcd";

    for (int i = 0; i < s1.length(); i++)

    {

        cout << s1[i] << " ";

    }

    cout << endl;

    for (string::iterator it = s1.begin(); it != s1.end(); it++)

    {

        cout << *it << " ";

    }

    cout << endl;

    for (int i = 0; i < s1.length(); i++)

    {

        cout << s1.at(i) << " ";// 抛出异常

    }

    cout << endl;



    try

    {

        for (int i = 0; i < s1.length()+3; i++)

        {

            cout << s1.at(i) << " ";// 抛出异常

        }

    }

    catch (...)

    {

        cout << "异常" << endl;

    }

    cout << endl;

    /*程序会异常结束

    try

    {

    for (int i = 0; i < s1.length()+3; i++)

        {

            cout << s1[i] << " ";

        }

        cout << endl;

    }

    catch (...)

    {

        cout << "异常" << endl;

    }*/



}

void main()

{

    // main21();

     mian22();

    system("pause");

}





<div class="se-preview-section-delimiter"></div>
//字符指针和string的转换
void main23()
{
    string s1 = "aaa";
    //s1=>char *
    printf("s1:%s\n", s1.c_str());
    //s1内容copy到buff
    char buff[128];
    //s1.copy(buff, 3, 0);
    //cout << "buff" << buff << endl;
}

void main24()
{
    string s1 = "abc";
    string s2 = "efg";
    s1 = s1 + s2;
    cout << "s1"<<s1 << endl;

    string s3 = "abc";
    string s4 = "efg";
    s3.append(s4);
    cout << "s4" << s3 << endl;
}

void main25()
{
    string s1 = "hello c++ hello world hello xiaoy";
    int index = s1.find("hello", 5);
    cout << "index"<<index << endl;
    int offindex = s1.find("hello", 0);
    while (offindex != -1)
    {
        cout << offindex << endl;
        offindex++;
        offindex = s1.find("hello", offindex );
    }

    //替换
    offindex = s1.find("hello", 0);
    while (offindex != -1)
    {
        cout << offindex << endl;
        s1.replace(offindex, 5, "Hello");
        offindex++;
        offindex = s1.find("hello", offindex);
    }
    cout << "替换后的" << s1 << endl;

}
//插入
void main26()
{
    string s1 = "bbb";
    s1.insert(1, "aaa");
    cout << s1 << endl;
}


void main27()
{

    string s1 = "AAAbbb";
    transform(s1.begin(), s1.end(), s1.begin(), toupper);
    cout <<"s1" <<s1 << endl;

    string s2 = "AAAbbb";
    transform(s2.begin(), s2.end(), s2.begin(), tolower);
    cout << "s2" << s2 << endl;
}

猜你喜欢

转载自blog.csdn.net/m0_37393277/article/details/65041927