# [C++] Sorting a map



## [I.R.A]_FBi (Mar 27, 2009)

Is it possible?


----------



## Kreij (Mar 27, 2009)

Why not use a list?


----------



## [I.R.A]_FBi (Mar 27, 2009)

ok, im writing a program that should read into several files, check for hits then output a screenw ith the file name and the amount of hits for the word, was thinking of using a vector of structs, but the lecturer just told me to use map and left me alone, and i cant seem to implement it.


----------



## W1zzard (Mar 27, 2009)

why do you even need such a complex data structure? wouldnt it be easier to print the match count after each file together with the filename and then move on ?


----------



## [I.R.A]_FBi (Mar 27, 2009)

Shes asking for it sorted in relevance of hits (highest hits first) any advice on alternatives seeing the proposal is overly complex?


----------



## W1zzard (Mar 27, 2009)

being an old school c user i'd personally just use an array (dynamically allocated on the heap) and run qsort() on it.


----------



## [I.R.A]_FBi (Mar 27, 2009)

but then how would i know which array cell with hits belongs to which file?


----------



## Kreij (Mar 28, 2009)

Using a multi-dimensional array that holds the filename and the hits would be one way.

Using two arrays with the filenames and their associated hits at the same indexes would be another ... but would be more work to sort as you would have to sort them simultaneously to keep the indexes in sync.


----------



## [I.R.A]_FBi (Mar 28, 2009)

the thing is the amount of files entered is vairable, theya re manually entered and stored in a vector


----------



## W1zzard (Mar 28, 2009)

typedef struct mydatastruct
{
 char[255] filename; <-- yes thats a hard limit, but even windows has hard limits for filename / pathname length
 int hitcount;
}

mydatastruct* dataz=calloc(nunberoffiles, sizeof(mydatastruct));

access stuff with mydatastruct[itemtoget]

dont forget to free(), if you need to change the lenght of the array at runtime use realloc()


----------



## [I.R.A]_FBi (Mar 28, 2009)

thank you


----------



## Oliver_FF (Mar 28, 2009)

The nature of a hashmap means that the contents of a hashmap have no order. There are also no guarantees that the order of the items in a hashmap will remain constant over time. 

Java provides a class called a TreeMap whose iterator will iterate over the items in the map according to the natural ordering of its keys - i've no idea if there is something similar for C++ though...


----------



## Kreij (Mar 29, 2009)

In C#, I would probably use a List of KeyValuePairs (Dictionary).
Since it is a strongly-typed Generic, you can use any variable types as the Key and Value.
The only stipulation is that all of the Keys must be unique.

I know he's using C++, but you never know who may be reading this and find the info useful.


----------



## [I.R.A]_FBi (Mar 29, 2009)

i just found this ..

http://www.codeguru.com/forum/showthread.php?t=366064

implemented it (the vector of structs, but not the actual sort) but what is "MyDataSortPredicate"


```
std::sort(myvector.begin(), myvector.end(), MyDataSortPredicate);
```


----------



## [I.R.A]_FBi (Mar 29, 2009)

just found this


```
bool MyDataSortPredicate(const MyData& lhs, const MyData& rhs)
{
  return lhs.m_iData < rhs.m_iData;
}
```

what does it do?

i added it and started to get errors right left and center


----------



## Kreij (Mar 30, 2009)

It returns true if the data in the structure lhs is less than the data in the rhs structure, other wise it returns false.


----------



## [I.R.A]_FBi (Mar 30, 2009)

so how do i implement it (less the errors of course)


----------



## Kreij (Mar 30, 2009)

If you don't know what it does, why do you want to implement it?
All the method is doing is comparing two numbers to see if the one on the left is less than the one on the right. What you do with the return value (true or false) depends on what you are trying to accomplish.


----------



## [I.R.A]_FBi (Mar 30, 2009)

```
2472 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h   instantiated from `void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<filestore*, std::vector<filestore, std::allocator<filestore> > >, _Size = int]'
```

thats the error im getting


----------



## [I.R.A]_FBi (Mar 30, 2009)

ive tried to understand it kreji but to no avial .. right now i just want to get it working


----------



## Kreij (Mar 30, 2009)

Can you post the code block that calls the DataSortPredicate?


----------



## [I.R.A]_FBi (Mar 30, 2009)

```
std::sort(filesortlist.begin(), filesortlist.end(), MyDataSortPredicate);
    
    bool MyDataSortPredicate(const MyData& lhs, const MyData& rhs);
    {
         return lhs.m_iData > rhs.m_iData;
    }
```


----------



## [I.R.A]_FBi (Mar 30, 2009)

rereading .. think ive done something stupid


----------



## [I.R.A]_FBi (Mar 30, 2009)

```
struct filestore
{
    string filename;
    int wordcount;
};

vector <filestore> filesortlist;
```

and the part in my previous posts now turns to


```
bool MyDataSortPredicate(const filestore& lhs, const filestore& rhs);
    {
         return lhs.wordcount > rhs.wordcount;
    }
    
    std::sort(filesortlist.begin(), filesortlist.end(), MyDataSortPredicate);
```

thats what happens when you give a caveman a precision instrument

now im getting this error


```
131 C:\Documents and Settings\Administrator\My Documents\Assignment\Untitled1.cpp `lhs' undeclared (first use this function)
```

with the same for rhs


----------



## Oliver_FF (Mar 30, 2009)

My C++ is extremely rusty, but at a guess this:


```
bool MyDataSortPredicate(const filestore& lhs, const filestore& rhs);
    {
         return lhs.wordcount > rhs.wordcount;
    }
```

Looks like it should be more like 


```
bool MyDataSortPredicate(const filestore* lhs, const filestore* rhs);
    {
         return lhs->wordcount > rhs->wordcount;
    }
```


----------



## [I.R.A]_FBi (Mar 30, 2009)

trying 

edit: still getting 


```
131 C:\Documents and Settings\Administrator\My Documents\Assignment\Untitled1.cpp `lhs' undeclared (first use this function)
```


----------



## Oliver_FF (Mar 30, 2009)

I take back what i said just now, ignore it - This link explains the syntax of what you had earlier.

However, that ; is going to ruin everything for you 

Look close...


```
bool MyDataSortPredicate(const filestore& lhs, const filestore& rhs);
    {
```


----------



## [I.R.A]_FBi (Mar 30, 2009)

that link is confusing ... and the semicolon i guess is what ur drawing my attention to right?


----------



## Oliver_FF (Mar 30, 2009)

the semicolon tells the C compiler that it is just a declaration of the function for use at a later time. It then reads down and see's the {}'s, tries to parse the code inside of them and goes "Uhh... what's lhs? it's not in scope :s"

Just get rid of the ;


----------



## [I.R.A]_FBi (Mar 30, 2009)

```
bool MyDataSortPredicate(const filestore& , const filestore& );

bool MyDataSortPredicate(const filestore& lhs, const filestore& rhs)
    {
         return lhs->wordcount > rhs->wordcount;
    }
```

gives me


```
132 C:\Documents and Settings\Administrator\My Documents\Assignment\Untitled1.cpp a function-definition is not allowed here before '{' token
```

and


```
134 C:\Documents and Settings\Administrator\My Documents\Assignment\Untitled1.cpp expected `,' or `;' before '{' token
```


sorry to be bothering you guys so much


----------



## Oliver_FF (Mar 31, 2009)

```
bool MyDataSortPredicate(const filestore& , const filestore& );

bool MyDataSortPredicate(const filestore& lhs, const filestore& rhs)
    {
         return lhs->wordcount > rhs->wordcount;
    }
```
Ok, you don't need the top line of this block.

Make sure you aren't trying to define a new function inside another function, because that won't work. 

If you have that code block inside the main function or something you'll need to move it out (and above)


```
#include....

bool MyDataSortPredicate(const filestore& lhs, const filestore& rhs)
    {
         return lhs->wordcount > rhs->wordcount;
    }

main method declaration 
    {
        ...
    }
```


----------



## [I.R.A]_FBi (Mar 31, 2009)

ok i moved the declaration out, ill do the same for the defiition


----------



## [I.R.A]_FBi (Mar 31, 2009)

compiles  

thanks to all those who helped


----------



## [I.R.A]_FBi (Mar 31, 2009)

and it works too


----------

