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

I have a class that stores an array, and I need to write a method to return a pointer to this array so that other objects can get / change it.

In the old version of my program, I did this by defining a CIe-style array having a private bool* list element, and then allocating memory in the constructor (and freeing it in the destructor). The method then was very simple:

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

Now I decided to rewrite the code and use std::vector<bool> instead of the classic array. The problem is that I 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 an array C. 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 

Can someone help me with this and tell me what to do?

(I also read that another alternative is to use list.data() as a pointer, but this will only work with the latest version of C ++ compilers. I'm not sure if this is a good idea or not.)

Thanks,

+4
source share
4 answers

Your course of thinking is generally correct, and removing the pointer to the first element would be enough. However, std::vector<bool> is not really a bool s vector , since it is implemented as a bit. There is a general consensus between the C ++ Standard Committee and the Library Working Group that vector<bool> should be deprecated and subsequently removed from the standard library, while functionality will be re-introduced under a different name. But while it still exists.

Returning to your problem, you cannot interpret bits as an array of logic elements. Therefore, there are two possible ways to do this:

  • Replace std::vector<bool> with one of the alternatives that will give you the correct behavior.
  • Convert std::vector<bool> to a C array manually when you need it. For example, you can use size() to preallocate memory, then iterate over the vector and fill our array.

Hope this helps. Good luck

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

This will work with any type, but std::vector<bool> has a special implementation, where 8 elements are stored in 1 byte, so it is not possible to use a raw pointer for this.

+3
source

try string.copy ():

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

std::vector<bool> uses an internal model, where each bool is stored as one bit. This means that it is not a simple C array inside, as you would like, and you cannot easily convert it.

Or replace std::vector<bool> with std::deque<bool> and do not use the deque parts, this will give the expected behavior.

Another solution is to use std::vector<char> instead and treat the values ​​like bools. This will give the expected behavior as long as you use the values ​​correctly when accessing it.

0
source

Source: https://habr.com/ru/post/1497232/


All Articles