473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Templates specialization

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[i] == 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[i] == ele)
++count;
}
return count;
}

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

Jul 10 '07 #1
5 1206
On 2007-07-10 13:18, Aarti wrote:
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[i] == 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[i] == ele)
++count;
}
return count;
}

--
Erik Wikström
Jul 10 '07 #2
On Jul 10, 9:18 pm, Aarti <aarti.sa...@gmail.comwrote:
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[i] == 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[i] == 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::stringarr_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");
}

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Jul 10 '07 #3
On Jul 10, 9:35 pm, Craig Scott <audiofana...@gmail.comwrote:
On Jul 10, 9:18 pm, Aarti <aarti.sa...@gmail.comwrote:
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[i] == 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[i] == 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::stringarr_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 <algorithmfor the std::count function.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Jul 10 '07 #4
On Jul 10, 7:43 am, Craig Scott <audiofana...@gmail.comwrote:
On Jul 10, 9:35 pm, Craig Scott <audiofana...@gmail.comwrote:


On Jul 10, 9:18 pm, Aarti <aarti.sa...@gmail.comwrote:
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[i] == 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[i] == 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::stringarr_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 <algorithmfor 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.

Jul 10 '07 #5
On Jul 10, 5:58 am, spekyuman <spekyu...@gmail.comwrote:
On Jul 10, 7:43 am, Craig Scott <audiofana...@gmail.comwrote:


On Jul 10, 9:35 pm, Craig Scott <audiofana...@gmail.comwrote:
On Jul 10, 9:18 pm, Aarti <aarti.sa...@gmail.comwrote:
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[i] == 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[i] == 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::stringarr_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 <algorithmfor 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.- 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

Jul 11 '07 #6

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
12
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler...
6
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
4
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class...
2
by: Shekhar | last post by:
template<typename T> struct A{}; //line 1 template<typename T> struct B{}; //line 2 template<typename T> struct B<A<T> > {}; //line 3: partial specialization of B VC6.0 compiler results for the...
6
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally...
6
by: vch | last post by:
When defining templates, can I cound on the compiler to parse only those templates that are actually used? For example, in the following definitions: <code> template <class T> struct Conv...
16
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 ]
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.