473,218 Members | 1,693 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,218 software developers and data experts.

simply code with templete

mos
Hi!

There are two function almost the same:

function removelist()
{
for(list<a*>::iterator it = m_list.begin(); it != m_list.end(); )
{
a* p = *it;
if (!p->IsValid())
{
delete p;
it = m_list.erase(it)
}
else
++it;
}
}

function removemap()
{
for(map<int,a*>::iterator it = m_map.begin(); it != m_map.end(); )
{
a* p = it->second;
if (!p->IsValid())
{
delete p;
it = m_map.erase(it)
}
else
++it;
}
}

how can I use templete to simply the code like
template<class ...>
function removeT(...)
{
...
}

the problem is the list: p = *it and map: p = it->second

then
function removelist()
{
removeT(m_list)
}

function removemap()
{
removeT(m_map)
}


Jul 3 '06 #1
4 1961
mos <mm*******@163.comwrote:
Hi!

There are two function almost the same:

function removelist()
{
for(list<a*>::iterator it = m_list.begin(); it != m_list.end(); )
{
a* p = *it;
if (!p->IsValid())
{
delete p;
it = m_list.erase(it)
}
else
++it;
}
}

function removemap()
{
for(map<int,a*>::iterator it = m_map.begin(); it != m_map.end(); )
{
a* p = it->second;
if (!p->IsValid())
{
delete p;
it = m_map.erase(it)
}
else
++it;
}
}

how can I use templete to simply the code like
template<class ...>
function removeT(...)
{
...
}

the problem is the list: p = *it and map: p = it->second
Use an accessor. So in essence, you call a function, pass it the
iterator and it returns the value you want.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 3 '06 #2
mos
Hi!
>
>the problem is the list: p = *it and map: p = it->second

Use an accessor. So in essence, you call a function, pass it the
iterator and it returns the value you want.
Thanks for your advise, I try like this:

class a
{
public:
bool IsValid(){
return false;
}
};

list<a*m_list;
map<int,a*m_map;

template<class T>
class wrap
{
public:
typedef typename T* Output;
typedef typename list<Output>::iterator Input1;
typedef typename map<int,Output>::iterator Input2;
Output m_p;
wrap(Input1 in1){
m_p = *in1;
}
wrap(Input2 in2){
m_p = in2->second;
}
Output operator() (){
return m_p;
}
};

template<class LIST, class T>
void removeT(LIST& m_list)
{
for(LIST::iterator it = m_list.begin(); it != m_list.end(); )
{
T* p = wrap<T>(it)();
if (!p->IsValid())
{
delete p;
it = m_list.erase(it);
}
else
++it;
}
}

void test()
{
removeT<list<a*>,a>(m_list);
removeT<map<int,a*>,a>(m_map);
}

it is work, am I right, or any better suggestion?
Best Regard.
mos.
Jul 3 '06 #3
mos <mm*******@163.comwrote:
Hi!
>>
>>the problem is the list: p = *it and map: p = it->second

Use an accessor. So in essence, you call a function, pass it the
iterator and it returns the value you want.

Thanks for your advise, I try like this:

class a
{
public:
bool IsValid(){
return false;
}
};

list<a*m_list;
map<int,a*m_map;

template<class T>
class wrap
{
public:
typedef typename T* Output;
typedef typename list<Output>::iterator Input1;
typedef typename map<int,Output>::iterator Input2;
Output m_p;
wrap(Input1 in1){
m_p = *in1;
}
wrap(Input2 in2){
m_p = in2->second;
}
Output operator() (){
return m_p;
}
};

template<class LIST, class T>
void removeT(LIST& m_list)
{
for(LIST::iterator it = m_list.begin(); it != m_list.end(); )
{
T* p = wrap<T>(it)();
if (!p->IsValid())
{
delete p;
it = m_list.erase(it);
}
else
++it;
}
}

void test()
{
removeT<list<a*>,a>(m_list);
removeT<map<int,a*>,a>(m_map);
}

it is work, am I right, or any better suggestion?
I haven't tried it, but it looks alright. As for a better
suggestion: you are limiting yourself to the list and map containers. If
you later decide to use a vector instead of a list, you will have to
manually change your wrap function. Here is what I came up with:
// general implementation for containers
template <typename C>
struct value_of
{
inline static typename C::value_type get (typename C::iterator i)
{
return *i;
}
};

// specialized implementation for maps
template <typename K, typename V>
struct value_of < typename std::map <K, V
{
inline static typename std::map <K, V>::value_type::second_type get
(typename std::map <K, V>::iterator i)
{
return i->second;
}
};

template <class C>
void remove (C& container)
{
for (typename C::iterator it = container.begin (); it !=
container.end(); )
{
if (!value_of <C>::get (it)->IsValid ())
{
delete value_of <C>::get (it);
it = container.erase (it);
}
else
{
++ it;
}
}
}

Also note that you no longer have to call your function like
"removeT <list <a*>, a(m_list);". Simply writing "remove (m_list);" is
sufficient (now the compiler will plug in all the necessary details for
you).

regards
--
jb

(reply address in rot13, unscramble first)
Jul 3 '06 #4
mos
Hi!

It is exactly I want!
and I learned a lot, thanks a lot.

Best regard.
mos.
Jul 3 '06 #5

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

Similar topics

2
by: basulasz | last post by:
I have a templete column, and a DropDownList in header templete. I want to add a SelectedIndexChanged event to that DropDownList. But I couldn't. How can I do that? Thanks... -- No Sign
0
by: Lance Geeck | last post by:
I am trying to move from VB6 to .NET. I have the enterprise version of VS.NET. I want to be able to build an application using the Enterprise templetes. These include projects for...
17
by: Barret Bonden | last post by:
As an old programmer just now looking at VB.net I have a question: How does one simply open one form from another ? I don't mean how does one create a new instance of that form , but rather how...
3
by: MDB | last post by:
I have a datagrid with an drop down list item templete and was wondering how I get the value of this? The datagrid is called dgProducts and the drop down list is called ddQty. Thanks in Advance...
10
by: Ju Hui | last post by:
$a="my result=<?=2+2?>" echo $a I want to get :my result=4; how to write this script? any comments are welcome... thanks.
2
by: spiritedSmile | last post by:
Hi, I would like to write ASP code to create a custom report for a simply accounting database. The databse is saved with a .sdb extension. I am able to access the data via access or excel so I...
28
by: jverri01 | last post by:
First, I am relatively new to working with variables. Most of my experience has been with interface design. i am using ACCESS ver. 2003, running in Windows XP. Second, I spent an hour searching...
2
by: movieking81 | last post by:
Hello All, I have a simply syntax question. I'm new to PHP, but I have worked with ASP for years. Can you reference a database field on an html page with PHP the same way you can with ASP. For...
2
by: billybob206 | last post by:
Howdy, Is there a way to make the Insert Item Templete in the FormView in VS ASP.Net the default view of a webpage. (The purpose is I'm trying to create a new user page without having to search...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.