Public Group

# using unique() with vectors

This topic is 5054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok... basically i NEED to use vectors, can't use lists so please don't suggest that. i #include <algorithm> and have often used sort() and merge() (list only functions) with my vectors, so i thought that i could use unique the same way. unfortunately that doesn't happen. i'll create the vector, sort it (that part works fine), but when i use unique it essentially doesn't resize the vector to erase the leftover elements, it looks like it reassigns every element to a new list of unique elements and then just says, ok i'm done, and actually creates EXTRA COPIES of a few of my remaining elements... not quite what i would like it to do... i think i messed up what i was trying to say, it got kind of confusing. basically... how do i use unique() correctly with vectors? and if i can't use unique() then what can i use that would do the same thing? if you really need an example i'll give you one, but it's kind of complex b/c i read the elements from a file and they're all actually string elements of a vector that belongs to a class. i'm basically trying to take three other vectors that belong to that class, copy them all into this new vector, so i have four different vectors. vec1 = {1, 2, 3, 4} vec2 = {3, 4, 5, 6} vec3 = {4, 5, 6, 7} vec4 = {} vec4.insert(vec4.begin(), vec1.begin(), vec1.end()) vec4.insert(//same as above w/ vec2) vec4.insert(//same as above w/ vec3) vec4 = {1, 2, 3, 4, 3, 4, 5, 6, 4, 5, 6, 7} sort(vec4.begin(), vec4.end()) vec4 = 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7} unique(vec4.begin(), vec4.end()) //this is what i want vec4 = {1, 2, 3, 4, 5, 6, 7} //this is what i get vec4 = {1, 2, 3, 4, 5, 6, 7, 5, 5, 6, 6, 7} oh, and one more thing, i can't use resize because i never know how long the final vector is going to be

##### Share on other sites
Here would be what I would suggest. Since you need to only have one of each item, you will need to remove the replicas. Here is some pseudo-code of one way I would do it
vec4 = {1, 2, 3, 4, 5, 6, 7, 5, 5, 6, 6, 7}vector inlist = 0for vec4 itr = begin, itr != end; itr ++{if inlist.Find itr == -1  inlist.pusback itr}vec4 = inlist;

Now that is really horrible pseudo-code, but can you see what I suggest you try doing? You just start at the beginning and loop to the end of vec4. If the item you are at is not in the inlist, then you add it. If it is, you just continue on. I think that should work for you, you just reassign the new vector at the end so you have what you are looking for. Hope it helps.

- Drew

##### Share on other sites
thnx drew, i see what ur saying, and i think that WOULD work... it's just going to create a lot of overhead (when this thing is done, it's going to have to do that little bit of prgramming 386 times..., additionally there is a function in main() where the user can search through each object and if that object has ANY of the elements the objects name will be added to a list... so we'll be creating additional vectors a LOT) hopefully there is another way, but that's the best so far...

*** the following is unneccessary detail but if you REALLY want to know what I'm trying to do... **
i feel a little more comfortable going to detail of what i'm trying to do here than in other forums i've posted this (some may find it 'immature') i'm creating a library for pokemon (yes some of you may run and hide) similar to the one they had in stadium2, but with more functionality, so you can see the scope of this - 386 objects each have five string vectors: lvl up moves, tms & hms, egg moves, learned moves, all moves, it's the allMoves vector that i'm having probs creating. (for those of you that don't know the actual moves can overlap throughout each subsection) the in the program implementation i want to have a funtion that can take a user created vector of moves to search with and return all pokemon that learn both ALL of those moves and ANY of those moves... the logic has been a nightmare but i got it all figured out... then i relize unique() didn't work right. i could implement what drew said, but that would require me seriously retooling my logic... or so i surmise

##### Share on other sites
unique returns a iterator to the new end of the sequence.

vec.erase(unique(vec.begin(),vec.end()),vec.end()); // should do what you want

##### Share on other sites
whoo-hoo! thnx alot! that works perfectly!!! and it streamlines into my program very nicely

##### Share on other sites
Quote:
 Original post by zappernapperwhoo-hoo! thnx alot! that works perfectly!!! and it streamlines into my program very nicely

[lol] I'm sorry. I totally missed that part where you said
Quote:
 basically... how do i use unique() correctly with vectors?

Oh well, if anyone needs the unique function for the 'shareware' version of their game, just look at my post [smile].

- Drew

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013568
×