Repeating letters in a string output after removing spaces [duplicate]

  c++

I’m trying to remove all spaces from a string. I wrote code, it removes spaces, but in the end it rewrites last couple letters depending on how many spaces it removed. For example in the code below, output is: randomteststringng.

Any help?

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {

    string txtname = "random test string";
    remove(txtname.begin(),txtname.end(),' ');
    
    cout<<txtname;
    
    return 0;
    
}

Source: Windows Questions C++

LEAVE A COMMENT