English 中文(简体)
为什么C++在达到病媒能力后插入时会照顾到器具?
原标题:Why doesn t C++ take care of iterators when insertion is done in a vector after capacity is reached?

我写了这一小法,只是想看一个炉子实际上如何失效,在达到其能力后不会改变病媒的位置。

这里,病媒和能力的大小在最初的5个。 之后,我插入了病媒中的其他几个要素,但没有重新开始我的发件人点到myvector.begin()。 由此可见<代码>49在我产出中,在<代码>后,病媒最大尺寸为:1073741823时,再次印刷病媒元素。

My question is why C++ does not make point iterator again to a valid myvector.begin() after all elements are copied into new location?
This can also lead to some behavior which can be hard to debug. I know a safe way to work would be to always reinitialize the iterator just before using it.

  #include<iostream>
  #include<vector>
  #include<stdio.h>

  using namespace std;

  int main()
  {
    vector<int> myvector;
    vector<int>::iterator it;
    int myarray[]= {100,200,300,400};
    myvector.insert(it,500);
    it=myvector.begin();
    myvector.insert(it,myarray,myarray+4);
    it=myvector.begin();
    for(;it!=myvector.end();++it)
    cout <<*it<<endl;
    cout <<"size of vector is :" << myvector.size() <<"
"; 
    cout <<"capacity of vector is : " << myvector.capacity()<<"
";  
    cout <<"maximum size of vector is : " << myvector.max_size()<<"
"; 
    myvector.push_back(600);
    for(;it!=myvector.end();++it)
    cout <<*it<<endl;
  }
  Output of program :-
  100
  200
  300
  400
  500
  size of vector is :5
  capacity of vector is : 5
  maximum size of vector is : 1073741823
  49
  100
  200
  300
  400
  500
  600
最佳回答

因为这种做法不切实际,而且可能是不可能的。

矢量是否应当保留一份all的载体清单,并在被称作无效触发方法后立即修改all/strong>?

问题回答

变压器与病媒没有任何有意义的联系(如果是这样的话,它怎么能够作为点子加以实施)。 病媒不了解该媒介。 您的工作是不使用一种无效的炉.。

因此,你提议为......目的究竟是什么,为病媒类别增加一个复杂程度的吨位? 如何解决现实世界的问题,我们知道,做这样的事情是一种坏的想法?

或许是因为病媒必须跟踪其所有探测器的名单,并在它们失效时通知他们。 这将带来大量间接费用。 STL集装箱业务都有非常明确的无效规则,你只是要跟随这些规则的操作者。

请注意,按照标准,在插入或删除之后,你只能依赖病媒传播器。 在重新制定之前,它仍为你工作,这只是执行细节。

STL公司试图执行所有可能的集装箱。 虽然您的集装箱设计无疑是可能的,但它只是没有包括。 <代码>std:vector<T>似乎相似,但试图成为比T[]更好的阵列,几乎没有间接费用。 这一目标与你直接相符。

很幸运的是,STL的设计是模块式的,即使你确实写了自己的集装箱,以便照此行事,你仍然可以重复所有STL算法以及STL-兼容算法(例如,正在加固的算法)。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?