replaced loop with copy_if algorithm