F
F
Flysink2016-11-12 23:15:32
Qt
Flysink, 2016-11-12 23:15:32

C++ duplicate removal, why doesn't it work?

Help me understand why it works in visual studio, but not in qt
I'm learning don't throw poop

void Wid::test() {
vector<string> stp;
    stp.push_back(text->toPlainText().toStdString());
    sort(stp.begin(),stp.end());
    stp.erase(unique(stp.begin(),stp.end()),stp.end());
    ofstream filesave(pp);
    copy(stp.begin(),stp.end(), ostream_iterator<string>(filesave,"\n"));
}

The text is entered and after pressing the button, the test() function is actually called via connect(), which should remove duplicates in QPlainTextEdit and write to a file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-11-13
@Mercury13

void Wid::test() {
    vector<string> stp;
    stp.push_back(text->toPlainText().toStdString());
    // В stp одна штука
    sort(stp.begin(),stp.end());
    // Ну что ей будет, этой штуке?
    stp.erase(unique(stp.begin(),stp.end()),stp.end());
    // unique даст end; поведение vector при этом не определено.
    ofstream filesave(pp);
    copy(stp.begin(),stp.end(), ostream_iterator<string>(filesave,"\n"));
}

M
MiiNiPaa, 2016-11-13
@MiiNiPaa

As Mercury13 correctly noted, you have one element in your vector - the entire contents of the QPlainTextEdit, along with line breaks, etc.
To work the way you want, you need to take this content, split it into lines, stuff it into a vector, and then work with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question