[英]How to remove an item of Vectors c++
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:更新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.这是我的完整代码:我正在打开一个包含这种格式的一些元素的文件(David, 2002, 1041, 1957)(cleve, 2003, 1071, 1517)(Ali, 2005, 1021, 1937)我得到用户输入并找到包含它的行。 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!";
} }
The erasing loop is broken.擦除循环坏了。 The proper way is to use iterators and use the iterator returned by
erase
.正确的方法是使用迭代器并使用
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:或者使用erase-remove 习惯用法:
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++20std::erase_if(std::vector)
:或者从 C++20 开始
std::erase_if(std::vector)
:
std::erase_if(lines, [&name](const std::string& line) {
return line.find(name) != std::string::npos;
});
You can use remove_if
for this.您可以为此使用
remove_if
。 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.然后记住使用
erase
将矢量“缩小”到新的大小。
#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"; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.