I think your first problem is if the vector is empty to avoid a seg-fault:
1 2 3 4 5 6 7 8
void insertName(vector<string>& x, string n){
if( n > x[x.size()-1] ) x.push_back(n); // what if x is empty?
else {
int i =0;
while( n > x[i] )i++; // should this be while(n < x[i]) ??
x.insert(x.begin()+i,n);
}
}
Also instead of x[x.size()-1] you can use x.back().
no, i know that x isn't empty, i put values from file names.txt, witch is not empty and names in this file are ordered by alphabet. i want to put one another name from keyboard so the order not to abolish, and to do this with sort algorithm and not so, as it's shown above.
When writing functions you can't make those kind of assumptions. It may be true in your current example, but it won't necessarily be true when you come to use your function in the future, or if someone else uses your function.