can swap function be used to free memory as in case of vectors as mentioned below?
std::vector<int> v1;
// somehow increase capacity
std::vector<int>().swap(v1);
can swap function be used to free memory as in case of vectors as mentioned below?
std::vector<int> v1;
// somehow increase capacity
std::vector<int>().swap(v1);
我不敢肯定我会理解你的问题,但你问,能否以同样的方式 empty一 set?
如果是,那么:
关于病媒的<代码>swap的定义是交换两种病媒的内容和。
set
doesn t have anything like capacity in its public interface, so it s not defined to swap capacity, just contents. There s no particular reason why a set implementation should over-allocate beyond what it needs. But if it does then there s no standard way to ensure that the "spare" memory is freed, since it s permitted to leave the spare memory where it is on a swap
.
是的,这一骗局使你能够把病媒的能力降至零:
std::vector<int> intVec;
intVec.reserve(100);
std::vector<int>().swap(intVec);
or
确切计算要素
std::vector<int> intVec;
intVec.reserve(100);
intVec.push_back(1);
std::cout << intVec.capacity() << std::endl; // prints 100
std::vector<int>(intVec).swap(intVec);
std::cout << intVec.capacity() << std::endl; // prints 1
for more on that look here: http://www.gotw.ca/gotw/054.htm
采用病媒的这种方式的理由是,如果你简单使用病媒代码< clear()或resize(
)方法,那么你的大小在逻辑上可能会被降到0,但所分配的记忆将留在那里重新使用。
如果你想要的是实际去除多余的记忆,那么你就会用空洞的病媒冲动,而这种媒介从来就从未分配过(尽管它可能分配一些)。
Doing the same with set may work too if set has allocated some "nodes" and decides to hold on to them for re-use although a set does not have a reserve() function so really it is up to the implementor of STL as to whether to release the memory or hold onto it.
由于<条码><>>/代码>不大可能保持超负荷能力,因此,互换成切碎。
此外,C++11在集装箱中添加了一个<代码>shrink_to_fit()的成员,如果可能有用的话。 因此,仅仅利用这一点,如果无法得到,那就不担心。
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...
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?