How to remove empty string values in a vector with C++

1 Answer

0 votes
#include <algorithm>
#include <iostream>
#include <vector>

// Function to remove null or empty string values from a vector
void removeEmptyStrings(std::vector<std::string>& vec) {
    vec.erase(std::remove_if(vec.begin(), vec.end(), [](const std::string& str) {
        return str.empty();
    }), vec.end());
}

int main() {
    std::vector<std::string> vec = {"C++", "", "programming", "", "language", ""};

    removeEmptyStrings(vec);

    for (const auto& str : vec) {
        std::cout << "\"" << str << "\" ";
    }
}


 
/*
run:
 
"C++" "programming" "language" 
 
*/

 



answered Jan 20, 2025 by avibootz

Related questions

1 answer 136 views
1 answer 95 views
1 answer 144 views
1 answer 174 views
1 answer 235 views
...