Search code examples
c++boost

function that covers boost::trim and accepts std::vector<std::string>


void trimMyStrings(std::vector<std::string> &dataVectors) {
    for(auto &dataVector : dataVectors) {
        boost::algorithm::trim(dataVector);
    }
}

int main() {
    std::string firstWord = "   blaasldlasasdasd   ";
    std::string secondWord = "   asdasdasd    ";
    std::vector<std::string> combineWord{firstWord, secondWord};
    trimMyStrings(combineWord);
    std::cout << firstWord << "&" << "secondWord";
}

the code above doesn't work

no error but the firstWord and secondWord don't get trimmed on the cout

How do I make it work?


Solution

  • you need a reference to the original strings that you place into your vector.

    std::vector<std::string*> combineWord{&firstWord, &secondWord};
    

    Then for trimMyStrings you need to update its parameter like:

    void trimMyStrings(std::vector<std::string*> &dataVectors)
    

    Then use trim with something like:

    boost::algorithm::trim(*dataVector);