Simple template examples.
More...
#include <iostream>
#include <string.h>
Go to the source code of this file.
|
template<class T > |
int | Tmycpy (T a, T b, size_t size, int numElements) |
| Template function to copy a string literal. More...
|
|
int | main (int argc, char *argv[]) |
|
Simple template examples.
Definition in file simpleTemplate.cc.
int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Definition at line 77 of file simpleTemplate.cc.
81 char ca1[] = {
"123456789"};
82 char ca2[
sizeof(ca1)];
85 memset(ca2,
'x',
sizeof(ca2));
90 int i = Tmycpy<char *>(ca1, ca2,
sizeof(ca2),
sizeof(ca2)/
sizeof(char));
94 std::cout <<
"SUCCESS: " 99 <<
"elements copied=" << i
102 std::cout <<
"#############################" << std::endl;
105 int ia[] = {9,8,7,6,5,4,3,2,1,0};
106 int ib[] = {0,0,0,0,0,0,0,0,0,4};
112 i = Tmycpy<int *>(ia, ib,
sizeof(ib),
sizeof(ib)/
sizeof(*ib));
117 std::cout <<
"SUCCESS: int array works too" 119 <<
"elements copied=" << i
122 std::cout <<
"SECOND ARRAY CONTENTS: " <<
"ia=" ;
123 for(
int j=0; j < i; j++)
128 for(
int j=0; j < i; j++)
131 std::cout << std::endl;
133 std::cout <<
"#############################" << std::endl;
template<class T >
int Tmycpy |
( |
T |
a, |
|
|
T |
b, |
|
|
size_t |
size, |
|
|
int |
numElements |
|
) |
| |
Template function to copy a string literal.
- Author
- Karl N. Redman
- Parameters
-
a | array 1 |
b | array 2 |
size | total size of the array |
numElements | used for printing the array contents (cheezy) |
- Returns
- count of elements copied or -1 on error. Failure to pass an array to this function is undetermined.
- Purpose:
- Template that copies a string literal or any array upto the point where the contents of the array element is equal to 0 ('\0').
- Note
- The arrays are expected to be the same size.
- Warning
- This template has very little practical application. This is merely here to demonstrate a simple Template function
Definition at line 32 of file simpleTemplate.cc.
39 std::cout <<
"INITIAL ARRAY CONTENTS: " <<
"a=" ;
40 for(
int i=0; i < numElements; i++)
45 for(
int i=0; i < numElements; i++)
51 <<
"numElements=" << numElements
70 if(memcmp(static_cast<void *>(a), static_cast<void *>(b), size) != 0)