Utils - combine

- Implemented combine, which takes two integer vectors and combines
  them resulting in a single vector contianing the unique values of
  both.
This commit is contained in:
Paul Beckingham 2011-05-28 09:49:22 -04:00
parent 8fabffe18c
commit 8f85b0e194
3 changed files with 50 additions and 1 deletions

View file

@ -381,6 +381,27 @@ std::string compressIds (const std::vector <int>& ids)
return result.str ();
}
////////////////////////////////////////////////////////////////////////////////
void combine (std::vector <int>& dest, const std::vector <int>& source)
{
// Create a map using the sequence elements as keys. This will create a
// unique list, with no duplicates.
std::map <int, int> both;
std::vector <int>::iterator i1;
for (i1 = dest.begin (); i1 != dest.end (); ++i1) both[*i1] = 0;
std::vector <int>::const_iterator i2;
for (i2 = source.begin (); i2 != source.end (); ++i2) both[*i2] = 0;
// Now make a sequence out of the keys of the map.
dest.clear ();
std::map <int, int>::iterator i3;
for (i3 = both.begin (); i3 != both.end (); ++i3)
dest.push_back (i3->first);
std::sort (dest.begin (), dest.end ());
}
////////////////////////////////////////////////////////////////////////////////
// Run an external executable with execvp. This means stdio goes to
// the child process, so that it can receive user input (e.g. passwords).

View file

@ -72,6 +72,7 @@ int execute (const std::string&, std::vector<std::string>);
#endif
std::string compressIds (const std::vector <int>&);
void combine (std::vector <int>&, const std::vector <int>&);
#endif
////////////////////////////////////////////////////////////////////////////////