Hi all,
I have noticed that I over and over again write member functions which
iterate over vectors and other containers.
Is there a standard way of doing this, like assigning a template
function to a range of container iterators?
<example code>
//show the eventui vector
int y = 20, v_interval = 20;
std::vector<QxEventUI*>::iterator it = m_widget_vector.begin();
while( it != m_widget_vector.end() ){
(*it)->show();
y+=v_interval;
it++;
}
</example code>
In the above example I iterate and execute show() for each vector
element. I'd like to set a template function for the iteration and for
each element execute it, how to do that?
any help and advice highly appreciated.
Thank you
/B 1 3897
Bob Smith wrote: Hi all, I have noticed that I over and over again write member functions which iterate over vectors and other containers. Is there a standard way of doing this, like assigning a template function to a range of container iterators?
<example code>
//show the eventui vector int y = 20, v_interval = 20; std::vector<QxEventUI*>::iterator it = m_widget_vector.begin(); while( it != m_widget_vector.end() ){ (*it)->show(); y+=v_interval; it++; } </example code>
In the above example I iterate and execute show() for each vector element. I'd like to set a template function for the iteration and for each element execute it, how to do that?
You can use the 'for_each' generic algorithm:
template<class InputIterator, class Function>
Function for_each(InputerIterator first, InputIterator last, Function func);
a partial solution based on your example:
void showQxEventUI(QxEventUI * element) {
element->show();
}
void (*pfi=)(int)=showQxEventUI;
for_each(m_widget_vector.begin(),m_widget_vector.e nd(),pfi);
If you need to update the elements, the 'transform' generic algorithm
can be used.
In both cases, a problem remains to udate variables outside the scope of
the function or object in the list. For example, your y and v_interval
variables in your example. any help and advice highly appreciated. Thank you /B This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jose luis fernandez diaz |
last post by:
Hi,
Is this right any stl container (vector, deque, list, . . .)?
typedef vector container;
int main()
{
container<int> c1;
|
by: Steve |
last post by:
Hi Guys,
I have a string which contains data elements separated by spaces. I also
have a function which returns the number of characters from the beginning of
the string for a given number of...
|
by: phil_gg04 |
last post by:
Dear C++ Experts,
Over the last couple of months I have been writing my first program
using shared memory. It has been something of an "in-at-the-deep-end"
experience, to say the least. At...
|
by: Dave Hansen |
last post by:
OK, first, I don't often have the time to read this group, so
apologies if this is a FAQ, though I couldn't find anything at
python.org.
Second, this isn't my code. I wouldn't do this. But a...
|
by: Jean-Marc Blaise |
last post by:
IBM recommends to place each ts container on a different physical disk.
What about the impacts if all containers (DMS) are in the same FS (supported
by multiple disks) ?
Does DB2 preallocate...
|
by: krbyxtrm |
last post by:
hello i have this profile for iterating empty vectors:
+0.3us (microsecond) on intel pentium 2.4Ghz
can this added delay to my application be reduced?
i mean near zero delay, its very important....
|
by: Nindi73 |
last post by:
HI
If I define the class DoubleMap such that
struct DoubleMap : public std::map<std::string, double>{};
Is there any overhead in calling std::map member functions ?
Moreover are STL...
|
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= |
last post by:
I have some code where there's this vector of pointers to objects and
I need to delete and erase some of them, the problem is that to know
which I need to iterate through the vector and I'm trying...
|
by: frankw |
last post by:
Hi,
I have a hash_map with string as key and an object pointer as value.
the object is like
class{
public:
float a;
float b;
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |