Possible to merge strings in an order of your chosing?

Hi!

Let's consider having two strings:


1
2
string example1 = "A3B3C3DEF3G"
string example2 = "HIJKLMN"


Is it possible to merge these but leave the numbers alone so the output would be:

 
string example3 = "H3I3J3KLM3N"


I tried a bit with stuff like
1
2
3
example1.replace(0, example2[0])
example1.replace(2, example2[1])
etc..


But that didn't work at all, I just get this error message:

1
2
error: no matching function for call to 'std::__cxx11::basic_string<char>::replace(int, std::__cxx11::basic_string<char>::size_type, __gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)'
             example1.replace(0, example2[1]);

loop over string calling a transform() that calls your lambda that may call replace (or not for numbers) on a per-letter basis would probably work if you wanted to go that route. Or maybe replace-if without the lambda?

I would just DIY this one.
Last edited on
Topic archived. No new replies allowed.