【问题标题】:Searching a vector for a string, and then find the position of the string in c++在向量中搜索一个字符串,然后在c++中找到该字符串的位置
【发布时间】:2021-12-01 04:21:44
【问题描述】:

我正在尝试从文本文件中删除一个字符串。为此,我想将文件读入一个向量,然后我想搜索这个字符串的位置,所以我可以使用 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

【问题讨论】:

标签: c++ file vector find erase


【解决方案1】:

Below 是工作示例。 不需要将字符串存入avector或者在向量内搜索字符串的位置,因为我们可以直接检查读取的行是否等于要搜索的字符串,如图所示。

ma​​in.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

【讨论】:

    【解决方案2】:

    std::find 将迭代器返回到找到的元素,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) 内存(就加载的行而言)解决方案:读取行并立即只写回那些与字符串不匹配的行。

    【讨论】:

    • std::distance(vec.begin(),it)it ?
    • @АлексейНеудачин 你是什么意思?
    • it 相同。 /
    • @АлексейНеудачин 抱歉,您能详细说明一下吗? it 与 `std::distance(vec.begin(),it)` 不同。后者返回一个数字,前者是一个迭代器,而不是一个索引。
    • 它是.find()
    【解决方案3】:
    #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;
    
    
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      相关资源
      最近更新 更多