【问题标题】:How to remove an item of Vectors c++如何删除向量c ++的一项
【发布时间】:2022-01-22 06:15:48
【问题描述】:

我有这段代码,我想在我的向量中找到一个单词并删除包含该单词的项目,但是我的代码将从第一行删除直到我想要的项目,我该如何解决?

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);
    }
}

更新 1:

这是我的完整代码: 我正在打开一个包含这种格式的一些元素的文件 (大卫,2002 年,1041 年,1957 年) ( 克莱夫, 2003 , 1071 , 1517 ) (阿里,2005 年,1021 年,1937 年) 我正在获取用户输入并找到包含它的行。然后我想完全删除那条线,所以我将它导入一个向量,然后我不能修改它

#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!";

}

【问题讨论】:

  • @RetiredNinja 我更新了帖子,请再次查看
  • @TedLyngmo 是的,答案非常有帮助,非常感谢。
  • 太棒了!不客气!

标签: c++ file vector input


【解决方案1】:

擦除循环已损坏。正确的方法是使用迭代器,使用erase返回的迭代器。像这样:

// 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;
    }
}

或者使用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());

或者从 C++20 开始std::erase_if(std::vector):

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

【讨论】:

  • erase_if 有多酷?我不知道。漂亮的答案,每一个代码块!
  • @rturrado 谢谢 :) 是的,erase_if 真的很好。我必须说,这是标准库中最受欢迎的补充。
【解决方案2】:

您可以为此使用remove_if。谓词参数应该检查您是否可以在一行中找到一个单词。然后记得使用erase 将向量“缩小”到新的大小。

[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"; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 2019-10-16
    • 2016-01-09
    相关资源
    最近更新 更多