📜  <iterator>C++ STL中的库(1)

📅  最后修改于: 2023-12-03 15:13:11.250000             🧑  作者: Mango

C++ STL中的库

1. 什么是STL

STL即Standard Template Library,C++标准模板类库,是由一些常用的数据结构和算法组成的代码集合。它被设计成通用的,高效的,并与C++内置类型和用户自定义类型兼容,涉及容器、算法、迭代器和函数对象等方面的内容。

2. 容器

STL提供了许多容器,可用于存储不同类型的数据,例如数组、链表、向量、队列、栈、集合、映射等。

2.1 vector

vector是一种动态数组,它能够以O(1) 时间在数组尾部加入元素,但在数组中间插入元素时,可能需要复制数据,导致效率降低。

#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector<int> vec; //定义一个空的vector

    vec.push_back(1); //在尾部加入元素
    vec.push_back(2);
    vec.push_back(3);

    for(int i=0;i<vec.size();i++) {
        cout << vec[i] << " ";  //遍历vector
    }
    //output: 1 2 3
    return 0;
}
2.2 map

map是一种键值对应的容器,可以快速查找的结构,它的查找速度为O(log n)。

#include <map>
#include <iostream>

using namespace std;

int main() {
    map<int, string> mp;

    mp.insert(pair<int, string>(1, "hello"));
    mp.insert(pair<int, string>(2, "world"));
    mp.insert(pair<int, string>(3, "c++"));

    map<int, string>::iterator iter;
    for(iter=mp.begin();iter!=mp.end();iter++) {
        cout << iter->first << " -> " << iter->second << endl; //遍历map
    }

    //output:
    //1 -> hello
    //2 -> world
    //3 -> c++
    return 0;
}
3. 算法

STL还提供了许多常用的算法,例如排序、查找、遍历等。

3.1 sort

sort是一个排序算法,可用于对不同类型的数组进行排序,按升序排序。

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};

    sort(arr, arr+11);  //排序

    for(int i=0;i<11;i++) {
        cout << arr[i] << " ";  //遍历数组
    }
    //output: 1 1 2 3 3 4 5 5 5 6 9
    return 0;
}
3.2 find

find是一个查找算法,用于查找数组中是否存在某个值,如果存在则返回该值在数组中的位置,否则返回数组结束的位置。

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
    int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    int *pt;

    pt = find(arr, arr+11, 9);  //查找9的位置

    if(pt != arr+11) {
        cout << "The position of 9 in the array is: " << pt - arr << endl;  //找到了
    }
    else {
        cout << "The array does not contain 9." << endl;  //未找到
    }
    //output: The position of 9 in the array is: 5
    return 0;
}
4. 迭代器

迭代器可用于遍历容器内的元素,STL中提供了多种迭代器,包括输入迭代器、输出迭代器、前向迭代器、双向迭代器和随机访问迭代器等。

#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    vector<int>::const_iterator iter;  //定义一个常量迭代器
    for(iter=vec.begin();iter!=vec.end();iter++) {  //遍历vector
        cout << *iter << " ";
    }
    //output: 1 2 3
    return 0;
}
5. 函数对象

函数对象是可调用对象,STL中提供了一些函数对象,例如unary_function、binary_function、plus等。函数对象可用于算法中的元素操作。

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
    int a[5] = {1, 2, 3, 4, 5};
    int b[5] = {6, 7, 8, 9, 10};
    int c[5];

    transform(a, a+5, b, c, plus<int>());  //对应位置元素相加

    for(int i=0;i<5;i++) {
        cout << c[i] << " ";  //遍历数组
    }
    //output: 7 9 11 13 15
    return 0;
}
6. 总结

STL提供了许多方便易用的容器、算法、迭代器和函数对象,它们的简单易用让代码变得更加简洁优雅,同时也提高了程序的可读性和可维护性。因此,学好STL是每个C++程序员的必修课。