replace(), replace_copy()

replace() replaces all instances of old_value with new_value within the sequence.

#include <algorithm> 
string oldval( "Mr. Winnie the Pooh" ); 
string newval( "Pooh" ); 
string sa[] = { "Christopher Robin", "Mr. Winnie the Pooh", 
                "Piglet", "Tigger", "Eeyore" }; 

vector<string> vec( sa, sa+5 ); 

// Christopher Robin Pooh Piglet Tigger Eeyore 
replace( vec.begin(), vec.end(), oldval, newval ); 

vector<string> vec2; 

// Christopher Robin Mr. Winnie the Pooh Piglet Tigger Eeyore 
replace_copy( vec.begin(), vec.end(), 
              inserter(vec2,vec2.begin()), newval, oldval ); 

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.