What is the most efficient way to erase from a set, iterate over it? Here are two approaches that I thought was better between. Is there any better way?
void WaitForFiles(std::set<string> files) {
while (files.size() > 0) {
std::set<string> found_files;
for (const auto& file : files) {
if (Exists(file)) {
found_files.insert(file);
}
}
for (const auto& found_file : found_files) {
files.erase(file);
}
}
}
Using set_difference:
void WaitForFiles(std::set<string> files) {
while (files.size() > 0) {
std::set<string> found_files;
for (const auto& file : files) {
if (Exists(file)) {
found_files.insert(file);
}
}
std::set<string> difference;
std::set_difference(files.begin(), files.end(),
found_files.begin(), found_files.end(),
std::inserter(difference, difference.end()));
files = difference;
}
}
Please note that the following failures:
void WaitForFiles(std::set<string> files) {
while (files.size() > 0) {
for (const auto& file : files) {
if (Exists(file)) {
files.erase(file);
}
}
}
}
To determine the effectiveness, keep in mind that in my case the file may take 30 minutes, so the Exists function will be called many times, and the installed files will not change very often compared to the number of times the cycle repeats.
source
share