zoukankan      html  css  js  c++  java
  • C++primer 5 中 uninitialized_copy函数报错的问题

    c++primer 中的一个函数报错的问题

    StrVec类的设计中定义这个类,定义了一个static变量alloc,用来分配内存和构造元素

    class StrVec
    {
    public:
     StrVec() :elements(nullptr), first_free(nullptr), cap(nullptr) {}
     StrVec(initializer_list<string> li);
     StrVec(const StrVec&);
     StrVec& operator= (const StrVec&);
     ~StrVec();
     void push_back(const string&);         // 拷贝元素
     size_t size() const { return first_free - elements; }
     size_t capacity() const { return cap - elements; }
     string *begin() const{ return elements; }
     string *end() const { return first_free; }
     void reserve(size_t n);
     void resize(size_t n);
     void resize(size_t n, string str);
    private:
      static allocator<string> alloc;      // 静态成员,分配

    pair<string*, string*> alloc_n_copy(const string*, const string*);             // 分配内存,拷贝元素

    在实现函数alloc_n_copy的时候

    pair<string *, string*> StrVec::alloc_n_copy(const string *b, const string *e)
    {
     auto data =alloc.allocate(e - b);
     return{ data,uninitialized_copy(b,e,data) };        
    }

    在调用uinitialized_copy函数时,会产生不安全的警告:

    错误 C4996 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' 

    对参数的调用可能不安全,需要调用这个函数的函数来保证迭代器的值是正确的。程序会报错。

    去掉static声明,加上宏命令#define  _SCL_SECURE_NO_WARNINGS(.cpp文件中)可以去掉这个警告

  • 相关阅读:
    poj 2406 Power Strings【最小循环节】
    hdoj 2087 剪花布条
    hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】
    hdoj 1151 Air Raid
    hdoj 2502 月之数
    hdoj 1862 EXCEL排序
    hdoj 1200 To and Fro
    hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】
    hdoj 1068 Girls and Boys【匈牙利算法+最大独立集】
    uva1563
  • 原文地址:https://www.cnblogs.com/sanerer/p/7834735.html
Copyright © 2011-2022 走看看