Can I have a variably modified vector in c++? - c++

Is there any way to do this:
std::vector<char[foo]> bar;
I need ONE contiguous array of char[foo] but I don't know the length until runtime. I need to be able to add blocks of char[foo] dynamically.
I need the inner array to be exactly the right size, so if foo was 3:
[ [1, 2, 3] [4, 5, 6] ... [x, y, z] ]
I could implement it myself but don't want to reinvent the wheel.

I'd suggest using
std::vector< std::vector< char > >
std::vector's internal memory is continuous.
Well, after a while, it appeared, that the whole memory must be continuous, which changes everything..
For this case, you can use directly std::vector< char >::insert( pos, begin, end ) to insert whole "chunks".
Something like: (I didn't test it, but you'll get the idea)
std::vector< char > bar;
// ..
{
std::vector< char > buff( foo );
// fill buff
bar.insert( bar.end(), buff.begin(), buff.end() );
}
Only keep in mind, that this may/will cause reallocation of the internal memory and several copies of the elements, but we're talking about char, so it's not a big deal.
You can avoid these reallocations using std::vector::reserve, if you have idea about the final size of bar.

I'd create a container class myself, use std::vector<char> internally, and create a wrapper slice class which holds a pointer to the container class and an index into the vector. The container class would hold the length foo. The container would have an iterator returning slices, each slice would have an iterator returning char.
[edit]
This solution also allocates contiguous memory.

After the question was reworded, you're probably looking for std::vector< std::array<char, 3> >
Memory will be contiguous for all elements. Assuming the last dimentsion is indeed a build time const.
Example:
std::vector< std::array<char, 3> > bar;
std::array<char, 3> foo = { 'a', 'b', 'c' };
bar.insert(bar.end(), 10, foo);

std::vector<char> bar(foo)
Constructs foo chars with in the vector, but because char is a built in type the constructor won't be called so it will be garbage values until initialised.
Instead use std::vector<char> bar(foo, char()) to fill it with initialised chars.

Related

What is the use of const std::vector?

I've seen code that uses a const std::vector, however can't see why it wouldn't make more sense to simply use an std::array instead.
The values for the vector seem to be initialized at compile-time.
What is the benefit of a const std::vector?
Edit: The vector was not big, only a couple of strings, however I see that this may be one advantage.
const std::vector lets you have a "fixed sized array" whose size you only know at run time but still allows you to have all the benefits of a standard container. If you used a raw/smart pointer in your code you will need to manually pass the size of the array that it points to into the function(s) that need to know the size of the array.
The std::vector could be used with a large list of data, because it could use Dynamic Allocated Array to store values and before C++20 it's doesn't have a constexpr constructor. But std::array uses a raw C-Array and it could be used at compile time with the stack size restricted list of data. So:
std::array const is good for:
Where your data size is less than the stack size.
Where data locality is important.
You want your list at the compile time (before C++20).
std::vector const is good for:
Where your data size is large than the stack size.
It's quite common to see C++ code that expects references to vectors, sometimes const, something like this:
auto do_sum(std::vector<int> const& numbers) -> int {
return std::accumulate(numbers.begin(), numbers.end(), 0);
}
Even though the good intentions are not about not copying memory, this code require the caller to allocate a std::vector, even though the amount of value and the actual values might be known at compile time.
This might be fixed by using a span instead:
auto do_sum(std::span<int const> numbers) -> int {
return std::accumulate(numbers.begin(), numbers.end(), 0);
}
Now this code don't require the users to allocate a vector for the array, and might use std::array, or plain C arrays.
Also, sometimes you don't know what are the numbers of element the array can have, and might be determined at runtime. Consider this:
auto create_vector() -> std::vector<int> {
auto vec = std::vector<int>{};
if (/* runtime condition */) {
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
} else {
vec.push_back(4);
vec.push_back(5);
}
return vec;
}
int main() {
std::vector<int> const vec = create_vector();
}
As you can see here, as the vector is moved from scope to scope, the const-ness changes and is expressing the intent of the developer to only make it mutable in some initializing scopes.
Because the std::array needs you to specify the size as part of the type. By using std::vector the compiler can work that out for your dynamically.
This matters in maintenance situations as it prevents errors. You only add/remove a string from the initializer of the object and the vector will have the correct size automatically. If you use an array you need to add/remove the string and change the type of the array.
const std::vector<std::string> dataV = { "A", "B", "C" };
const std::array<std::string, 3> dataA = { "A", "B", "C" };
If I now modify these to only have two values.
// The vector will auto resize
const std::vector<std::string> dataV = { "A", "B"};
// This will still be of size three.
// This is not usually what you want.
const std::array<std::string, 3> dataA = { "A", "B"};
// The person modifying the code has to manually spot that and
// change the type to explicitly have two member array. Note
// It may not be as obvious as you think as the type may
// be hidden with a type alias of some description
using DataStore = std::array<std::string, 3>;
/// Lots of code:
DataStore dataA = { "A", "B"}; // Would you have spotted.

changing std::array to std::vector

I have some code that uses a std::array. This is using the stack to allocate the memory. But I need to create an array of around 2MB. So doing some searching it seems that I can use std::vector that uses the heap.
I modified my code to do that
//alignas(128) std::array<uint32_t, array_size> write_data;
//alignas(128) std::array<uint32_t, array_size> read_data = { { 0 } };
alignas(128) std::vector<uint32_t> write_data[array_size];
alignas(128) std::vector<uint32_t> read_data[array_size];
But I am now getting error on other parts of the code. This uses some threads
std::thread read_thread(read, std::ref(dev),
(void*)read_data.data(), read_data.size() * sizeof(uint32_t), dma_block_size*32);
std::thread write_thread(write, std::ref(dev),
(void*)write_data.data(), write_data.size() * sizeof(uint32_t), num_dma_blocks);
E0289 no instance of constructor "std::thread::thread" matches the argument list
E0153 expression must have class type
The problem is on the read/write and read_data/write_data in the code above. Could someone tell me what the issue is as I am not really a cpp programmer. Thanks
You can init vector with a specific size using a constructor like this:
std::vector<int> vec(10);
By using std::vector<int> vec[10]; you declare array of 10 vectors. each vector is empty.
BTW: if you use std::vector<int> vec{10}; you declare a vector with a single int which is equal to 10.
It seems if I change the vector init to the below then it works
alignas(128) std::vector<uint32_t> write_data (array_size);
alignas(128) std::vector<uint32_t> read_data (array_size);

Initialise a dynamically allocated array [duplicate]

In C++, I can statically initialize an array, e.g.:
int a[] = { 1, 2, 3 };
Is there an easy way to initialize a dynamically-allocated array to a set of immediate values?
int *p = new int[3];
p = { 1, 2, 3 }; // syntax error
...or do I absolutely have to copy these values manually?
You can in C++0x:
int* p = new int[3] { 1, 2, 3 };
...
delete[] p;
But I like vectors better:
std::vector<int> v { 1, 2, 3 };
If you don't have a C++0x compiler, boost can help you:
#include <boost/assign/list_of.hpp>
using boost::assign::list_of;
vector<int> v = list_of(1)(2)(3);
You have to assign each element of the dynamic array explicitly (e.g. in a for or while loop)
However the syntax int *p = new int [3](); does initialize all elements to 0 (value initialization $8.5/5)
To avoid endless push_backs, I usually initialize a tr1::array and create a std::vector (or any other container std container) out of the result;
const std::tr1::array<T, 6> values = {T(1), T(2), T(3), T(4), T(5), T(6)};
std::vector <T> vec(values.begin(), values.end());
The only annoyance here is that you have to provide the number of values explicitly.
This can of course be done without using a tr1::array aswell;
const T values[] = {T(1), T(2), T(3), T(4), T(5), T(6)};
std::vector <T> vec(&values[0], &values[sizeof(values)/sizeof(values[0])]);
Althrough you dont have to provide the number of elements explicitly, I prefer the first version.
No, you cannot initialize a dynamically created array in the same way.
Most of the time you'll find yourself using dynamic allocation in situations where static initialization doesn't really make sense anyway. Such as when you have arrays containing thousands of items. So this isn't usually a big deal.
Using helper variable:
const int p_data[] = {1, 2, 3};
int* p = (int*)memcpy(new int[3], p_data, sizeof(p_data));
or, one line
int p_data[] = {1, 2, 3}, *p = (int*)memcpy(new int[3], p_data, sizeof(p_data));
Never heard of such thing possible, that would be nice to have.
Keep in mind that by initializing the array in the code that way
int a[] = { 1, 2, 3 };
..... only gains you easier code writing and NOT performance.
After all, the CPU will do the work of assigning values to the array, either way you do it.

C++ vector of fixed size vecors

How can I declare vector of fixed size vectors in C++?
For example:
vector of vectors with N elements.
Not this:
vector<vector<int> > v(N) //declares vector of N vectors
std::array is your friend here.
http://en.cppreference.com/w/cpp/container/array
For instance, to declare vector of vectors with N elements, you can
typedef std::array<int, N> N_array;
Then use
std::vector<N_array>
You could use std::array:
std::array<int, 10> myNumbers;
The only down side to this is you can't see how many "active" elements there are, since you don't push/emplace back. You use it like an ordinary( but safe ) array.
If you wish to have vector of fixed size, most likely you don't need one! Use std::array instead.
But still you insist to have one..
vector<vector<int> > vecOfVec(NumberOfVectors);
for ( int i = 0 ; i < NumberOfVectors; i++ )
vecOfVec[i].resize(NumberOfElementsInVector);

Can i push an array of int to a C++ vector?

Is there any problem with my code ?
std::vector<int[2]> weights;
int weight[2] = {1,2};
weights.push_back(weight);
It can't be compiled, please help to explain why:
no matching function for call to ‘std::vector<int [2], std::allocator<int [2]> >::push_back(int*&)’
The reason arrays cannot be used in STL containers is because it requires the type to be copy constructible and assignable (also move constructible in c++11). For example, you cannot do the following with arrays:
int a[10];
int b[10];
a = b; // Will not work!
Because arrays do not satisfy the requirements, they cannot be used. However, if you really need to use an array (which probably is not the case), you can add it as a member of a class like so:
struct A { int weight[2];};
std::vector<A> v;
However, it probably would be better if you used an std::vector or std::array.
You cant do that simply.
It's better you use either of these:
vector <vector<int>> (it's basically a two dimensional vector.It should work in your case)
vector< string > (string is an array of characters ,so you require a type cast later.It can be easily.).
you can declare an structure (say S) having array of int type within it i.e.
struct S{int a[num]} ,then declare vector of
vector< S>
So indirectly, you are pushing array into a vector.
Array can be added to container like this too.
int arr[] = {16,2,77,29};
std::vector<int> myvec (arr, arr + sizeof(arr) / sizeof(int) );
Hope this helps someone.
Arrays aren't copy constructable so you can't store them in containers (vector in this case). You can store a nested vector or in C++11 a std::array.
You should use std::array instead of simple array:
#include <vector>
#include <array>
std::vector<std::array<int, 2>> weights;
std::array<int, 2> weight = {1, 2};
weights.push_back(weight);
or with a constructor:
std::vector<std::array<int, 2>> weights;
weights.push_back(std::array<int, 2> ({1, 2});
One possible solution is:
std::vector<int*> weights;
int* weight = new int[2];
weight[0] =1; weight[1] =2;
weights.push_back(weight);
Just use
vector<int*> .That will definitely work.
A relevant discussion on the same topic : Pushing an array into a vector
Situation like:
int arr[3] = { 1, 2, 3 };
std::vector<int[]> v;
v.push_back(arr);
doesn't work with error "cannot initialize array in vector with .."
This, could be worked well
int * arr = new int[3] { 1, 2, 3 };
std::vector<int*> v;
v.push_back(arr);
To instantiate the vector, you need to supply a type, but int[2] is not a type, it's a declaration.