std::string reg = ("AA00 AAA");
// Open the file for reading
std::ifstream file_in("../data/history/"+reg+".his");
// populate vector with line string from file
std::vector<std::string> contents;
int i;
std::string line;
while(!file_in.eof( )) {
std::getline(file_in, line);
contents.emplace_back(line);
i++;
}
// specify phrases for replacement
std::string toReplace(" NA ");
std::string dateStamp = dateHelpers::getCurrentDate();
//initialize aux container
std::vector<std::string> newLineVec;
//perform replacment
std::for_each(contents.begin(), contents.end(),[dateStamp](auto line){ // << was capturing newLineVec Here.
int index;
std::cout << "Initial String :" << line << std::endl;
while((index = line.find("NA")) != std::string::npos) { //for each location where phrase is present
line.replace(index, dateStamp.length(), dateStamp); //remove & replace at that position
std::cout << "Final String :" << line << std::endl;
// newLineVec.emplace_back(line); // store results ???
}
}
);
我的目标是搜索和替换文本文件中的word示例。我似乎不能使用for each的结果填充容器,然后流回输入文件。我的方法在这里是明智的还是我偏离了轨道?
任何帮助都将不胜感激。我已经尝试使代码片段的可复制性最小化,所以任何关于如何改进我的提问的输入也会有很大帮助。
编辑 * 输出示例..
Initial String :james smith 88 broad lane 07474493221 2021/05/12 NA
Final String :james smith 88 broad lane 07474493221 2021/05/12 2022/12/14
Initial String :adam brown 42 church street 07392834769 2022/03/03 NA
Final String :adam brown 42 church street 07392834769 2022/03/03 2022/12/14
Initial String :James smith 88 broad lane 07474493221 2022/09/12 NA
Final String :James smith 88 broad lane 07474493221 2022/09/12 2022/12/14
**edit 2-〉我很抱歉,因为我昨晚一定很累,因为今天早上一切都很好。漫长的一天一定让我更好。这里清理了一下。现在工作的守卫,以避免潜在的分段故障。
void storeLineFiles(std::vector<std::string> &linesVec, const std::string path){
// Open the file for reading
std::ifstream file_in(path);
// populate container with lines from file (strings)
int i;
std::string line;
while(!file_in.eof( )) {
std::getline(file_in, line);
linesVec.emplace_back(line);
i++;
}
}
void replaceStrContainerWords(std::vector<std::string> &input, const std::string toReplace, const std::string &replacement){
//initialize aux container
std::vector<std::string> newLineVec;
//for each line in input vec + capture env vars
std::for_each(input.begin(), input.end(),[ &toReplace, &replacement, &newLineVec](auto line){
int index;
//replace all instances of toReplace with replacement
while((index = line.find(toReplace)) != std::string::npos) { //for each location where phrase is present
line.replace(index, replacement.length(), replacement); //remove & replace at that position
newLineVec.emplace_back(line); // store result temporarily in aux container
}
}
);
input = newLineVec; // copy result to input container for ergonomics.
}
int main(int argc, char *argv[]) {
std::string reg = ("AA00 AAA");
std::string path = ("../data/history/" + reg + ".his");
std::vector<std::string> toChange;
storeLineFiles(toChange, path);
for (auto each : toChange) { std::cout << each << "\n"; }
std::string toReplace("NA");
std::string dateStamp = dateHelpers::getCurrentDate();
replaceStrContainerWords(toChange, toReplace, dateStamp);
for (auto each : toChange) { std::cout << each << "\n"; }
}
2条答案
按热度按时间iqxoj9l91#
不需要将数据存储在容器中,您可以读取每一行,用替换字符串替换模式字符串,并在单个循环中输出它。
下面是伪代码
kr98yfug2#
我创造了一个最小的可复制的例子。
我纠正了你忘记将参数“line”作为引用传递给lambda的问题。因此,在上面的例子中:
所以,单词“line”前面的
&
更正后的MRE如下所示,并且可以正常工作: