#include #include using namespace std; class EmptyVectorEx { public: EmptyVectorEx() : m_message("the vector is empty") { /* no code */ }; const char* what() const { return m_message; } private: const char *m_message; }; void vsort(vector &data); int main() { vector v; v.push_back(17); v.push_back(3); v.push_back(12); v.push_back(1); v.push_back(22); v.push_back(5); cout << "Unsorted vector:" << endl; for (int i=0; i < v.size(); i++) cout << v[i] << " "; cout << endl << endl; try { vsort(v); } catch (EmptyVectorEx &e) { cout << "Error: " << e.what() << endl; } cout << "Sorted vector:" << endl; for (int i=0; i < v.size(); i++) cout << v[i] << " "; cout << endl << endl; return 0; } void vsort(vector &data) { if ( data.empty() ) throw EmptyVectorEx(); int tmp; for (int i = 0; i < data.size() - 1; i++) for (int j = data.size() - 2; j >= i; j--) if ( data[j] > data[j+1] ) { tmp = data[j]; data[j] = data[j+1]; data[j+1] = tmp; } }