J
jacob navia
Here is a comparison of two source codes that use the C++ STL and the
C containers library (ccl).
First, the C++ example:
// Standard Template Library example
#include <iostream>
#include <list>
using namespace std;
// Simple example uses type int
int main(void)
{
list<int> L;
L.push_back(0); // Insert a new element at the end
L.push_front(0); // Insert a new element at the beginning
L.insert(++L.begin(),2);// Insert "2" before position of first argument
// (Place before second argument)
L.push_back(5);
L.push_back(6);
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
cout << endl;
return 0;
}
/* OUTPUT: 0 2 0 5 6 */
---------------------------------------------------------------------------
Here is the C example
#include "containers.h"
static int PrintInt(void *pdata,void *extraArgs)
{
int i = *(int *)pdata;
fprintf(extraArgs,"%d ",i);
}
int main(void)
{
List *L;
int data;
L = iList.Create(sizeof(int));
data = 0;
iList.Add(L,&data);
iList.PushFront(L,&data);
data = 2;
iList.InsertAt(L,1,&data);
data = 5;
iList.Add(L,&data);
data = 6;
iList.Add(L,&data);
iList.Apply(L,PrintInt,stdout);
iList.Finalize(L);
}
----------------------------------------------------------------------------
I could have used iterators to print the list but I think using "Apply"
looks better ...
What do you think?
C containers library (ccl).
First, the C++ example:
// Standard Template Library example
#include <iostream>
#include <list>
using namespace std;
// Simple example uses type int
int main(void)
{
list<int> L;
L.push_back(0); // Insert a new element at the end
L.push_front(0); // Insert a new element at the beginning
L.insert(++L.begin(),2);// Insert "2" before position of first argument
// (Place before second argument)
L.push_back(5);
L.push_back(6);
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
cout << endl;
return 0;
}
/* OUTPUT: 0 2 0 5 6 */
---------------------------------------------------------------------------
Here is the C example
#include "containers.h"
static int PrintInt(void *pdata,void *extraArgs)
{
int i = *(int *)pdata;
fprintf(extraArgs,"%d ",i);
}
int main(void)
{
List *L;
int data;
L = iList.Create(sizeof(int));
data = 0;
iList.Add(L,&data);
iList.PushFront(L,&data);
data = 2;
iList.InsertAt(L,1,&data);
data = 5;
iList.Add(L,&data);
data = 6;
iList.Add(L,&data);
iList.Apply(L,PrintInt,stdout);
iList.Finalize(L);
}
----------------------------------------------------------------------------
I could have used iterators to print the list but I think using "Apply"
looks better ...
What do you think?