Jump to content

  • Log In with Google      Sign In   
  • Create Account

unique texfile strings


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 cozzie   Members   -  Reputation: 1778

Like
0Likes
Like

Posted 16 November 2012 - 03:04 PM

Hi,

I'm strugging/ staring blind at a fairly simple 'issue'.

I have an array of strings and want to find out the number of unique strings, the strings are stored in tempTexFiles[]:

[source lang="cpp"] int pNrUniqueMaterials = 1; bool NEW = false; for(int texcount=1;texcount<pNrMaterials;++texcount) { for(int tlist=0;tlist<pNrMaterials;++tlist) { if(tempTexFiles[texcount] == tempTexFiles[tlist]) break; else NEW = true; } if(NEW) { pNrUniqueMaterials++; NEW = false; } } mNrMaterials = pNrUniqueMaterials;[/source]

Probably something to simple for words, been staring at it for an hour.

The results now is 5 and should be 3....

Any help is really appriciated.

greets,
Cris

Edited by cozzie, 16 November 2012 - 03:09 PM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13935

Like
0Likes
Like

Posted 16 November 2012 - 03:31 PM

Can this be moved to General Programming or For Beginners? It has nothing to do with graphics...

Your algorithm is broken. As long as not all strings are identical, NEW will always be true and you will end up with pNrUniqueMaterials == pNrMaterials. A correct algorithm in the style of yours would go something like this:
int pNrUniqueMaterials = 0;
  for (int i = 0; i < pNrMaterials; ++i) {
    if (std::find(tempTexFiles, tempTexFiles+i, tempTexFiles[i]) == tempTexFiles+i)
	  ++pNrUniqueMaterials;
  }

If you have a large number of strings and this takes too long, you can do better, for instance by using an unordered_set.

Edited by Álvaro, 16 November 2012 - 03:31 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS