在向量中搜索字符串,然后在 C++ 中找到字符串的位置

Searching a vector for a string, and then find the position of the string in c++

我正在尝试从文本文件中删除一个字符串。为此,我想将文件读入一个向量,然后我想搜索这个字符串的位置,所以我可以使用 vector::erase 来删除它。从矢量中删除字符串后,我可以将矢量写入新文件。

到目前为止,我已经完成了所有这些,但找到了字符串的位置。我已经找到了使用 的 std::find 的各种解决方案,但这些答案试图检查这个字符串是否存在,而不是它的位置。

这里是一个如何设置文本文件的例子。带有一个字符串,后跟一个整数,后跟不带空格的 .txt。每个字符串都换行。

file123.txt
Bob56.txt'
Foo8854.txt

在这种情况下,向量将是“file123.txt”、“bob56.txt”、“Foo8854.txt”。

这是我已经制作的代码:

std::vector<std::string> FileInVector;
std::string str;
int StringPosition;

std::fstream FileNames;
FileNames.open("FileName Permanent Storage.txt");
while (std::getline(FileNames, str)) {
  if(str.size() > 0) {
      FileInVector.push_back(str); // Reads file, and this puts values into the vector
  }
}

//This is where it would find the position of the string: "bob56.txt" as an example

FileInVector.erase(StringPosition); // Removes the string from the vector
remove("FileName Permanent Storage.txt"); // Deletes old file
std::ofstream outFile("FileName Permanent Storage.txt"); // Creates new file
for (const auto &e : FileInVector) outFile << e << "\n"; // Writes vector without string into the new file

Below 是工作示例。 无需将字符串存入a vector或搜索字符串在vector中的位置,因为我们可以直接检查读取的行是否等于字符串待搜索,如图。

main.cpp

#include <iostream>
#include <fstream>

int main()
{
    
    std::string line, stringtobeSearched = "Foo8854.txt";

    std::ifstream inFile("input.txt");
    
    std::ofstream outFile("output.txt");
    if(inFile)
    {
        while(getline(inFile, line, '\n'))        
        {
            std::cout<<line<<std::endl;
            //if the line read is not same as string searched for then write it into the output.txt file
            if(line != stringtobeSearched)
            {
                outFile << line << "\n";
            }
            //if the line read is same as string searched for then don't write it into the output.txt file
            else 
            {
                std::cout<<"string found "<<std::endl;//just printing it on screen
            }
            
            
        }
    }
    else 
    {
        std::cout<<"file could not be read"<<std::endl;
    }
    inFile.close();
    outFile.close();
    
   
    return 0;
}

input.txt

file123.txt
Bob56.txt'
Foo8854.txt
file113.txt
Bob56.txt'
Foo8854.txt
file223.txt
Bob96.txt'
Foo8814.txt

output.txt

file123.txt
Bob56.txt'
file113.txt
Bob56.txt'
file223.txt
Bob96.txt'
Foo8814.txt

std::find returns 找到元素的迭代器,std::vector::erase 也接受迭代器。如果需要,std::distance 可用于计算索引。

小例子:

#include <vector>
#include <string>
#include <algorithm>

#include <iostream>


void print(const auto& vec){
    for(const auto& e:vec){
        std::cout<<e<<' ';
    }
    std::cout<<'\n';
}

int main(){
    std::vector<std::string> vec{"a","b","c","d"};
    auto it = std::find(vec.begin(),vec.end(),"c");
    
    if(it!=vec.end())//If found
    {
        std::cout<<"Index "<<std::distance(vec.begin(),it)<<'\n';
        vec.erase(it,it+1);
        print(vec);
    }
}

输出:

Index 2
a b d 

也就是说,有一个简单的 O(1) 内存(就加载的行而言)解决方案:读取行并立即只写回那些与字符串不匹配的行。

#include <filesystem>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <chrono>
#include <algorithm>
#include <vector>
#include <execution>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <string>
#include <atomic>

int main(int argc, char *argv[])
{

    std::vector<std::string> b{"uyv","uky","u6t"};
    std::vector<std::string> cb{"uyv"};

    auto heil = std::search(b.begin(), b.end(), cb.begin(), cb.end());
    b.erase(heil);
    for (auto c : b)
        std::cout << c << std::endl;


}