其他分享
首页 > 其他分享> > vector详讲(一)

vector详讲(一)

作者:互联网

<vector>头文件里带有两个类型参数的类模板,第一个参数是需要指定的数据类型,第二个是分配器(allocator)类型

template<class T, class Allocator = allocator<T>>   class vector;

用分配器来为元素分配内存和释放内存。需要注意的是vector的运算符operator[] 和方法 at()的区别就是。前者不进行边界检查,而后者进行边界检查,超出边界会抛出out_of_range()的异常;

#include <iostream>
#include <vector>
#include <limits>
int main()
{
    std::vector<double> vectorDouble;
    //int max = -std::numeric_limits<double>::infinity();
    //std::cout << "max : " << max << std::endl;      //2^31 = 2147483648;max = -2147483648;
    for(int i = 0;true;++i)
    {
        double temp = 0.0;
        std::cout << "enter scord(-1 is stop )" << i << ": ";
        std::cin >> temp;
        if(temp == -1)
        {
            break;
        }
        vectorDouble.push_back(temp);
//        if(temp > max)
//        {
//            max = temp;
//        }

    }

    //max /= 100.0;
    for(auto &element : vectorDouble)
    {
        //element = element / max;
        std::cout << element << " ";
    }
    return 0;
}

这里的numeric_limits<>模板详见

标签:std,详讲,temp,max,vectorDouble,vector,include
来源: https://www.cnblogs.com/boost/p/10369360.html