jeudi 29 octobre 2015

Given a string, how can I add only unique characters from another string to it?(C++)

I've been trying many different things for hours now, I tried using std::unique but I was getting funky results, then I tried using string::find. I feel like it would be easy to accomplish this if I used std::vector I'm looking for an efficient way to do this using the standard library, I read though a lot of the questions on here and cpluplus.com on this subject but I couldn't use their answers to implement what I needed. I apologize if this is trivial but I'm tired of trying different things at this point.

For example:

int main(){

  std::string unique_chars;
  std::string new_string = "ABC"

  getUniqueChars(unique_chars, new_string);
  cout << unique_chars << endl;

  new_string = "ABDF"
  getUniqueChars(unique_chars, new_string);

  cout << unique_chars; 

  return 0;
}  

void getUniqueChars(string &unique_chars, string &new_string){

   //add only unique characters from new_string to unique_chars

   return;
}

Should output:

ABC
ABCDF

Aucun commentaire:

Enregistrer un commentaire