简体   繁体   English

矢量和倾销

[英]vector and dumping

From what i know a vector is guaranteed to be continuous and i can write a chunk of memory to it and do send of fwrite with it. 据我所知,向量肯定是连续的,我可以向其中写入一块内存,并使用它发送fwrite。 All i need to do is make sure i call .resize() to force it to be the min length i need then i can use it as a normal char array? 我需要做的就是确保我调用.resize()强制其为我需要的最小长度,然后可以将其用作普通char数组? would this code be correct 此代码正确吗

v.resize(numOfElements);
v.clear(); //so i wont get numOfElements + len when i push back
vector<char>v2;
v2.resize(numOfElements*SizeOfType);
while(...)
{
...
v.push_bacK(x); 
}
compress(&v2[0], len, &v[0], len);
fwrite(&v2[0], ....)

noting that i never push back or pop v2 i only resize it once and used it as a char array. 请注意,我从不后退或弹出v2,我只调整了一次大小并将其用作char数组。 Would this be safe? 这样安全吗? and if i also dumped v that would also be safe(i do push back and clear, i may dump it for testing) 如果我也转储了v也将是安全的(我确实将其推回并清除,我可能会将其转储用于测试)

v.resize(numOfElements);
v.clear(); //so i wont get numOfElements + len when i push back

Well, that above code snippet is in effect allocating and creating elements, just to destroy them again. 好吧,上面的代码片段实际上是在分配和创建元素,只是要再次销毁它们。 It's in effect the same as: 实际上与以下内容相同:

v.reserve(numOfElements);

Just that this code is way faster. 只是这段代码更快。 So, v.size() == 0 in both cases and v.capacity() might be the same as numOfElements in both cases too (although this is not guaranteed ). 所以, v.size() == 0在这两种情况下和v.capacity() 可能是相同的 numOfElements在这两种情况下,太(虽然这不能保证 )。 In the second case, however, the capacity is at least numOfElements , which means the internal buffer will not be reallocated until you have push_back'ed that many elements to your vector. 但是,在第二种情况下,该容量至少为numOfElements ,这意味着在将push_back的向量中包含这么多的元素之前,不会重新分配内部缓冲区。 Note that in both cases it is invalid if you try accessing any elements - because there are zero elements actually contained. 请注意,在两种情况下,如果您尝试访问任何元素都是无效的 -因为实际包含的元素为零。

Apart from that, i haven't figured a problem in your code. 除此之外,我还没有在您的代码中发现问题。 It's safe and i would encourage it so use it instead of a raw new or malloc because of the added safeties it provides. 它是安全的,我会鼓励使用它,而不是原始的newmalloc因为它提供了更多的安全性。 I'm however not sure what you mean by "dump v". 但是,我不确定“转储v”是什么意思。

Indeed, std::vector is guaranteed to be contiguous, in order to be layout-compatible with a C array. 实际上,为了与C数组在布局上兼容,可以保证std :: vector是连续的。 However, you must be aware that many operations of the vector invalidate all pointers pointing to its elements, so you'd better stick to one type of use: avoid mixing pointer arithmetic and method calls on the vector. 但是,您必须意识到向量的许多操作都会使指向其元素的所有指针无效,因此您最好坚持使用一种类型:避免在向量上混合使用指针算术和方法调用。

Apart from that is perfectly correct, except the first line : what you want is 除此之外,这是完全正确的,除了第一行:您想要的是

v.reserve(numOfElements);

which will allocate enough place to store numOfElements into the vector, whereas 这将分配足够的空间以将numOfElements存储到向量中,而

v.resize(numOfElements);

will do the following: 将执行以下操作:

// pseudo-code
if (v.size() < numOfElements)
    insert (numOfElements - size) elements default 
    constructed at the end of the vector

if (v.size() > numOfElements)
    erase the last elements so that size = numOfElements

To sum up, after a reserve you are sure that vector capacity is superior or equal to numOfElements, and after a resize you are sure that vector size is equal to numOfElements. 综上所述,在reserve您确定向量容量大于或等于numOfElements,在resize您确定向量大小等于numOfElements。

For something like this I would personally use a class like STLSoft's auto_buffer<> : 对于这样的事情,我个人将使用STLSoft的auto_buffer<>这样的类:

As a disclaimer - I don't use the actual STLSoft library version, I've adapted my own template that is quite similar - I started from the Matthew Wilson's (the STLSoft author's) book "Imperfect C++" . 作为免责声明-我没有使用实际的STLSoft库版本,我改编了自己的模板,该模板非常相似-我是从Matthew Wilson(STLSoft作者的) “ Imperfect C ++”一书开始的。

I find it useful when I really just want a plain-old C array, but the size must be dynamic at runtime. 当我真的只想要一个普通的C数组时,我发现它很有用,但是大小在运行时必须是动态的。 auto_buffer<> is safer than a plain old array, but once you've constructed it you have no worries about how many elements are there or not - it's always whatever you constructed it with, just like an array (so it's a bit less complex than vector<> - which is appropriate at times). auto_buffer<>比普通的旧数组更安全,但是一旦构造完成,您就不必担心有多少个元素了-就像数组一样,它总是用它来构造的(所以它不太复杂)比vector<> -有时更合适)。

The major downside to auto_buffer<> is that it's not standard and it's not in Boost, so you either have to incorporate some of STLSoft into your project or roll your own version. auto_buffer<>的主要缺点是它不是标准的,也不在Boost中,因此您必须将STLSoft集成到项目中或发布自己的版本。

Yes you use a vector of char as a buffer for reading raw input. 是的,您将char向量用作读取原始输入的缓冲区。

// dynamically allocates a buffer of 10,000 char as buffer
std::vector<char>   data(10000);

fread(&data[0], sizeof(char),data.size(),fp);

I would not use it for reading any non POD data type directly into an vector though. 我不会用它来将任何非POD数据类型直接读入向量中。

You could potentially use a vector as a source for a write. 您可能会使用向量作为写入的来源。
But I would be very carefull how you read that back in (it may be easier to serialize it). 但是,我会非常小心地读回您的内容(序列化可能会更容易)。

fwrite(&data[0], sizeof(char),data.size(),fp);

You're replacing reserve() with resize, you may as well replace 您正在用resize替换reserve(),也可以替换

vector<char> v2

with

vector<Type> v2

This should simplify the code a tiny bit. 这样应该可以简化代码。

To be frank, it's the oddest use of vectors I've ever seen, but it probably will work. 坦率地说,这是我见过的最奇怪的向量用法,但它可能会起作用。 Are you sure you don't want to go with new char[size] and some sort of auto pointer from boost? 您确定不想使用新的char [size]和boost的某种自动指针吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM