1

Is there a standard algorithm in the library that does the job of the following for-loop?

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>


int main( )
{
    const char oldFillCharacter { '-' };
    std::vector<char> vec( 10, oldFillCharacter ); // construct with 10 chars
    // modify some of the elements
    vec[1] = 'e';
    vec[7] = 'x';
    vec[9] = '{';

    const char newFillCharacter { '#' };

    for ( auto& elem : vec ) // change the fill character of the container
    {
        if ( elem == oldFillCharacter )
        {
            elem = newFillCharacter;
        }
    }

    // output to stdout
    std::copy( std::begin( vec ), std::end( vec ),
               std::ostream_iterator<char>( std::cout, " " ) );
    std::cout << '\n';
    /* prints: # e # # # # # x # { */
}

I want to replace the above range-based for-loop with a one-liner if possible. Is there any function that does this? I looked at std::for_each but I guess it's not suitable for such a scenario.

6
  • 4
    How about plain std::replace? Commented Apr 8, 2022 at 7:35
  • Tell us what you are trying to do, on a high level. In that code, there are several things going on, and what are we supposed to be pinpointing? Commented Apr 8, 2022 at 7:35
  • So to boil this down, are you simply trying to replace one value with another in a sequence of values? If so, that's what you should state up front -- no need to show code or other things that may confuse what you are actually trying to accomplish. Commented Apr 8, 2022 at 7:40
  • @PaulMcKenzie Yes. Simple as that. I just wanted a one-liner instead of a dummy loop. Commented Apr 8, 2022 at 7:42
  • Well, googling "STL algorithm to replace" would have probably gotten you to std::replace or std::replace_if. Doing a web search for for loops wouldn't take you to what you are looking for. Commented Apr 8, 2022 at 7:44

3 Answers 3

7

This loop will replace every occurrence of oldFillCharacter with newFillCharacter. If you don't want to do something more fancy std::replace looks good:

std::replace(std::begin(vec), std::end(vec), oldFillCharacter, newFillCharacter);

Or a bit simpler with std::ranges::replace:

std::ranges::replace(vec, oldFillCharacter, newFillCharacter);
Sign up to request clarification or add additional context in comments.

1 Comment

I was totally unaware of this algorithm. Seems like the most suitable one. Also thanks for suggesting the ranges variation!
3

You can use std::for_each.

std::for_each(vec.begin(), vec.end(), [](char& elem) { 
    if ( elem == oldFillCharacter ) elem = newFillCharacter;
    });

Comments

2
std::replace(vec.begin(), vec.end(), '_', '#');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.