Templates specialization

A

Aarti

I am trying to write a function to count the number of occurances of a
particular element in an array. I am also trying to specialize the
function for strings. I am passing the array as reference to the
function. Is there a way I can avoid hardcoding the size of the
array? I know we can do it if we pass the array as a pointer but can
we do it for pass by reference?

#include <iostream>
#include <string>
using std::cout;
using std::string;

template<typename T, int len>
int my_count(T (&arr)[len], T ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< len; ++i)
{
if(arr == ele)
++count;
}
return count;
}

//can we avoid hardcoding the size of the array here?
template<>
int my_count<string>(string (&arr)[3], string ele)
{
int count = 0;
//cout << "LEN = " << len << std::endl;
for(int i=0; i< 3; ++i)
{
if(arr == ele)
++count;
}
return count;
}

int main()
{
string arr_str[] = {"123", "123", "123"};
cout << my_count<string>(arr_str, "123");
}
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I am trying to write a function to count the number of occurances of a
particular element in an array. I am also trying to specialize the
function for strings. I am passing the array as reference to the
function. Is there a way I can avoid hardcoding the size of the
array? I know we can do it if we pass the array as a pointer but can
we do it for pass by reference?

#include <iostream>
#include <string>
using std::cout;
using std::string;

template<typename T, int len>
int my_count(T (&arr)[len], T ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< len; ++i)
{
if(arr == ele)
++count;
}
return count;
}


Don't specialise, overload:

template<int len>
int my_count(string (&arr)[len], string ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< 3; ++i)
{
if(arr == ele)
++count;
}
return count;
}
 
C

Craig Scott

I am trying to write a function to count the number of occurances of a
particular element in an array. I am also trying to specialize the
function for strings. I am passing the array as reference to the
function. Is there a way I can avoid hardcoding the size of the
array? I know we can do it if we pass the array as a pointer but can
we do it for pass by reference?

#include <iostream>
#include <string>
using std::cout;
using std::string;

template<typename T, int len>
int my_count(T (&arr)[len], T ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< len; ++i)
{
if(arr == ele)
++count;
}
return count;

}

//can we avoid hardcoding the size of the array here?
template<>
int my_count<string>(string (&arr)[3], string ele)
{
int count = 0;
//cout << "LEN = " << len << std::endl;
for(int i=0; i< 3; ++i)
{
if(arr == ele)
++count;
}
return count;

}

int main()
{
string arr_str[] = {"123", "123", "123"};
cout << my_count<string>(arr_str, "123");
}


Your example has a much more concise implementation if you are willing
to make more use of the STL. Instead of a raw array of strings, you
can use a std::vector of strings

If it fits with what your main application wants to do, I'd recommend
that you prefer to use std::vector instead of a raw array of strings.
This is what the STL is meant for. The (more or less) equivalent of
your example follows, with no real need to specialize for strings:

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::vector<std::string> arr_str;
arr_str.push_back("123"); // There are better ways, but you
get the point
arr_str.push_back("123");
arr_str.push_back("123");

std::cout << std::count(arr_str.begin(), arr_str.end(),
"123");
}
 
C

Craig Scott

I am trying to write a function to count the number of occurances of a
particular element in an array. I am also trying to specialize the
function for strings. I am passing the array as reference to the
function. Is there a way I can avoid hardcoding the size of the
array? I know we can do it if we pass the array as a pointer but can
we do it for pass by reference?
#include <iostream>
#include <string>
using std::cout;
using std::string;
template<typename T, int len>
int my_count(T (&arr)[len], T ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< len; ++i)
{
if(arr == ele)
++count;
}
return count;

//can we avoid hardcoding the size of the array here?
template<>
int my_count<string>(string (&arr)[3], string ele)
{
int count = 0;
//cout << "LEN = " << len << std::endl;
for(int i=0; i< 3; ++i)
{
if(arr == ele)
++count;
}
return count;

int main()
{
string arr_str[] = {"123", "123", "123"};
cout << my_count<string>(arr_str, "123");
}


Your example has a much more concise implementation if you are willing
to make more use of the STL. Instead of a raw array of strings, you
can use a std::vector of strings

If it fits with what your main application wants to do, I'd recommend
that you prefer to use std::vector instead of a raw array of strings.
This is what the STL is meant for. The (more or less) equivalent of
your example follows, with no real need to specialize for strings:

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::vector<std::string> arr_str;
arr_str.push_back("123"); // There are better ways, but you
get the point
arr_str.push_back("123");
arr_str.push_back("123");

std::cout << std::count(arr_str.begin(), arr_str.end(),
"123");

}


Sorry, I accidentally omitted a required header. You should also
#include <algorithm> for the std::count function.
 
S

spekyuman

I am trying to write a function to count the number of occurances of a
particular element in an array. I am also trying to specialize the
function for strings. I am passing the array as reference to the
function. Is there a way I can avoid hardcoding the size of the
array? I know we can do it if we pass the array as a pointer but can
we do it for pass by reference?
#include <iostream>
#include <string>
using std::cout;
using std::string;
template<typename T, int len>
int my_count(T (&arr)[len], T ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< len; ++i)
{
if(arr == ele)
++count;
}
return count;
}
//can we avoid hardcoding the size of the array here?
template<>
int my_count<string>(string (&arr)[3], string ele)
{
int count = 0;
//cout << "LEN = " << len << std::endl;
for(int i=0; i< 3; ++i)
{
if(arr == ele)
++count;
}
return count;
}
int main()
{
string arr_str[] = {"123", "123", "123"};
cout << my_count<string>(arr_str, "123");
}

Your example has a much more concise implementation if you are willing
to make more use of the STL. Instead of a raw array of strings, you
can use a std::vector of strings
If it fits with what your main application wants to do, I'd recommend
that you prefer to use std::vector instead of a raw array of strings.
This is what the STL is meant for. The (more or less) equivalent of
your example follows, with no real need to specialize for strings:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> arr_str;
arr_str.push_back("123"); // There are better ways, but you
get the point
arr_str.push_back("123");
arr_str.push_back("123");
std::cout << std::count(arr_str.begin(), arr_str.end(),
"123");

Sorry, I accidentally omitted a required header. You should also
#include <algorithm> for the std::count function.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia- Hide quoted text -

- Show quoted text -


Listen to Craig Scott, the "Standard Template Library" was created for
programmers to utilize common procedures. Unless your objective is to
learn how to write template functions from scratch, utilize the STL.
Books written outside the last five years will claim the STL will cost
you time and overhead, that is not the case now. Make sure to buy more
recent programming references and try not to re-invent the wheel. Make
sure the book primarily covers containers and algorithms, as they are
the heart of STL.
 
A

Aarti

I am trying to write a function to count the number of occurances of a
particular element in an array. I am also trying to specialize the
function for strings. I am passing the array as reference to the
function. Is there a way I can avoid hardcoding the size of the
array? I know we can do it if we pass the array as a pointer but can
we do it for pass by reference?
#include <iostream>
#include <string>
using std::cout;
using std::string;
template<typename T, int len>
int my_count(T (&arr)[len], T ele)
{
int count = 0;
cout << "LEN = " << len << std::endl;
for(int i=0; i< len; ++i)
{
if(arr == ele)
++count;
}
return count;
}
//can we avoid hardcoding the size of the array here?
template<>
int my_count<string>(string (&arr)[3], string ele)
{
int count = 0;
//cout << "LEN = " << len << std::endl;
for(int i=0; i< 3; ++i)
{
if(arr == ele)
++count;
}
return count;
}
int main()
{
string arr_str[] = {"123", "123", "123"};
cout << my_count<string>(arr_str, "123");
}
Your example has a much more concise implementation if you are willing
to make more use of the STL. Instead of a raw array of strings, you
can use a std::vector of strings
If it fits with what your main application wants to do, I'd recommend
that you prefer to use std::vector instead of a raw array of strings.
This is what the STL is meant for. The (more or less) equivalent of
your example follows, with no real need to specialize for strings:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> arr_str;
arr_str.push_back("123"); // There are better ways, but you
get the point
arr_str.push_back("123");
arr_str.push_back("123");
std::cout << std::count(arr_str.begin(), arr_str.end(),
"123");
}

Sorry, I accidentally omitted a required header. You should also
#include <algorithm> for the std::count function.
- Show quoted text -

Listen to Craig Scott, the "Standard Template Library" was created for
programmers to utilize common procedures. Unless your objective is to
learn how to write template functions from scratch, utilize the STL.
Books written outside the last five years will claim the STL will cost
you time and overhead, that is not the case now. Make sure to buy more
recent programming references and try not to re-invent the wheel. Make
sure the book primarily covers containers and algorithms, as they are
the heart of STL.- Hide quoted text -

- Show quoted text -


Thanks for the help and advice. Yes I normally use STL but this time
around was trying to learn templates. Anyways..thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top