简体   繁体   中英

How can I convert a C++ vector to C-style array?

I have a class which stores an array and I need to write a method to return a pointer to that array so that other objects can access/modify it.

In an old version of my program, I did that by defining the array in C style. Ie, having a private element bool* list and then allocating memory in the constructor (and liberating that in the destructor). The method then was very simple:

bool* MyClass::getList() {
    return list;
}

Now, I have decided to rewrite the code and use std::vector<bool> instead of the classical array. The problem is that I have also modified the above method as:

bool* MyClass::getList() {
    return &(list[0]);
}

which seems to be the standard way of converting a C++ vector to C array. However, I cannot compile my code, I get the following error:

error: taking address of temporary [-fpermissive]
error: cannot convert ‘std::vector<bool>::reference* {aka std::_Bit_reference*}’ to ‘bool*’ in return

Could anyone help me out with this and tell me what shall I do?

(I have also read that another alternative is to use list.data() as the pointer but this would work only with the newest version of C++ compilers. I am not sure if this is a good idea or not).

Thanks,

Your course of thinking is right in general and taking a pointer of the first element would have been sufficient. However, std::vector<bool> is not really a vector of bool s as it is being implemented as a bit set. There is a general consensus among the C++ Standard Committee and the Library Working Group that vector<bool> should be deprecated and subsequently removed from the standard library, while the functionality will be reintroduced under a different name. But for now it is still there.

Getting back to your problem, you cannot really interpret a bitset as array of booleans. Therefore, there are two possible ways to go about this:

  1. Replace std::vector<bool> with one of the alternatives that would give you the right behavior.
  2. Convert std::vector<bool> to a C array manually when you need it. For example, you can use size() to pre-allocate memory, then iterate over a vector and fill our your array.

Hope it helps. Good Luck!

bool* MyClass::getList() {
    return &(list[0]);
}

This will work with any type, but std::vector<bool> has special implementation, where 8 elements are stored in 1 byte, so it doesn't seem possible to take raw pointer to it.

try string.copy():

bool array[100];
std::vector<bool>;
std::copy(vector.begin(), vector.end(), array);

std::vector<bool> uses an internal model where each bool is stored as a single bit. This means it's not a plain C array internally as you would like and you can't convert it to one effortlessly.

Either replace std::vector<bool> with std::deque<bool> and don't use the deque parts, this will give the expected behaviour.

The other solution is to use a std::vector<char> instead and treat the values as bools. This will give the expected behaviour as long as you cast the values properly when accessing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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