make_checked_array_iterator
建立其他演算法可使用的 checked_array_iterator。
![]() |
---|
這個函式是 Standard C++ 程式庫的 Microsoft 擴充功能。透過使用這個函式實作的程式碼不可移植到不支援此 Microsoft 擴充功能的 C++ Standard 建置環境。 |
template <class Iter>
checked_array_iterator<Iter>
make_checked_array_iterator(
Iter Ptr,
size_t Size,
size_t Index = 0
);
參數
Ptr
目的陣列的指標。Size
目的陣列的大小。Index
陣列中選擇性的索引。
傳回值
checked_array_iterator 的執行個體。
備註
make_checked_array_iterator 函式是在 stdext 命名空間中定義。
這個函式接受原始指標 (通常會導致界限滿溢的顧慮),並將其包裝在執行檢查的 checked_array_iterator 類別中。 由於該類別標記為已檢查,因此 STL 不會對它發出警告。 如需詳細資訊與程式碼範例,請參閱已檢查的迭代器。
範例
在下列範例中,會建立向量並填入 10 個項目。 向量的內容是透過使用複製演算法複製到陣列,然後 make_checked_array_iterator 會用來指定目的。 接著界限故意違規檢查,以便觸發偵錯判斷提示失敗。
// make_checked_array_iterator.cpp
// compile with: /EHsc /W4 /MTd
#include <algorithm>
#include <iterator> // stdext::make_checked_array_iterator
#include <memory> // std::make_unique
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <typename C> void print(const string& s, const C& c) {
cout << s;
for (const auto& e : c) {
cout << e << " ";
}
cout << endl;
}
int main()
{
const size_t dest_size = 10;
// Old-school but not exception safe, favor make_unique<int[]>
// int* dest = new int[dest_size];
unique_ptr<int[]> updest = make_unique<int[]>(dest_size);
int* dest = updest.get(); // get a raw pointer for the demo
vector<int> v;
for (int i = 0; i < dest_size; ++i) {
v.push_back(i);
}
print("vector v: ", v);
copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));
cout << "int array dest: ";
for (int i = 0; i < dest_size; ++i) {
cout << dest[i] << " ";
}
cout << endl;
// Add another element to the vector to force an overrun.
v.push_back(10);
// The next line causes a debug assertion when it executes.
copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));
}
Output
vector v: 0 1 2 3 4 5 6 7 8 9
int array dest: 0 1 2 3 4 5 6 7 8 9
需求
標頭:<iterator>
**命名空間:**stdext