std :: iterator,指针和VC ++警告C4996 [英] std::iterator, pointers and VC++ warning C4996

查看:260
本文介绍了std :: iterator,指针和VC ++警告C4996的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int *arr = (int*) malloc(100*sizeof(int));
int *arr_copy = (int*) malloc(100*sizeof(int));
srand(123456789L);
for( int i = 0; i < 100; i++) {
    arr[i] = rand();
    arr_copy[i] = arr[i];
}

// ------ do stuff with arr ------

// reset arr...
std::copy(arr_copy, arr_copy+100,  arr);

编译时我收到 std :: copy()<的警告/ code>:

while compiling this I get this warning for std::copy():

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2227):
warning C4996: 'std::_Copy_impl': Function call 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'

我知道如何禁用/忽略警告,但是有一个简单的单行解决方案来从未经检查的指针中制作检查迭代器吗?类似的东西(我知道cout不是像int *那样的未经检查的指针,只是例如。):

I know how to disable/ignore the warning, but is there is a simple one liner solution to make a "checked iterator" out of an unchecked pointer? Something like (I know cout is not an unchecked pointer like int*, but just e.g.):

ostream_iterator<int>  out(cout," ");

std::copy(arr_copy, arr_copy+numElements,  out);

我不想写一个全新的专用类my_int_arr_output_iterator:iterator ...... 。但是我可以使用现有的迭代器吗?

I don't want to write a whole new specialized class my_int_arr_output_iterator : iterator.... But can I use one of the existing iterators?

---编辑---

因为有关于我使用c-style-arrays和malloc而不是STL容器的许多问题,我只想说我正在编写一个小程序来测试不同的排序算法的性能和内存使用情况。您在上面看到的代码片段是专门的(原始代码是具有多种方法的模板类,针对不同类型的数组中的不同数量的元素测试一种算法)特定于该问题的版本。

As there are many many questions abt my usage of c-style-arrays and malloc instead of STL containers, let me just say that I'm writing a small program to test different sorting algorithms' performance and memory usage. The code snippet you see above is a specialized (original code is template class with multiple methods, testing one algorithm for different number of elements in arrays of different types) version specific to the problem.

换句话说,我知道如何使用STL容器(向量)及其迭代器(vector :: begin / end)来完成此操作。我不知道的是我问了什么

In other words, I do know how to do this using STL containers (vector) and their iterators (vector::begin/end). What I didn't know is what I asked.

非常感谢,如果不是我,希望其他人会从答案中受益。

Thanks though, hopefully someone else would benefit from the answers if not me.

推荐答案

您正在寻找的直接答案是 stdext :: checked_array_iterator 。这可用于包装一个指针,它的长度为MSVC checked_iterator

The direct answer you're looking for is the stdext::checked_array_iterator. This can be used to wrap a pointer and it's length into a MSVC checked_iterator.

的std ::拷贝(arr_copy,arr_copy + 100,stdext: :checked_array_iterator< int *>(arr,100));

他们还提供 stdext :: checked_iterator ,它可以包装未经检查的容器。​​

They also provide a stdext::checked_iterator which can wrap a non-checked container.

这篇关于std :: iterator,指针和VC ++警告C4996的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆