Skip to content

Instantly share code, notes, and snippets.

@jitpaul
Created May 3, 2018 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitpaul/cf08234c1aac4c0c85ee84c09da389e5 to your computer and use it in GitHub Desktop.
Save jitpaul/cf08234c1aac4c0c85ee84c09da389e5 to your computer and use it in GitHub Desktop.
Templates
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <typename T>
void sort(vector<T>& array) {
int i = 0;
while (i < array.size()) {
for (int j = 0; j < array.size() - 1-i; j++) {
if (array[j] > array[j+1]) {
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
i++;
}
}
int main() {
// int array
vector<int> intArray = {4,3,6,1};
sort(intArray);
for (int each : intArray) cout << each << " ";
cout << endl;
// char array
vector<char> charArray = {'b','r','w','p'};
sort(charArray);
for (char each : charArray) cout << each << " ";
cout << endl;
//string array
vector<string> stringArray = { "hello","how","are","you" };
sort(stringArray);
for (string each : stringArray) cout << each << " ";
cout << endl;
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment