Home > Software engineering >  How to remove an item of Vectors c
How to remove an item of Vectors c

Time:12-21

i have this code and i want to find a word in my vector and delete the item that includes that word but, my code will delete from the first line until the item that i want, how can i fix that?

std::string s;
std::vector<std::string> lines;
while (std::getline(theFile, s))
{
    lines.push_back(s);
}
//finding item in vector and changing it
for (unsigned i = 0; i < lines.size(); i  )
{
    std::size_t found = lines[i].find(name);
    if (found != std::string::npos)
    {
        lines.erase(lines.begin()   i);
    }
}

Update 1:

this is my full Code: I'm opening a file that contains some elements in this format ( David, 2002 , 1041 , 1957 ) ( cleve, 2003 , 1071 , 1517 ) ( Ali, 2005 , 1021 , 1937 ) i'm getting a user input and finding the line that contains it. then i want to remove that line completely so i import it to a vector and then i can't modify it

#include <iostream>
#include <string> 
#include <vector>
#include <stream>
#include <algorithm>
using namespace std; 
using std::vector;

int main(){
string srch;
string line;
fstream Myfile;
string name;
int counter;
Myfile.open("Patientlist.txt", ios::in | ios::out);
cout <<"Deleting your Account";
cout << "\nEnter your ID: ";
cin.ignore();
getline(cin, srch);

if (Myfile.is_open())
{
    while (getline(Myfile, line))
    {
        if (line.find(srch) != string::npos)
        {
            cout << "\nYour details are: \n"
                 << line << endl;
        }
        break;
    }
}
else
{
    cout << "\nSearch Failed...  Patient not found!" << endl;
}
Myfile.close();
ifstream theFile("Patientlist.txt");
//using vectors to store value of file
std::string s;
std::vector<std::string> lines;
while (std::getline(theFile, s))
{
    lines.push_back(s);
}
//finding item in vector and changing it
for (unsigned i = 0; i < lines.size(); i  )
{
    std::size_t found = lines[i].find(name);
    if (found != std::string::npos)
    {
        lines.erase(lines.begin()   i);
    }
}
//writing new vector on file
ofstream file;
file.open("Patientlist.txt");
for (int i = 0; i < lines.size();   i)
{
    file << lines[i] << endl;
}
file.close();
cout << "Done!";

}

CodePudding user response:

The erasing loop is broken. The proper way is to use iterators and use the iterator returned by erase. Like so:

// finding item in vector and changing it
for (auto it = lines.begin(); it != lines.end();) {
    if (it->find(name) != std::string::npos) {
        it = lines.erase(it);
    } else {
          it;
    }
}

Or using the erase–remove idiom:

lines.erase(std::remove_if(lines.begin(), lines.end(),
            [&name](const std::string& line) {
                return line.find(name) != std::string::npos;
            }), lines.end());

Or since C 20 std::erase_if(std::vector):

std::erase_if(lines, [&name](const std::string& line) {
                         return line.find(name) != std::string::npos;
                     });

CodePudding user response:

You can use remove_if for this. The predicate argument should check if you can find a word in a line. Then remember to use erase to "shrink" the vector to its new size.

[Demo]

#include <algorithm>  // remove_if
#include <iostream>  // cout
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> lines{"one two three", "three four five", "five six one"};
    lines.erase(
        std::remove_if(std::begin(lines), std::end(lines), [](auto& line) {
            return line.find("one") != std::string::npos;}),
        std::end(lines)
    );
    for (auto& line : lines) { std::cout << line << "\n"; }
}
  • Related