1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
void printArray(int arr[], int size) {
for ( int i = 0; i < size; i++ ) {
cout << arr[i] << ' ';
}
cout << endl;
}
int main() {
int myarray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << "myarray contains: ";
printArray(myarray, 10);
cout<<endl;
int myints[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> myvector (myints, myints + sizeof(myints) / sizeof(int));
cout<<"myvector contains: ";
for (std::vector<int>::iterator a = myvector.begin(); a!=myvector.end(); ++a)
cout<<' '<< *a;
cout<<"\n"<<endl;
int myints2 [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::list<int> mylist (myints2, myints2 + sizeof(myints2) / sizeof(int));
cout<<"mylist contains: ";
for (std::list<int>::iterator b = mylist.begin(); b!=mylist.end(); ++b)
cout<<' '<< *b;
cout<<"\n"<<endl;
int myarray2[10] = {};
std::copy(myarray, myarray+10, myarray2);
cout<<"myarray2 contains: ";
printArray(myarray2, 10);
cout<<endl;
std::vector<int> myvector2 (10);
std::copy(myvector.begin(), myvector.end(), myvector2.begin());
cout<<"myvector2 contains: ";
for (std::vector<int>::iterator c = myvector2.begin(); c!=myvector2.end(); ++c)
cout<<' '<<*c;
cout<<"\n"<<endl;
std::list<int> mylist2 (10);
std::copy(mylist.begin(), mylist.end(), mylist2.begin());
cout<<"mylist2 contains: ";
for (std::list<int>::iterator d = mylist2.begin(); d!=mylist2.end(); ++d)
cout<<' '<<*d;
cout<<"\n"<<endl;
int myarray3[10] = {};
for(int i = 0; i<10; i++)
{
myarray3[i] = myarray[i] +2;
}
cout<<"myarray3 contains: ";
printArray(myarray3, 10);
cout<<endl;
std::vector<int> myvector3(10);
for(int n = 0; n<10; n++)
{
myvector3[n] = myvector[n]+3;
}
cout<<"myvector3 contains: ";
for(std::vector<int>::iterator e = myvector3.begin(); e!=myvector3.end(); ++e)
cout<<' '<<*e;
cout<<"\n"<<endl;
std::list<int> mylist3 (mylist.size());
for (std::list<int>::iterator b = mylist.begin(); b!=mylist.end(); ++b)
{
mylist3.push_back( *b + 5 );
}
cout<<"mylist3 contains: ";
for (std::list<int>::iterator f = mylist3.begin(); f!=mylist3.end(); ++f)
cout<<' '<<*f;
cout<<endl;
return 0;
}
|