\\ Vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class point{
public:
int x;
int y;
void print(){ cout << x << " " << y << endl;}
};
int main()
{
vector<point> vp;
point pt;
for(int i = 1; i < 20; ++i){
pt.x = i;
pt.y = i*i;
vp.push_back(pt);
}
cout << "\nInitialized vector:\n";
for(unsigned int i = 0; i < vp.size(); ++i){
vp[i].print();
}
//std::vector<point>::const_iterator pos;
for(unsigned int pos = 0; pos < vp.size(); ){
if( vp[pos].x < 5){
vp.erase(vp.begin() + pos);
}else{
++pos;
}
}
cout << "\nChanged vector:\n";
for(unsigned int i = 0; i < vp.size(); ++i){
vp[i].print();
}
return 0;
}
\\ deque
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<float> cool_front, cool_back;
for(unsigned int i = 0; i < 10; ++i){
cool_front.push_front(i*1.2);
cool_back.push_back(i*1.2);
}
cout << "deque push front array:\n\t";
for(unsigned int i = 0; i < cool_front.size(); ++i){
cout << cool_front.at(i) << " ";
}
cout << endl;
cout << "deque push back array\n\t";
for(unsigned int i = 0; i < cool_back.size(); ++i){
cout << cool_back.at(i) << " ";
}
cout << endl;
return 0;
}
\\ List
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<char> cool;
for(char c = 'a'; c <= 'z'; ++c){
cool.push_back(c);
}
while(!cool.empty()){
cout << cool.front() << " ";
cool.pop_front();
}
cout << endl;
return 0;
}
\\ for_each
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class point{
public:
int x;
int y;
//void print(){ cout << x << " " << y << endl;}
};
void print(point& pt){
cout << pt.x << " " << pt.y << endl;
}
void square(point& pt){
pt.x = pt.x * pt.x;
pt.y = pt.y * pt.y - pt.x;
}
int main()
{
vector<point> vp;
point pt;
for(int i = 1; i < 20; ++i){
pt.x = i;
pt.y = i*i;
vp.push_back(pt);
}
cout << "\nInitialized vector:\n";
//for(unsigned int i = 0; i < vp.size(); ++i){
// vp[i].print();
//}
for_each(vp.begin(),vp.end(), print);
for_each(vp.begin(), vp.end(), square);
cout << "\nSquared Vector : \n";
for_each(vp.begin(), vp.end(), print);
//std::vector<point>::const_iterator pos;
for(unsigned int pos = 0; pos < vp.size(); ){
if( vp[pos].x < 5){
vp.erase(vp.begin() + pos);
}else{
++pos;
}
}
cout << "\nChanged vector:\n";
for_each(vp.begin(), vp.end(), print);
//for(unsigned int i = 0; i < vp.size(); ++i){
// vp[i].print();
//}
return 0;
}
\\ sort
#include <iostream>
using namespace std;
int partition(int a[], int from, int to)
{
if(from == to){return from;}
int med = from;
int start = from;
int end = to;
while(start < end)
{
while(start < end && a[start] <= a[med]){ ++start;}
while(start < end && a[end] > a[med]){--end;}
if(start < end){
swap(a[start], a[end]);
}
}
swap(a[med], a[start -1]);
return start;
}
void quicksort(int dat[], int from, int to)
{
for(int i = 0; i < 8; ++i)
{
cout << dat[i] << " ";
}
cout << endl;
if(from >= to)
return;
int med = partition(dat, from, to);
quicksort(dat, from, med -1);
quicksort(dat, med+1, to);
}
void mergesort(int dat[], int from, int to)
{
if(from >= to)
{
return;
}
if (to - from == 1)
{
if (dat[from] > dat[to])
{
swap(dat[from], dat[to]);
}
}
else
{
int med = (from + to) / 2;
mergesort(dat, from, med-1);
mergesort(dat, med, to);
int i = from, j = med;
int tdx = 0;
int *temp = new int[to - from + 1];
while (i <= med -1 && j <= to)
{
if (dat[i] < dat[j])
{
temp[tdx] = dat[i];
tdx++;
i++;
}
else
{
temp[tdx] = dat[j];
tdx++;
j++;
}
}
if (j > to)
{
while(i < med)
{
temp[tdx++] = dat[i++];
}
}
for (int i = from; i <= to; ++i)
{
dat[i] = temp[i - from];
}
delete[] temp;
}
}
int main()
{
int dat[] = {5,8,4,7,3,2,9,0};
//quicksort(dat, 0, 7);
mergesort(dat, 0, 7);
for (int i = 0; i <= 7; ++i)
{
cout << dat[i] << " ";
}
cout << endl;
return 0;
}