The most efficient way to list items in C / C ++

I have a list of 100 unsorted items. Each item belongs to a group. The group to which the item belongs is simply an element of the item class.

Using C / C ++ I'm looking for the most efficient way to scan through a list of items, check which group they are in, and print the item on the screen. Here's a catch though. After an element from a group has been printed on the screen, I do not want to print more elements belonging to this group.

I use the pre STL compiler and the size of the executable is critical, so I don't want to start defining my own Hash classes.

+3
source share
8 answers

( , , ). , .

n + n * log(n)

, .

+6

/- bool, , .

:

#include <unordered_map>
#include <string>
#include <iostream>

std::string getGroupForNumber( int num )
{
//
}

int main()
{
    typedef std::tr1::unordered_map< std::string, bool > hashmap;
    hashmap groupsPrinted;

    for( int i = 0 ; i < 100 ; ++i ) {
        if ( groupsPrinted[ getGroupForNumber( i ) ] == false ) {
            groupsPrinted[ getGroupForNumber( i ) ] = true;
            std::cout << i << std::endl;
        }
    }
    return 0;
}
+1

c/++ , c-. . ? ? , ?

( ):

.

  typedef struct node{
    void *item; /* this is your item */
    node *next; 
  } node_t;

  typedef struct {
    node_t *my_group;
    int used;
  } group_t;

  static group_t my_items[NUM_OF_GROUPS]; /* this is your ordered by groups list.*/

, . group_t :

typedef struct group{
  node_t *my_group;
  group *next_free;
} group_t;
+1

0..99, , . "false". arr [groupId] = 'true' . STL.

0

std:: , .

0

.

?

, .

, ?

, . , , , , .

0

? ? ?

0

The cost of printing on the screen is several orders of magnitude more than anything you can do with objects. If you had an array of 10 million objects in just a few groups, sorting is not a smart option. If groups can be identified by a static index (i.e., an Integer in a given range), simply use an array of masks to indicate whether this was visible. If groups are more complex, you can save groups that were noticed in any given data structure (hash, tree, etc.).

0
source

Source: https://habr.com/ru/post/1699330/


All Articles