473,804 Members | 3,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates...???

Dear,

I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates where
mystery for me I didn't undersrand nothing.
So I will be thankfull if somebody solve with templates
these two programs in end of this post...

1)A program which creates a linked list of prim numbers..

/*--------------------------------*/
#include<stdio. h>
#include<malloc .h>
struct lst{
long int value;
struct lst* next;
};

int main(int argc, char* argv[]){
long int i,j,k;
struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
prvi=(struct lst*)malloc(siz eof(struct lst));
prvi->next=(struct lst*)malloc(siz eof(struct lst));
prvi->value=1;
prvi->next->value=2;
tekuci_prvi=prv i->next;
zadnji=prvi->next;
zadnji->next=(struct lst*)NULL;
k=atoi(argv[1]);
tekuci_drugi=pr vi;
for(i=1;i<k;i++ ){
exit:
tekuci_drugi=pr vi;
while(tekuci_dr ugi->next){
if((i%(tekuci_d rugi->value)==0) && tekuci_drugi->value!=1){
if(i==1){
goto exit;
}
i++;
goto exit;
}
tekuci_drugi=te kuci_drugi->next;
}
tekuci_prvi->next=(struct lst*)malloc(siz eof(struct lst));
tekuci_prvi=tek uci_prvi->next;
tekuci_prvi->value=i;
tekuci_prvi->next=(struct lst*)NULL;
/*printf("%ld\n" ,i);*/
}
tekuci_prvi=prv i;
while(tekuci_pr vi->next){
printf("%d\n", tekuci_prvi->value);
tekuci_prvi=tek uci_prvi->next;

}
exit(0);
}

2)A program which creates a file(if you redirect output with ">")
of random numbers use:"rand > file.txt", and "file.txt" is input for third
program...

#include<stdio. h>
#include<stdlib .h>

int main(int argc, char *argv[])
{ int i, j, k;
k=atoi(argv[1]);
srand(k);
for(i=0;i<k;i++ ){
printf("%d\n",r and());
}
exit(0);
}

3)A program which reads a list of random numbers generated with second
program a "file.txt" and make a sorted list of them...

#include<stdio. h>
#define BUFFER 128

struct lst{
int value;
struct lst *next;
};

int main(int argc , char *argv[]){
int i, j ,k;
FILE *fp;
char buffer[BUFFER];
struct lst *prvi, *tekuci_jedan, *tekuci_dva, *zadnji;
fp=(fopen(argv[1],"r"));
if(fp==(FILE*)N ULL){
printf("\nError can't open:%s",argv[1]);
}
prvi=(struct lst*)malloc(siz eof(struct lst));
tekuci_jedan=pr vi;
while(fgets(buf fer, BUFFER+1, fp)){
tekuci_jedan->next=(struct lst*)malloc(siz eof(struct lst));
tekuci_jedan->value=atoi(buf fer);
tekuci_jedan=te kuci_jedan->next;
}
tekuci_jedan->next=(struct lst*)NULL;
tekuci_jedan=pr vi;
while(tekuci_je dan->next){
tekuci_dva=prvi ;
while(tekuci_dv a->next){
if(tekuci_jedan->value < tekuci_dva->value){
/* i=tekuci_jedan->value; */
tekuci_jedan->value=tekuci_j edan->value+tekuci_d va->value;
tekuci_dva->value=tekuci_j edan->value-tekuci_dva->value;
tekuci_jedan->value=tekuci_j edan->value-tekuci_dva->value;
}
tekuci_dva=teku ci_dva->next;
}
tekuci_jedan=te kuci_jedan->next;
}
tekuci_jedan=pr vi;
while(tekuci_je dan->next){
printf("\n%d",t ekuci_jedan->value);
tekuci_jedan=te kuci_jedan->next;
}
exit(0);
}
All programs are commpilled with gcc without any waring...

Thank in advance, Robert...!!!

P.S. I don't know does a gcc support templates if not you can
send mee a version for VC++ 6.0




Sep 10 '05 #1
2 1592
On Sat, 10 Sep 2005 01:47:26 +0200, "Bore Biko" <bo*******@yaho o.co.uk> wrote:
Dear,

I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates where
mystery for me I didn't undersrand nothing.
So I will be thankfull if somebody solve with templates
these two programs in end of this post...


Templates in C++ is just a tool that you can use to solve certain kinds of
problems. The programs that you have included do not belong to that class of
problems and are therefore not likely to benefit from templates.

Templates allow you to program in a style called "generic programming", a
style that lets you use types as a compile-time parameter in your program.

For example, here's a function that adds two numbers:

template <class T>
T add(T a, T b)
{
return a + b;
}

And here's a program that calls it:

int main()
{
double a = add(0.0, 1.0);
int b = add(1, 2);
return 0;
}

The compiler will automatically generate two instances of add(), one that
deals with doubles, and another that deals with ints. In fact, you can pass as
parameters any class that has the operator + defined (and allow access to the
appropriate assignment and copy constructors).

-dr
Sep 10 '05 #2
Bore Biko wrote:
Dear,

I am an ordinary C programmer and I am most interesed
about dynamical data structuring and programming,
I don't like to use matricess and rows, I like to program
with practical programs that doesent use much memory.
I know a lot of C++ programmers ,and they tolded me,
that C++ templates are real solution for dynamical memory
use programming.I readed 3 books about C++ , but I don't
have a practice and a mass things about templates where
mystery for me I didn't undersrand nothing.
So I will be thankfull if somebody solve with templates
these two programs in end of this post...

I tried to keep the spirit of the algorithm and a little bit of the style.
1)A program which creates a linked list of prim numbers..

/*--------------------------------*/
#include<stdio. h>
#include<malloc .h>
struct lst{
long int value;
struct lst* next;
};

int main(int argc, char* argv[]){
long int i,j,k;
struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
prvi=(struct lst*)malloc(siz eof(struct lst));
prvi->next=(struct lst*)malloc(siz eof(struct lst));
prvi->value=1;
prvi->next->value=2;
tekuci_prvi=prv i->next;
zadnji=prvi->next;
zadnji->next=(struct lst*)NULL;
k=atoi(argv[1]);
tekuci_drugi=pr vi;
for(i=1;i<k;i++ ){
exit:
tekuci_drugi=pr vi;
while(tekuci_dr ugi->next){
if((i%(tekuci_d rugi->value)==0) && tekuci_drugi->value!=1){
if(i==1){
goto exit;
}
i++;
goto exit;
}
tekuci_drugi=te kuci_drugi->next;
}
tekuci_prvi->next=(struct lst*)malloc(siz eof(struct lst));
tekuci_prvi=tek uci_prvi->next;
tekuci_prvi->value=i;
tekuci_prvi->next=(struct lst*)NULL;
/*printf("%ld\n" ,i);*/
}
tekuci_prvi=prv i;
while(tekuci_pr vi->next){
printf("%d\n", tekuci_prvi->value);
tekuci_prvi=tek uci_prvi->next;

}
exit(0);
}

#include <iostream>
#include <list>
#include <cstdlib>

typedef std::list< unsigned long > UlongList;

int main ( unsigned int argn, char* args[] ) {
unsigned long upper_bound = std::atoi( args[1] );
UlongList primes;
for ( unsigned long candidate = 2; candidate < upper_bound; ++candidate )
{
for ( UlongList::cons t_iterator iter = primes.begin();
iter != primes.end(); ++ iter ) {
if ( 0 == candidate % *iter ) {
goto failed;
}
}
std::cout << candidate <<'\n';
primes.push_bac k( candidate );
failed : ;
}
}
This produces slightly different output: just the prime numbers strictly
less than the command line argument are printed in ascending order.

Note: 1 is not a prime number.


2)A program which creates a file(if you redirect output with ">")
of random numbers use:"rand > file.txt", and "file.txt" is input for third
program...

#include<stdio. h>
#include<stdlib .h>

int main(int argc, char *argv[])
{ int i, j, k;
k=atoi(argv[1]);
srand(k);
for(i=0;i<k;i++ ){
printf("%d\n",r and());
}
exit(0);
}
#include <iostream>
#include <cstdlib>

int main ( unsigned int argn, char* args[] ) {
unsigned long count = std::atoi( args[1] );
std::srand( count );
for ( unsigned long i = 0; i < count; ++i ) {
std::cout << std::rand() << '\n';
}
}
This version does not look that much different at all.

3)A program which reads a list of random numbers generated with second
program a "file.txt" and make a sorted list of them...

#include<stdio. h>
#define BUFFER 128

struct lst{
int value;
struct lst *next;
};

int main(int argc , char *argv[]){
int i, j ,k;
FILE *fp;
char buffer[BUFFER];
struct lst *prvi, *tekuci_jedan, *tekuci_dva, *zadnji;
fp=(fopen(argv[1],"r"));
if(fp==(FILE*)N ULL){
printf("\nError can't open:%s",argv[1]);
}
prvi=(struct lst*)malloc(siz eof(struct lst));
tekuci_jedan=pr vi;
while(fgets(buf fer, BUFFER+1, fp)){
tekuci_jedan->next=(struct lst*)malloc(siz eof(struct lst));
tekuci_jedan->value=atoi(buf fer);
tekuci_jedan=te kuci_jedan->next;
}
tekuci_jedan->next=(struct lst*)NULL;
tekuci_jedan=pr vi;
while(tekuci_je dan->next){
tekuci_dva=prvi ;
while(tekuci_dv a->next){
if(tekuci_jedan->value < tekuci_dva->value){
/* i=tekuci_jedan->value; */
tekuci_jedan->value=tekuci_j edan->value+tekuci_d va->value;
tekuci_dva->value=tekuci_j edan->value-tekuci_dva->value;
tekuci_jedan->value=tekuci_j edan->value-tekuci_dva->value;
}
tekuci_dva=teku ci_dva->next;
}
tekuci_jedan=te kuci_jedan->next;
}
tekuci_jedan=pr vi;
while(tekuci_je dan->next){
printf("\n%d",t ekuci_jedan->value);
tekuci_jedan=te kuci_jedan->next;
}
exit(0);
}

#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

typedef std::vector< int > IntVector;

int main ( unsigned int argn, char* args[] ) {
std::fstream in_file ( args[1] );
if ( ! in_file ) {
std::cerr << "File " << args[1] << " could not be opened.\n";
} else {
IntVector i_vect;
int number;
while ( in_file >> number ) {
i_vect.push_bac k( number );
}
std::sort( i_vect.begin(), i_vect.end() );
std::copy( i_vect.begin(), i_vect.end(),
std::ostream_it erator< int >( std::cout, "\n" ) );
}
}
Here, I changed the data structure to be a vector instead of a list. Using a
list the program would read:

#include <fstream>
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

typedef std::list< int > IntList;

int main ( unsigned int argn, char* args[] ) {
std::fstream in_file ( args[1] );
if ( ! in_file ) {
std::cerr << "File " << args[1] << " could not be opened.\n";
} else {
IntList i_list;
int number;
while ( in_file >> number ) {
i_list.push_bac k( number );
}
i_list.sort();
std::copy( i_list.begin(), i_list.end(),
std::ostream_it erator< int >( std::cout, "\n" ) );
}
}
Notice the subtle change in calling the sort algorithm.

P.S. I don't know does a gcc support templates if not you can
send mee a version for VC++ 6.0


g++ supports templates in all recent versions.
Best

Kai-Uwe Bux
Sep 10 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2574
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The header is the same in each page and is described in an XML document, "Head.xml". The document part, which is variable in content, is described in other XML files (e.g. "Document.xml", "Product.xml", "Register.xml").
5
2537
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
22
2200
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding solutions which are overly specific),
12
1970
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and *.cpp). What amazes me is that I have already some code like this in another project where I don't get errors, so I am pretty sure I am missing some stupid thing.
16
16295
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
1659
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont get! In the following i've copy pasted the example from <http://www.w3schools.com/xsl/xsl_apply_templates.asp> into this post. I divide into sections and my real question will come after the code snippet:
25
3339
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the same subject in clc++m to get the more of the context. Ted
28
2643
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
104
4624
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
7
1713
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must remove all defined templates. I am not really a c++ guy so I have not worked with templates all that much (not really at all) and as such I don't have a good idea where to start.
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.