#include #include #include using namespace std; class EmptyVectorEx : public exception { public: EmptyVectorEx() : m_message("the vector is empty") { /* no code */ }; const char* what() const throw () { return m_message; } private: const char *m_message; }; void vsort(vector &data) { if ( data.empty() ) throw EmptyVectorEx(); int tmpInt; for (int i = 0; i < data.size() - 1; i++) for (int j = data.size() - 2; j >= i; j--) if ( data[j] > data[j+1] ) { tmpInt = data[j]; data[j] = data[j+1]; data[j+1] = tmpInt; } } 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 (exception &e) { cout << "Error: " << e.what() << endl; } cout << "Sorted vector:" << endl; for (int i=0; i < v.size(); i++) cout << v[i] << " "; cout << endl << endl; vector u; try { vsort(u); } catch (exception &e) { cout << "Error: " << e.what() << endl; } return 0; }