473,779 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic looping

Hi,

I need to a way to loop thru vectors (or some other appropriate collection
type)

using constructs such as:

for (pr = someVector.begi n(); pr != someVector.end( ); pr++)

SomeFunction(*p r);

OR

for_each(someVe ctor.begin(), someVector.end( ), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as it's

being looped over. Simply adding to the vector as it's being looped,
however,

produces strange results. But I know there must be *some* easy way to do
what I

want. Does anyone know what it is?

Thanks,

cpp




Jul 19 '05 #1
3 1835
cppaddict wrote:
Hi,

I need to a way to loop thru vectors (or some other appropriate collection
type)

using constructs such as:

for (pr = someVector.begi n(); pr != someVector.end( ); pr++)

SomeFunction(*p r);

OR

for_each(someVe ctor.begin(), someVector.end( ), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as it's

being looped over. Simply adding to the vector as it's being looped,
however,

produces strange results. But I know there must be *some* easy way to do
what I

want. Does anyone know what it is?


a) You can use an index

for (int pi = 0; pi < someVector.size (); pi++)

b) You can use std::list<>.
The advantage of a std::list is that you may add or remove elements
without invalidating iterators (that are still in the list). However,
depending on your algorithm you may end up removing an element that is
currently being pointed to be an iterator which means you need to manage
this using an indirection.

Jul 19 '05 #2
> I need to a way to loop thru vectors (or some other appropriate
collection
type)

using constructs such as:

for (pr = someVector.begi n(); pr != someVector.end( ); pr++)

SomeFunction(*p r);

OR

for_each(someVe ctor.begin(), someVector.end( ), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as it's
being looped over. Simply adding to the vector as it's being looped,
however, produces strange results.
Changes to the vector may invalidate the iterators.
But I know there must be *some* easy way to do
what I want. Does anyone know what it is?


Use indexes instead of iterators:

for(int i = 0; i < someVector.size (); ++i)
{
SomeFunction(so meVector[i]);
}
--
Peter van Merkerk
peter.van.merke rk(at)dse.nl

Jul 19 '05 #3
cppaddict wrote in news:sS******** **********@news svr27.news.prod igy.com:
Hi,

I need to a way to loop thru vectors (or some other appropriate
collection type)

using constructs such as:

for (pr = someVector.begi n(); pr != someVector.end( ); pr++)

SomeFunction(*p r);

OR

for_each(someVe ctor.begin(), someVector.end( ), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as
it's

being looped over. Simply adding to the vector as it's being looped,
however,

produces strange results. But I know there must be *some* easy way to
do what I

want. Does anyone know what it is?


You need to pick an appropriate container one that doesen't invalidate
its iterators after your insert/push_back.

You can use std::vector if 1) you have reserved enough space in the
vector, by calling reserve() and 2) you only append with push_back().

Alternativly std::deque<> has many of the properties of std::vector<>,
but in addition you can use push_back() (and also push_front()) without
invalidating iterators, (i.e. no need to call reserve()).

If you need to do inserts (i.e. not at the begining or end) then
you need to use std::list<>.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4

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

Similar topics

2
2168
by: jason | last post by:
What is the best way to build a dynamic html body message if one is looping through a customer table (=/=500 customers) and corresponding inventory table: 1. FSO html template with paramters 2. Build the thing in the .HTMLbody of CDO while looping 3. Another elegant method.... The customer email (TO: john@catco.com) would look something like this:
2
5321
by: theComputer7 | last post by:
I cut down the code to make this half way understandable... I have read Data Grid girls caution about over use of dynamic controls. I truly believe what I am doing requires dynamically inserted user controls. Worse I'm trying to add dynamic user controls from within a repeater loop (looping through attributes)... I bind to a function in the code behind and pass in the attribute. <asp:repeater id="rAttributes" Runat="server">
7
3391
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
12
6259
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE *********************************** set objRS = GetMyRecordSet() i=1 objRS.MoveFirst
2
7053
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
13
10185
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his first order. Likewise if he clicks the second LinkButton, he will be shown the details of the second order he had placed. The Text of the LinkButtons will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the LinkButtons. The problem is...
3
2946
by: Olaf Rabbachin | last post by:
Hi *, ich erzeuge zur Laufzeit in einer ASP-Tabelle Tabellenzellen. Im Großteil dieser Zellen befinden sich Checkboxes. Per JS ändere ich beim Click auf eine solche Zelle oder deren Checkbox die Farbe der Zelle. Problem: Bei einem history.back (oder Back-Button am Client) gehen die Client-seitig geänderten Zellfarben verloren. Ich suche nun einen möglichst performanten Weg, diese per StartupScript dort wiederherzustellen. Mein letzter...
5
9549
by: vcuankitdotnet | last post by:
Hi. I have tried to search for this online, but can't find what I'm looking for so I decided to come here. I have a ASP.NET 2.0 website with a masterpage. My .aspx page uses the masterpage. Here is the HTML from my .aspx page: <asp:Content ID="Content" ContentPlaceHolderID="MyContentArea" runat="server"> <asp:PlaceHolder ID="phControlHolder" runat="server"></ asp:PlaceHolder><br /><br /> <asp:Button ID="btnSubmitForm" runat="server"...
1
3668
by: PraveenAnekalmat | last post by:
Hi everyone, I'm using anchor within the for loop which means the number of anchors(links) dynamically varies. Within the anchor i'm calling one javascript function and passing the dynamic value (select ) in that. The problem is even i click any link only last value i.e..select value at i = j is passing because the looping will happen during loading the page itself...so how can i pass exact values? Do i need to set any ID for the tag?If so...
0
1884
by: sarakumarsg | last post by:
Hai, i would like to use dynamic sql for update records in the DB@ db. Means, the value to be updated will be send as concatenated string. Then based on the concatnation value i need to retrieve the records into cursor..then update the set.. how can i do that.. say for example, i would like to update the salary for all the employees in my company. I have different grade of employee. In my User interface(.net )i can view all the...
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7478
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.