Sorting textures names

Hi,

I’ve a problem.

In a file, I’ve :

Brown
Ceilin
Floor
Wood
Ceilin
Ceilin
Floor

I would like to sort the textures names for
only conserve the names who appear one time.

So, in my outpu file, I would like to have :

Brown
Ceilin
Floor
Wood

Read a line of the file.

Test whether it is in the linked list.
if not (
Store it in the linked list.
write it to the new file.
)
until there is no line left.

Hi !

I suggest you use STL and keep your names in a list and before adding a new name test if it already exists in the list !
I do this with my TextureManager so it never created the same texture 2 times.
If you know know about STL just tell me !


Evil-Dog
Sleep is a waste of time

I mean…if you DONT know about STL tell me…hehe


Evil-Dog
Sleep is a waste of time

What is STL ?

God, is it so difficult?

You can do it like that. It’s one from tousand ways.

  1. Sort all names into array(hope you can do it - it’s damned easy).
  2. Sorry, samle in paskal
    for k:=0 to LastIndex-1 do
    if CompareStrings(TArray[k], TArray[k+1])=0 then TArray[k]:=’’;
  3. Write all array members that are not ‘’(empty), from 0 to LastIndex-1 into file.

Hop!

[This message has been edited by Zengar (edited 09-29-2001).]

Zengar, you don’t need to make Leyder Dylan
feel like he’s stupid with your “God is it so difficult ?”
Everyone here is here to learn and help others.

Leyder Dylan STL is Standard Template Library I think
You can use it to create list very easily
like
#include <vector> //A vector in STL is like an array
using namespace std;

vector< string* > vecTextureName;
vecTexture.push_back( your texturename here );
and to test if the texture you have to go throught your vector with an iterator
look for some doc about STL you’ll find it very useful ! I won’t give you all the code like that hehhehe I can only show you the path, you’re the one who must walk through it.
hehehee good luck man !


Evil-Dog
Sleep is a waste of time

#include <string.h>
Strcmp(…) function can be usefull.

Help me please

Sorry, I agree I was too rude
Have you tried my idea? It’s interesting if it works(he-he)

This is some code I put together in about 5 minutes. It uses STL to sort and remove uniqes entries.

#include
#include
#include
#include

using namespace std;

class outputString
{
public:
void operator() (const string &s) const {cout << s << endl;}
};

void main(void)
{
vector listOfNames;

listOfNames.push_back(“brown”);
listOfNames.push_back(“ceiling”);
listOfNames.push_back(“floor”);
listOfNames.push_back(“wood”);
listOfNames.push_back(“ceiling”);
listOfNames.push_back(“wood”);
listOfNames.push_back(“ceiling”);
listOfNames.push_back(“floor”);
listOfNames.push_back(“ceiling”);
listOfNames.push_back(“brown”);
listOfNames.push_back(“floor”);
listOfNames.push_back(“floor”);
listOfNames.push_back(“ceiling”);

sort(listOfNames.begin(), listOfNames.end());

vector::iterator listOfNamesEnd = unique(listOfNames.begin(), listOfNames.end());
for_each(listOfNames.begin(), listOfNamesEnd, outputString());
}

Hope you understand the code. I think it’s pretty string forward and easy to understand, but that might have to do with the fact that I know a little STL

By the way, you can insert this line,

for_each(listOfNames.begin(), listOfNames.end(), outputString());

before and after sort() and unique() to see what happend to the list of entries.

Originally posted by Leyder Dylan:
[b]Hi,

I’ve a problem.

In a file, I’ve :

Brown
Ceilin
Floor
Wood
Ceilin
Ceilin
Floor

I would like to sort the textures names for
only conserve the names who appear one time.

So, in my outpu file, I would like to have :

Brown
Ceilin
Floor
Wood

[/b]

//using standard template library, STL for short.

#include <list>
#include <string>
using namespace std;

//declare linked list to hold strings
list<string> lst;

//then add everything to the list at the back
lst.push_back(texture_name)

//use this function
lst.unique();

//now there’s no duplicates inside the list.

Hi,

It’s work. Thanks a million

But, I’ve another problem. I want to write the string in a file but it’s doesn’t work.

No error at the compilation but error at the runtime.

I’ve only added a line in the class :

class outputString
{
public: void operator() (const string &s) const {cout << s << endl;
fprintf(Temp1,"%s
", s);
}
};

And it’s impossible to put the string in a array like this :

strcpy(Tab[0], s);

Convertion error.

Use s.c_str() instead of s.

Yepp, DFrey got it. s is a string object, the STL-component string. If you want to convert this string to a char *, you must use s.c_str().

Questions Burner, your code will not remove all duplicate entries. The definition of unique() is that it will remove any equal entries next to each other. unique only guaratees that (*I) != (*I+1) for any value of I, where I is an iterator of the proper type. Meaning, if two entries are separated by another entry, none of them will be removed. You must sort the list before calling unique() to remove ALL duplicate entries.

Thanks for reminding me, I forgot to sort them.