// reversing a vector recursively // Mikhail Nesterenko // 11/18/2014 #include #include using std::vector; using std::cout; using std::cin; using std::endl; void reverse(vector::iterator, vector::iterator); int main(){ vector v; cout << "Input characters (zero when done): "; char ch; do{ cin >> ch; v.push_back(ch); } while(ch != '0'); reverse(v.begin(),v.end()-1); // string should have at least one character cout << "Reversed: "; for(int i=1; i< v.size(); ++i) cout << v[i]; cout << endl; } void reverse(vector::iterator from, vector::iterator to){ if(from < to){ char tmp = *from; *from = *to; *to = tmp; reverse(from+1, to-1); } }