how to find different elements in an array and put them into another array? c++ -
how to find different elements in an array and put them into another array? c++ -
i need find different elements in array , set them array. example:
a[0] = "group1" a[1] = "group2" a[2] = "group1" a[3] = "group2" a[4] = "group3"
and need array be:
b[0] = "group1" b[1] = "group2" b[2] = "group3"
order of groups doesn't matter. grateful help. thanks!
create new array re-create of first, , sort , utilize std::unique
eliminate duplicates:
std::vector<std::string> { ... }; std::vector<std::string> b( ); std::sort( b.begin(), b.end() ); b.erase( std::unique( b.begin(), b.end() ), b.end() );
another solution utilize std::unordered_set
sec container , re-create elements there:
std::vector<std::string> { ... }; std::unordered_set<std::string> b( a.begin(), a.end() );
you may want utilize std::unordered_set
temporary container , re-create array b afterwards:
std::vector<std::string> { ... }; std::unordered_set<std::string> tmp( a.begin(), a.end() ); std::vector<std::string> b( tmp.begin(), tmp.end() );
you can utilize std::set
instead if want unique elements sorted, less effective solution.
it hard more provided not plenty info - type of arrays utilize etc.
c++ arrays
Comments
Post a Comment