473,320 Members | 2,052 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,320 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 1966
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.