template <class T> void swap(T& a, T& b) { T tmp = a; a = b; b = tmp; } |
#include <stdlib.h> #include <iostream> inline int cmp(const void * a, const void * b) { int aa = *(int *)a; int bb = *(int *)b; return (aa < bb) ? -1 : (aa > bb) ? 1 : 0; } int main(int argc, char * argv[]) { const int size = 1000; int array[size]; int n = 0; while (std::cin >> array[n++]); n--; // it got incremented once too many times qsort(array, n, sizeof(int), cmp); for (int i = 0; i < n; ++i) std::cout << array[i] << std::endl; return 0; } |
#include <stdlib.h> #include <string> #include <algorithm> #include <vector> #include <iostream> int main(int argc, char * argv[]) { std::vector<int> v; // create an empty vector of integers} int input; while (std::cin >> input) // while not end of file} v.push_back(input); // append to vector} std::sort(v.begin(), v.end()); int n = v.size(); for (int i = 0; i < n; ++i) std::cout << v[i] << std::endl; return 0; } |
vector<int> v; vector::iterator i1 = v.begin(); vector::iterator i2 = v.end(); while (i1 != i2) { ... ++i1; } |
#include <algorithm> std::sort(first, beyond); std::sort(first, beyond, std::greater<int>()); |
#include <stdlib.h> #include <iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std; int main(int argc, char * argv[]) { vector<int> v; istream_iterator<int> start(cin), end; back_insert_iterator<vector<int> > dest(v); copy(start, end, dest); sort(v.begin(), v.end()); copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\n")); return 0; } |
back_insert_iterator<vector<int> > dest(v); |
typedef vector<int> int_vector; back_insert_iterator<int_vector> dest(v); |
typedef istream_iterator<int> int_istream_iterator; copy(int_istream_iterator(cin), int_istream_iterator(), v.begin()); |