Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8443

Re: C Containers Library vs STL

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: C Containers Library vs STL
Date 2011-08-04 09:13 +1200
Message-ID <99trvaF9m3U1@mid.individual.net> (permalink)
References <j1cboc$qj2$1@speranza.aioe.org>

Show all headers | View raw


On 08/ 4/11 08:40 AM, jacob navia wrote:
> Hi
>
> I would like to compare the C containers library (written in C for C
> programmers) against the STL.
>
> Here is the code for the CCL. Maybe one C++ wizard would solve the
> same problem using the STL?
>
> I would be very ingterested in comparing code size, complexity, etc.
>
>
> Thanks in advance, and here is the C part:
> --------------------------------------------------------------
> Unique
> ------
>
> Given a text file, print in standard output the lines that are
> unique in it, i.e. filtering all duplicated lines.
>
> Algorithm:
> ---------
>
> Normally this involves keeping a sorted list/array of lines
> and testing if a line is in the set or  not.
>
> Solution using the CCL.
> ----------------------
>
>     1 #include<containers.h>
>     2 int main(int argc,char *argv[])
>     3 {
>     4         FILE *f;
>     5         int i=1,r;
>     6         Dictionary *dict;
>     7         char buf[8192];
>     8
>     9         if (argc<  2) {
>    10                 fprintf(stderr,"%s<file name>\n",argv[0]);
>    11                 return -1;
>    12         }
>    13         f = fopen(argv[1],"r");
>    14         if (f == NULL)
>    15                 return -1;
>    16         dict = iDictionary.Create(0,500);
>    17         if (dict == NULL)
>    18                 return -1;
>    19         while (fgets(buf,sizeof(buf),f)) {
>    20                 r= iDictionary.Add(dict,buf,NULL);
>    21                 if (r>  0)
>    22                         printf("[%3d] %s",i,buf);
>    23                 else if (r<  0) break;
>    24                 i++;
>    25         }
>    26         iDictionary.Finalize(dict);
>    27         fclose(f);
>    28 }
>
> Algorithm
> ---------
> A hash table will be used to determine if a line is a duplicate
> or not.

This is probably the closest equivalent (sticking to a similar layout 
style):

#include <set>
#include <string>
#include <iostream>
#include <fstream>

typedef std::set<std::string> Lines;

int main( int argc, char** argv )
{
   std::ifstream in( argv[1] );

   Lines unique;

   while( in ) {
     std::string line;

     std::getline( in, line );

     if( unique.insert(line).second ) {
       std::cout << line << std::endl;
     }
   }
}


> Commentary
> ----------
> We use the following local variables (lines 4-7):
> Name    Usage
> f       Input stream bound to the file to read
> i       Counter for lines read
> r       Result of adding a line
> dict    Dictionary (Hash table)
> buf     Line buffer limited to 8K per line

It would have been much easier to give the variables meaningful names!

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-03 22:40 +0200
  Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 09:13 +1200
    Re: C Containers Library vs STL Robert Wessel <robertwessel2@yahoo.com> - 2011-08-03 17:36 -0500
      Re: C Containers Library vs STL Robert Wessel <robertwessel2@yahoo.com> - 2011-08-03 17:38 -0500
        Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 01:30 +0200
          Re: C Containers Library vs STL Robert Wessel <robertwessel2@yahoo.com> - 2011-08-03 18:40 -0500
            Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 13:42 +0200
          Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 12:34 +1200
            Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 13:38 +1200
              Re: C Containers Library vs STL Juha Nieminen <nospam@thanks.invalid> - 2011-08-04 07:02 +0000
                Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 19:40 +1200
                Re: C Containers Library vs STL Juha Nieminen <nospam@thanks.invalid> - 2011-08-04 14:16 +0000
                Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 16:25 +0200
                Re: C Containers Library vs STL Nobody <nobody@nowhere.com> - 2011-08-04 21:40 +0100
            Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 09:42 +0200
              Re: C Containers Library vs STL Ian Collins <ian-news@hotmail.com> - 2011-08-04 19:59 +1200
              Re: C Containers Library vs STL Marc <marc.glisse@gmail.com> - 2011-08-04 12:47 +0000
                Re: C Containers Library vs STL jacob navia <jacob@spamsink.net> - 2011-08-04 15:17 +0200

csiph-web