std::vector<int>& find1(std::string& search_word)
{
Here, you're returning a reference. A reference is an alias to an object. The object that is returned by this function will bind to the reference and be returned to the caller.
std::vector<int> final;
This is a local variable with automatic storage-duration. At the end of this function (meaning the closing brace) the vector will be deallocated and erased from the stack.
...
return final;
}
For this reason, returning a local variable from a function by reference is Undefined Behavior. Your program is now in an ill-formed state.
int main()
{
...
std::vector<int>& p = find1(search);
}
p
is also a reference, meaning it is an alias for the object that is returned. This is also wrong because the object that you think was returned was actually deallocated when the function returned.
To fix, return by value:
std::vector<int> find1(std::string& search_word)
Also, use an object, not a reference:
std::vector<int> p = find1(search);