我正在嘗試從文本檔案中洗掉一個字串。為此,我想將檔案讀入一個向量,然后我想搜索該字串的位置,因此我可以使用 vector::erase 將其洗掉。從向量中洗掉字串后,我可以將向量寫入新檔案。
到目前為止,我已經完成了所有這些,但是找到了字串的位置。我使用 < algorithm> 的 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
uj5u.com熱心網友回復:
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)記憶體(就加載的行而言)解決方案:讀取行并立即只寫回那些與字串不匹配的行。
uj5u.com熱心網友回復:
下面是作業示例。有沒有必要將字串存盤到一個 vector或搜索矢量字串里面的位置,因為我們可以直接檢查讀取的行等于字串要搜索,如圖所示。
主程式
#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;
}
輸入檔案
file123.txt
Bob56.txt'
Foo8854.txt
file113.txt
Bob56.txt'
Foo8854.txt
file223.txt
Bob96.txt'
Foo8814.txt
輸出.txt
file123.txt
Bob56.txt'
file113.txt
Bob56.txt'
file223.txt
Bob96.txt'
Foo8814.txt
uj5u.com熱心網友回復:
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318445.html
下一篇:來自文本檔案的有效JSON
