C++ algorithm unchecked_copy() version program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the unchecked_copy() which is similar to copy(), but enforces the use of an unchecked iterator as output iterator when _SECURE_SCL=1 is defined

To show: How to use the C++ algorithm, unchecked_copy() version to enforce the use of an unchecked iterator as output iterator when _SECURE_SCL=1 is defined in C++ programming

 

// C++ STL algorithm unchecked_copy() version

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

using namespace stdext;

 

int main(void)

{

// vector containers

vector <int> vec1, vec2;

// vector iterators

vector <int>::iterator Iter1, Iter2;

// push data

int i, ii;

 

for (i = 0; i <= 5; i++)

vec1.push_back(10 * i);

// push more data

for (ii = 0; ii <= 10; ii++)

vec2.push_back(3 * ii);

 

cout<<"vec1 vector elements = ";

// print data

for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// print data

cout<<"\nvec2 vector elements = ";

for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// to copy the first 3 elements of vec1 into the middle of vec2

unchecked_copy(vec1.begin(), vec1.begin() + 3, vec2.begin() + 4);

cout<<"\nvec2 with the first 3 elements of vec1 into the middle of vec2 (vec1 insert) = ";

for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// to shift the elements inserted into vec2 two positions to the left

unchecked_copy(vec2.begin()+4, vec2.begin() + 7, vec2.begin() + 2);

cout<<"\nvec2 with shifted elements inserted into vec2 two positions to the left = ";

for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

vec1 vector elements = 0 10 20 30 40 50

vec2 vector elements = 0 3 6 9 12 15 18 21 24 27 30

vec2 with the first 3 elements of vec1 into the middle of vec2 (vec1 insert) = 0 3 6 9 0 10 20 21 24 27 30

vec2 with shifted elements inserted into vec2 two positions to the left = 0 3 0 10 20 10 20 21 24 27 30

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index