Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Antonio Villanueva
- C++ class uses the bubble method
- to sort a vector without using sort
- sorts numbers or letters*/
- #include <iostream>
- #include <vector>
- using namespace std;
- template <typename T>
- class Ordena {
- public:
- Ordena (vector <T> &v) : v(v){};
- void ordenaElementos(){//sort vector public
- ordenaE();
- }
- void printVector (){//print vector
- for (const T &elem:v){
- cout <<elem<<" ";
- }
- cout <<endl;
- }
- private:
- vector<T>& v;
- void swapElems (int x,int y){//swap elems
- T tmp;
- tmp=v[x];
- v[x]=v[y];
- v[y]=tmp;
- }
- void ordenaE(){ //sort a vector
- for (size_t x=0 ;x < v.size();x++){
- for (size_t y=0 ;y < v.size();y++){
- if ( v[x] < v[y] ){
- swapElems(x,y);
- }
- }
- }
- }
- };
- int main(void)
- {
- //Test of a numeric vector
- //vector <int> v {3,5,1,8,7};
- //Ordena <int> o(v);
- //Test of a char vector
- vector <char> v {'x','a','y','b','w'};
- Ordena <char> o(v);
- o.ordenaElementos();
- o.printVector();
- //test the local version of the vector
- for (const auto &elem:v){
- cout<<elem<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement