01: #include <iostream> 02: #include <string> 03: #include <vector> 04: using namespace std; 05: /** 06: Inserts an element into a vector. 07: @param v a vector 08: @param pos the position before which to insert the element 09: @param s the element to insert 10: */ 11: void insert(vector<string>& v, int pos, string s) 12: { int last = v.size() - 1; 13: v.push_back(v[last]); 14: for (int i = last; i > pos; i--) 15: v[i] = v[i - 1]; 16: v[pos] = s; 17: } 18: /** 19: Prints all elements in a vector. 20: @param v the vector to print 21: */ 22: void print(vector<string> v) 23: { for (int i = 0; i < v.size(); i++) 24: cout << "[" << i << "] " << v[i] << "\n"; 25: } 26: 27: int main() 28: { vector<string> staff(5); 29: staff[0] = "Cracker, Carl"; 30: staff[1] = "Hacker, Harry"; 31: staff[2] = "Lam, Larry"; 32: staff[3] = "Reindeer, Rudolf"; 33: staff[4] = "Sandman, Susan"; 34: print(staff); 35: 36: int pos; 37: cout << "Insert before which element? "; 38: cin >> pos; 39: 40: insert(staff, pos, "New, Nina"); 41: print(staff); 42: return 0; 43: }