473,407 Members | 2,315 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,407 software developers and data experts.

iterating over containers

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

Jul 19 '05 #1
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


Jul 19 '05 #2

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

Similar topics

7
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;
16
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...
14
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...
7
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...
1
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...
25
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....
15
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...
3
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...
5
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; ...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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
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...
0
isladogs
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...

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.