Algorithm
Move
#include <algorithm>
#include <iostream>
#include <vector>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" "<<&*it<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::vector<int> a = {1, 2, 3, 4};
std::vector<int> b;
std::cout<<"Before move ..."<<std::endl;
display(a.begin(), a.end());//1 2 3 4
display(b.begin(), b.end());//empty
b = std::move(a);
std::cout<<"After move ..."<<std::endl;
display(a.begin(), a.end());//empty
display(b.begin(), b.end());//1 2 3 4
return 0;
}
- Before move, a contains four elements, b is empty, after move, b take over the memory, a is empty
Move a Range
#include <algorithm>
#include <iostream>
#include <vector>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" "<<&*it<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::vector<int> a = {1, 2, 3, 4};
std::vector<int> b(4);
std::cout<<"Before move ..."<<std::endl;
display(a.begin(), a.end());
display(b.begin(), b.end());
std::move(a.begin(), a.end(), b.begin());
std::cout<<"After move ..."<<std::endl;
a.clear();
a[0] = 10;
display(a.begin(), a.end());//10 2 3 4
display(b.begin(), b.end());//1 2 3 4
return 0;
}
Move a range from a container, the container still holds the place that were once occupied by these values. Use vector::erase to remove the range out of the container
Sort
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
template <class T>
void init(T it, T end)
{
srand(time(NULL));
for(; it != end; ++it)
*it = rand()%100;
}
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> array(5);
init(array.begin(), array.end());
display(array.begin(), array.end());
std::sort(array.begin(), array.end());
display(array.begin(), array.end());
return 0;
}
//sort vector by convert vector to pointer
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
template <class T>
void init(T array[], int size)
{
srand(time(NULL));
for(int i = 0; i < size; i++)
array[i] = T(rand()%100);
}
template <class T>
void display(T array[], int size)
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> array(5);
init(&array[0], array.size());
display(&array[0], array.size());
std::sort(array.begin(), array.end());
display(&array[0], array.size());
return 0;
}
//sort by convert iterator to pointer
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
template <class T>
void init(T *array, int size)
{
srand(time(NULL));
for(int i = 0; i < size; i++)
array[i] = T(rand()%100);
}
template <class T>
void display(T *array, int size)
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> array(5);
init(&*array.begin(), array.size());
display(&*array.begin(), array.size());
std::sort(array.begin(), array.end());
display(&*array.begin(), array.size());
return 0;
}
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//constructor
Rectangle():Rectangle(0, 0){}
Rectangle(double w):Rectangle(w, 2*w){}
Rectangle(double w, double l):width(w), length(l){}
//accessor
double getWidth() const {return width;}
double getLength() const {return length;}
double getArea() const {return width*length;}
//mutator
void setWidth(double w) {width = w;}
void setLength(double l) {length = l;}
};
#endif
//main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include "Rectangle.h"
struct comp
{
bool operator()(const Rectangle &a, const Rectangle &b) const
{
return (a.getArea() < b.getArea());
}
};
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->getArea()<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<Rectangle> v;
v.push_back(Rectangle(3, 4));
v.push_back(Rectangle(1, 2));
v.push_back(Rectangle(2, 3));
std::sort(v.begin(), v.end(), comp());
display(v.begin(), v.end());
return 0;
}
- Sorting with Comparison struct
#include <iostream>
#include <vector>
#include <algorithm>
#include "Rectangle.h"
bool comp(const Rectangle &a, const Rectangle &b)
{
return (a.getArea() < b.getArea());
}
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->getArea()<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<Rectangle> v;
v.push_back(Rectangle(3, 4));
v.push_back(Rectangle(1, 2));
v.push_back(Rectangle(2, 3));
std::sort(v.begin(), v.end(), comp);
display(v.begin(), v.end());
return 0;
}
- Sorting with Comparison function
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//constructor
Rectangle():Rectangle(0, 0){}
Rectangle(double w):Rectangle(w, 2*w){}
Rectangle(double w, double l):width(w), length(l){}
//accessor
double getWidth() const {return width;}
double getLength() const {return length;}
double getArea() const {return width*length;}
//mutator
void setWidth(double w) {width = w;}
void setLength(double l) {length = l;}
};
#endif
//main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include "Rectangle.h"
bool comp(const Rectangle &a, const Rectangle &b)
{
return (a.getArea() < b.getArea());
}
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->getArea()<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<Rectangle> v;
v.push_back(Rectangle(3, 4));
v.push_back(Rectangle(1, 2));
v.push_back(Rectangle(2, 3));
std::sort(v.begin(), v.end(), comp);
display(v.begin(), v.end());
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include "Rectangle.h"
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->getArea()<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<Rectangle> v;
v.push_back(Rectangle(3, 4));
v.push_back(Rectangle(1, 2));
v.push_back(Rectangle(2, 3));
std::sort(v.begin(), v.end(), [](const Rectangle &a, const Rectangle &b){return a.getArea() < b.getArea();});
display(v.begin(), v.end());
return 0;
}
Partial_sort_copy
#include <iostream>
#include <algorithm>
#include <vector>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};
std::vector<int> c(5);
std::partial_sort_copy(v.begin(), v.end(), c.begin(), c.end());
display(c.begin(), c.end());//1 2 3 4 5
return 0;
}
- Copies the smallest elements in the range [first,last) to [result_first,result_last), sorting the elements copied
- Overload operator <
Is_sorted
#include <iostream>
#include <vector>
#include <algorithm>
int main(int argc, char *argv[])
{
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> v2 = {5, 4, 3, 2, 1};
if(std::is_sorted(v.begin(), v.end()))
{
std::cout<<"v is sorted ..."<<std::endl;
}
if(std::is_sorted(v2.begin(), v2.end()))
{
std::cout<<"v2 is sorted ..."<<std::endl;
}
return 0;
}
Random_shuffle
#include <iostream>
#include <vector>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> v = {1, 2, 3, 4, 5};
std::random_shuffle(v.begin(), v.end());
display(v.begin(), v.end());
return 0;
}
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//constructor
Rectangle():Rectangle(0, 0){}
Rectangle(double w):Rectangle(w, 2*w){}
Rectangle(double w, double l):width(w), length(l){}
//accessor
double getWidth() const {return width;}
double getLength() const {return length;}
double getArea() const {return width*length;}
//mutator
void setWidth(double w) {width = w;}
void setLength(double l) {length = l;}
};
#endif
//main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include "Rectangle.h"
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->getArea()<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<Rectangle> v;
v.push_back(Rectangle(1, 2));
v.push_back(Rectangle(2, 3));
v.push_back(Rectangle(3, 4));
v.push_back(Rectangle(4, 5));
std::random_shuffle(v.begin(), v.end(), [](int i){return std::rand()%i;});
display(v.begin(), v.end());
return 0;
}