std::vector question

I havea program which on execution gives unpredictable behaviour (it
shouldn't). In trying to track down the problem, I'm wondering if there is a
difference between these two ways of filling a std::vector with data:

Method 1:

std::vector<int> v;
int k;

for(i=0;i<n;i++){
  k = i + 3;
  v.push_back(k);
}

Method 2:

std::vector<int> v;

for(i=0;i<n;i++){
  int k = i + 3;
  v.push_back(k);
}


0
h.dean (65)
12/29/2004 2:28:33 AM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
396 Views

Similar Articles

[PageSpeed] 14

"Hamish" <h.dean@xtra.co.nz> wrote...
>I havea program which on execution gives unpredictable behaviour (it
> shouldn't). In trying to track down the problem, I'm wondering if there is 
> a
> difference between these two ways of filling a std::vector with data:
>
> Method 1:
>
> std::vector<int> v;
> int k;
>
> for(i=0;i<n;i++){
>  k = i + 3;
>  v.push_back(k);
> }
>
> Method 2:
>
> std::vector<int> v;
>
> for(i=0;i<n;i++){
>  int k = i + 3;
>  v.push_back(k);
> }
>
>

No, in this particular case there is no difference.  However, something
tells me that your real code, the code that gives you trouble, is a bit
more complicated than that.

It is often important to follow certain rules in your programming.  For
example, the famous "Rule of Three" (look it up).  If your class has
some kind of dynamic memory management, you simply _must_ follow it.

V


0
v.Abazarov (15)
12/29/2004 2:58:49 AM
"Hamish" <h.dean@xtra.co.nz> wrote in message
news:oDoAd.3372$mo2.171844@news.xtra.co.nz...
> I havea program which on execution gives unpredictable behaviour (it
> shouldn't). In trying to track down the problem, I'm wondering if there is
a
> difference between these two ways of filling a std::vector with data:
>
> Method 1:
>
> std::vector<int> v;
> int k;
>
> for(i=0;i<n;i++){
>   k = i + 3;
>   v.push_back(k);
> }
>
> Method 2:
>
> std::vector<int> v;
>
> for(i=0;i<n;i++){
>   int k = i + 3;
>   v.push_back(k);
> }
>

The only difference with these two methods is the scope of k but this
doesn�t affect the behavior in this case. Please post your real code which
gives you trouble.

Cheers
Chris


0
12/29/2004 9:57:17 AM
> > I havea program which on execution gives unpredictable behaviour (it
> > shouldn't). In trying to track down the problem, I'm wondering if there
is
> a
> > difference between these two ways of filling a std::vector with data:
> >
> > Method 1:
> >
> > std::vector<int> v;
> > int k;
> >
> > for(i=0;i<n;i++){
> >   k = i + 3;
> >   v.push_back(k);
> > }
> >
> > Method 2:
> >
> > std::vector<int> v;
> >
> > for(i=0;i<n;i++){
> >   int k = i + 3;
> >   v.push_back(k);
> > }
> >
>
> The only difference with these two methods is the scope of k but this
> doesn�t affect the behavior in this case. Please post your real code which
> gives you trouble.

I haven't been able to track down the problem yet. However, the only
difference between an older version which worked, adn this version is the
following data structures:

std::vector<PolyTypeClass*> PolyTypes;

class PolyTypeClass{
public:
 PolyTypeClass();
 virtual ~PolyTypeClass();

 const PolyTypeClass& operator= (const PolyTypeClass& poly);
 void Copy(PolyTypeClass * pCopy);

 int ID;
 std::vector<BasePolygonClass> Rot;
};

class BasePolygonClass{
public:
 BasePolygonClass();
 BasePolygonClass(int Size);
 virtual ~BasePolygonClass();

 const BasePolygonClass& operator= (const BasePolygonClass& poly);
 void Copy(BasePolygonClass * pCopy);

 std::vector<PointPropClass> Points;
 double Area;
 double Length;
 double Height;
 BOOL IsConvex;
 double Angle;
 BOOL XFlip;
 BOOL YFlip;
};

class PointPropClass
{
public:
 PointPropClass();
 virtual ~PointPropClass();
 const PointPropClass& operator= (const PointPropClass& poly);
 void Copy(PointPropClass * pCopy);

 BOOL TP;
 double Angle;
 int Num;
 int CavNum;
 int St;
 int Fin;
 int Type;
 BOOL Neg;
 BOOL IsGhosh;
 double x;
 double y;
};


0
h.dean (65)
12/30/2004 4:57:14 AM
"Hamish" <h.dean@xtra.co.nz> wrote in message
news:PULAd.3637$mo2.201785@news.xtra.co.nz...
> > > I havea program which on execution gives unpredictable behaviour (it
> > > shouldn't). In trying to track down the problem, I'm wondering if
there
> is
> > a
> > > difference between these two ways of filling a std::vector with data:
> > >
> > > Method 1:
> > >
> > > std::vector<int> v;
> > > int k;
> > >
> > > for(i=0;i<n;i++){
> > >   k = i + 3;
> > >   v.push_back(k);
> > > }
> > >
> > > Method 2:
> > >
> > > std::vector<int> v;
> > >
> > > for(i=0;i<n;i++){
> > >   int k = i + 3;
> > >   v.push_back(k);
> > > }
> > >
> >
> > The only difference with these two methods is the scope of k but this
> > doesn�t affect the behavior in this case. Please post your real code
which
> > gives you trouble.
>
> I haven't been able to track down the problem yet. However, the only
> difference between an older version which worked, adn this version is the
> following data structures:
>
> std::vector<PolyTypeClass*> PolyTypes;
>
> class PolyTypeClass{
> public:
>  PolyTypeClass();
>  virtual ~PolyTypeClass();
>
>  const PolyTypeClass& operator= (const PolyTypeClass& poly);
>  void Copy(PolyTypeClass * pCopy);
>
>  int ID;
>  std::vector<BasePolygonClass> Rot;
> };
>
> class BasePolygonClass{
> public:
>  BasePolygonClass();
>  BasePolygonClass(int Size);
>  virtual ~BasePolygonClass();
>
>  const BasePolygonClass& operator= (const BasePolygonClass& poly);
>  void Copy(BasePolygonClass * pCopy);
>
>  std::vector<PointPropClass> Points;
>  double Area;
>  double Length;
>  double Height;
>  BOOL IsConvex;
>  double Angle;
>  BOOL XFlip;
>  BOOL YFlip;
> };
>
> class PointPropClass
> {
> public:
>  PointPropClass();
>  virtual ~PointPropClass();
>  const PointPropClass& operator= (const PointPropClass& poly);
>  void Copy(PointPropClass * pCopy);
>
>  BOOL TP;
>  double Angle;
>  int Num;
>  int CavNum;
>  int St;
>  int Fin;
>  int Type;
>  BOOL Neg;
>  BOOL IsGhosh;
>  double x;
>  double y;
> };
>

What strikes me (although in this case it should not pose a problem after a
quick glance at your code) is that you do supply a dtor and an assignment op
but not a copy ctor. If you really need a dtor & an assignment op it�s good
practice to supply a copy ctor too.

If you could please post the actual code of "Method 1" & "Method 2" which
works with your data structures and also the implementation of the Copy()
method would be interesting.

Cheers
Chris


0
12/30/2004 8:28:15 AM
Reply:

Similar Artilces:

Questions on RDC over HTTP
I hosted my own mail server (Exchange 2003 Enterprise) at home. And I want to use RDC over Http feature. (ex. I can login to my Exchange inbox even though I am not inside my LAN and without using VPN) However, the setup is not quite successful because it kind of telling me that SSL is required for this feature. basically, can I run this feature without SSL? Thanks. The general answer is no. :-) You can setup your own CA for this purpose. -- This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples (if any) are subject to the terms...

Time question #2
when the user inputs data into cell C8 I want the current time to be displayed in cell D8, and remain there without changing value, so if the user inputs the number1 into cell C8 the time 17:00 is shown in cell D8, and from that moment on it does not change. Can this be done, if so any ideas?? Thanks It can be done using circular reference or code http://www.mcgimpsey.com/excel/timestamp.html -- Regards, Peo Sjoblom (No private emails please) "Anthony" <Anthony@discussions.microsoft.com> wrote in message news:BBA371C7-550B-408B-B373-34AA1EF892D5@microsoft.com......

basic question
Hi, I have never used excel for math problems before and am having a little trouble with basic equations. I entered the eqn in the function (using the column names such as G1-G2 etc...) but now I'm a little lost. When I drag the equation down to fill in the rest of the column nothing happens. Is there an intermediate step I'm missing? Thank you!!! Tools>options>calculation and make sure it is set to automatic -- Regards, Peo Sjoblom "M121385" <M121385@discussions.microsoft.com> wrote in message news:BB7CFE63-A301-4F3F-A723-4EAF201D3071@microsoft.co...

User Guide Question
I have Money 2004 and I was wondering if there was a way to print out the entire user guide without having to save every screen? I'm getting more frustrated with Money as time goes on, I thought M05 would help me with things but it doesn't appear to be that way. I might think about switching to Quicken. I like the fact that they have books about how to do certain things and such. There isn't any way to print the on-net User's Guide in total that's been identified here. I find it hard to imagine that M05 will fix any issues you are having with M04--indeed it see...

PKI Question (IECA)
Has anybody had to deal with the not so new requirement by DoD to implement PKI for all contractors who need to exchange email between themselves and DoD? The requirement is called IECA. I'm just trying to find out if anyone has had to do this, what's involved, besides paying the $100-$150 extortion fee to simply verify who I say I am. Thanks, Tom I haven't had to deal with this, but a Google search turns up the following. http://www.digsigtrust.com/federal/dod.html "Tom Pennington" <NONEt2pennington@comcast.net> wrote in message news:u84WAabTEHA.2128@TK2MS...

A simple vlookup question
Hello, So I have the text "abc301" in cell A1 and "301" in cell A2. When I do a vlookup using A2 as the lookup value, it works. However, if I use RIGHT(A1, 3) as the lookup value, I get a N/A. Does anyone know how I can use the 3 final characters in a cell as the lookup value? Hi, I do not really understand what you want to do. Is cell A2 (301) a figure or text? Try this. =VLOOKUP(VALUE(RIGHT(A1;3));A1:A2;1;FALSE) Wkr, JP "eggman2001" <sodani@gmail.com> wrote in message news:4f658713-79ba-49a9-897c-7aeeef8ef1f0@z28g2000vbl.googlegroups.com... > ...

Dataset serialization question
I am creating a dataset like so: data = new DataSet("_DATA"); DataTable mm = new DataTable("T_IDS"); mm.Columns.Add(new DataColumn("C_PKEY",Type.GetType("System.Int32"))); mm.Columns.Add(new DataColumn("C_IDS",Type.GetType("System.Collections.ArrayList"))); data.Tables.Add(mm); and later I use the WriteXml method of the dataset to serialize it and after that readXml to deserialize it. Unfortunately the ArrayList ends up just saying System.Collections.ArrayList. So it doesn't deserialize properly. It is an arraylist of int...

Upgrade question
I want to get Office Publisher upgrade version. If I get Office Home and Student will this qualify me for the upgrade version of publisher? I also have Works Suite 2002. Will this qualify me for the Publisher upgrade as well? Thanks Yes to Office Home and Student 2007; no to Works Suite. You need one of the following: Microsoft Publisher 2000-2002; Microsoft Office Publisher 2003; Microsoft Publisher Deluxe 2000-2002; Microsoft Office Publisher 2003 with Digital Imaging; any 2000-2007 Microsoft Office suite; any Microsoft Office XP suite. -- JoAnn Paules MVP Micros...

Linking error LNK2001
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error: Creating library Release/fnimqcmd.lib and object Release/fnimqcmd.exp CoIMQCmd.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ traits@D@std@@V?$allocator@D@2@@std@@AAEXG@Z) CoIM...

Searching question
My sheet contains several rows of data and the number or rows will grow each day. At the end of the year, I'm guessing that this sheet will contain between 500 and 600 rows of data. I created a search macro which basically steps though each row one at a time (using a For-Next loop). I then do simple string comparison to check if the row of data matches the search criteria. If anyone else here was given the task of creating a similar searching system in Excel, what would you do?? I often hear about using auto filters and VLOOKUP & MATCH functions, so I often wonder if ...

2 Questions
Q1) I have 2 different formulas in 2 different columns, now no matte what I want these formulas in those columns all the way down. Is ther a way to fill an entire column with the following formulas, beside using the fill option? =IF($G10="",(IF($K10="",(IF($D10="","",(SUM(EDATE($B10,12)+$I10)))),"")),"") =IF($G10="",(IF($D10="","",(IF($C10="","",(SUM(EDATE($C10,12))+$I10))))),"") Q2) Is there a way to lock only certain cells or columns withou locking the entire worksheet...

dynamic menu question
Hello, this code below works if I have static menu created in resource editor with some IDR_ but when I create menu dynamically how do I attach new IDR_ value to it? HMENU m_wDynaMenu; m_wDynaMenu = ::CreateMenu(); AppendMenu(m_wDynaMenu, MF_STRING, IDR_DYNAMENU, _T("first entry")); m_Button.SetMenu(IDR_DYNAMENU, CWnd::GetSafeHwnd() ); how to associate IDR_DYNAMENU with this menu? probably I'm doing something stupid here :) thanks You have to get the menu item with code like: CMenu* pMenu = AfxGetMainWnd()->GetMenu(); Then you modify the menu dynamically. If you ...

A Different Question About Colors #2
I use Outlook 2003 SP-2 I started using the Organize tool to assign colors to mail from certain people. I now find that I�d like to change or eliminate the colors for certain of those people, and it won�t let me. It�ll say Done! As it always does, but the colors don�t change. Any thoughts are appreciated. ** Posted from http://www.teranews.com ** you need to go to Views, Customize, current view and edit the colors under autoformatting. -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com Out...

question about saving
I have an excel worksheet that I use to prepare a weekly work schedule. It contains popup menus that I use to select certain variables such as dates, times and people. Once I have a weekly schedule set, I want to distribute it electronically but without the popup menus being active. How can I do this? thanks ken -- For email response, please remove ***NOSPAM*** from address Assuming the "popup menus" are data validation (DV) lists, try this to find / kill all DVs in the sheet at one go Press F5 > Special > Check "Data validation > All "> OK (above will ...

Very simple question
Hi everyone, I'm new to Visual C++. I'm trying to write an application which takes user input (like an int), does some calculations, and displays the results in a window on screen. I've created an edit box for the user to enter the int, and added a variable which will take on the value of this int. How do I display the result of the calculation? I suspect I need to create a dialog box and use some variation of a Set command. Is there an easy way to do this? Thanks in advance for any help. Daryl Try creating another edit box for the output? Daryl wrote: > Hi everyone, ...

Questions
Hi, Which could be the questions asked in interview vc++, MFC, ATL COM . If anybody suggests link of the website, that is welcome too. Thank you, Lee <yang__lee@ausi.com> wrote in message news:1143779032.016063.201900@g10g2000cwb.googlegroups.com... > Hi, > Which could be the questions asked in interview vc++, MFC, ATL COM . > If anybody suggests link of the website, that is welcome too. http://www.techinterviews.com/?p=56 -- Ajay Kalra [MVP - VC++] ajaykalra@yahoo.com search MFC FAQ, ATL FAQ in the google. I see Ajay posted the Microsoft version which is good. One th...

simple form question
Is there a property that i can set on a text box that every time I open a form the text box will be empty? -- Message posted via http://www.accessmonster.com No property, but put code in the form's Load event to set it to blank: Private Sub Form_Load() Me!MyTextBox = Null End Sub -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!) "jjsaw5 via AccessMonster.com" <u34872@uwe> wrote in message news:7550cf12fd111@uwe... > Is there a property that i can set on a text box that every time I open a > form the text box will be empty?...

Project Center questions
I'm very interested in the Project Center of the new 2004 Entourage. If it's as good as I think it can be, I can imagine an entire website set up for those using this feature. It could be the holy grail of those who've read David Allen's Getting Things Done, those who have used Franklin/Covey's time management ideas, or anyone trying to keep their hectic life in order. I'm stumbling around with it currently. I've read the $5 eBook by Tom Negrino (http://www.tidbits.com/takecontrol/), but still many questions. Here are my latest queries: 1. Has anyone figured out h...

transparency question
P2007 Will output for commercial printer when done. Design checker points out 4 graphics in pub have transparency...fixed 3 of them (pngs). The other is a MS clip art of a Christmas wreath that I am using for a frame around another graphic (transparent center I guess) - will I have a problem with this? tnx -- ~~~~~~~~~~~~~~~~~~ Rob Giordano Microsoft MVP Expression ...

exchange2003 std Allocated Memory
like in 5.5, is now possible to set the amount of memory exchange uses? Setting report limits higher is not controller the monster. Thank you for your support. Yes, you can through the Performance Optimizer. However, that is not a recommended practice. Be aware that Exchange's Information Store Service uses all available memory for caching of data, but will release memory when needed by other services. Are you sure that you're seeing a real problem and not an illusory one? -- Ed Crowley MVP - Exchange "Protecting the world from PSTs and brick backups!" "BrianMu...

enable/disable question
Is it possible to disable the something like below? I dont see an enabled property... //////////////////////////////////////////////// <a id="FurtherActionHyperlink" href="#" onclick="window.open('SORFollup.aspx?SORID=<%# Eval("SORID") %>', '', 'toolbar=0, height=320,width=375,resizable=0,scrollbars=0');return false"> Further Action</a> ////////////////////////////////////////////// if not, any suggestions on how to try launching a popup in the same manner? Thanks On Sep 15, 2:07=A0am, D...

Drop Down List Questions
Hi, I have a couple of problems with drop down lists I was hoping to get some help on: 1) The drop down lists appear with the list displayed in the middle of the list instead on the first item, can that be changed so the list appears at the top of the list instead of in the middle? 2) Two of the columns are an item name (A) followed by the item number (B). Is it possible to have the item number in column B enter automaticly based on the name I select in column A? Thanks in advance, Tagg Hi for the second question you may use VLOOKUP. See: http://www.mvps.org/dmcritchie/excel/vlooku...

Windows 2008 Std as a member server of SBS 2003
Hi everyone, We have a Windows 2003 std running as a Terminal server, being a member server of a SBS 2003 R2. We are in process of changing our TS but will keep the SBS 2003. Will I have any problems having a Windows 2008 as a member server for the SBS 2003? I know that it is recommended not to have the TS as the member domain, but is it possible to have the 2008 promoted as member domain of a SBS 2003? Your comments are appreciated Regards Hi Mehran, Please take a look here: http://technet.microsoft.com/en-us/library/cc708131%28WS.10%29.aspx Regards, Frank "...

Question for the real experts!
What does Excel do AFTER i calculated the sheet with (f9). (calculation is set to manual) I have the following problem: In cells F2-I5000 (and R2-U5000 AC2-AF5000 AO2-AR5000) are formulas reffering to cells in A2-E5000.(and m1-q5000 x1-ab5000 aj1-an5000) In if I clear cells A2-E5000 (and the other ranges) with a macro then before F9 it is really fast.(1 second) After F9 it's really slow(12 seconds) The marco is this: Cells(1, 1) = Now() Range("A2:e5000").Select Selection.ClearContents Range("m1:q5000").Select Selection.ClearContents Range("x1:ab5000").Se...

Noob question
I really need help wiht this problem . How do you alter a column size? I did it once where the cursor would just move it bigger or smaller. I want to know how you do that. I would be very grateful for any help. Thanks -- Micro_pal ------------------------------------------------------------------------ Micro_pal's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=26941 View this thread: http://www.excelforum.com/showthread.php?threadid=401635 Move the cursor to the right-hand size of the column header, click the mouse, the column becomes lightly highlighted, a...